我有这样的路径架构:
>main_path/
__init__.py
config/
__init__.py
common.py
app_1/
__init__.py
config.py
index.py
>
我希望能够在config.py
:
>from main_path.config import common
>
虽然它不起作用。 Python 告诉我:
> $> pwd
..../main_path/app_1
$> python index.py
[...]
ImportError: No module named main_path.config
>
据我了解,如果我从 main_path 加载所有内容,这是可能的,尽管目标是让多个应用程序具有通用的配置文件。
我尝试将父目录添加到__path__
在应用程序_1/__init__
.py 但它没有改变任何东西。
我的下一步行动是建立一个符号链接,尽管我不太喜欢这个“解决方案”,所以如果您有任何想法可以帮助我,我将不胜感激!
提前致谢 !
根据模块文档 http://docs.python.org/tutorial/modules.html模块必须位于要导入的 PYTHONPATH 环境变量中。您可以在程序中修改它,例如:
import sys
sys.path.append('PATH_TO/config')
import common
欲了解更多信息,您可能想查看修改Python的搜索路径 http://docs.python.org/install/index.html#modifying-python-s-search-path in 安装Python模块 http://docs.python.org/install/index.html.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)