Python importing subdirectories

南笙酒味 提交于 2019-12-06 09:53:53

Put this in prog.py:

import module

Python will only load packages or modules that are imported.

To make it work, you probably need jcollado's answer as well.

If you have the following structure:

package
  __init__.py
  module.py

In __init__.py you can either try this:

import package.module

or this:

from . import module

This way, if package is in your PYTHONPATH, you'll get the expected behaviour:

>>> import package
hello
Martin Thurau

Suppose you have a file structure like this:

prog.py
module/
    __init__.py
    code.py

Then import module would import the code in module/__init__.pyand import module.code or from module import code would import the code in module/code.py under the local name "module.code" or "code".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!