[英]importing from subdirectories in Python
I am new to Python and I am confused in importing files from subdirectories. 我是Python的新手,从子目录导入文件时我很困惑。 My filesystem structure is as follows:
我的文件系统结构如下:
/doc/a/main.py
/second.py
/doc/b/prog.py
Now I want to import main and second in prog.py. 现在我想在prog.py中导入main和second。 Can anyone suggest me a method to do so?
有人可以建议我这样做吗?
import sys
sys.path.append("../a")
import main, second
and then call prog.py
while in directory b
. 然后在目录
b
调用prog.py
First you should learn what a proper Python package makes: it needs to contain a file named __init__.py
首先,您应该了解适当的Python软件包的功能:它需要包含一个名为
__init__.py
的文件。
The search path of Python can be configured either by modifying sys.path or setting the $PYTHONPATH environment variable. 可以通过修改sys.path或设置$ PYTHONPATH环境变量来配置Python的搜索路径。
See also 也可以看看
http://docs.python.org/tutorial/modules.html http://docs.python.org/tutorial/modules.html
or google for "python import pythonpath" 或google为“ python import pythonpath”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.