问题描述
我正在运行Python 2.5.
I am running Python 2.5.
这是我的文件夹树:
ptdraft/
nib.py
simulations/
life/
life.py
(我在每个文件夹中也都有__init__.py
,为便于阅读,在此省略)
(I also have __init__.py
in each folder, omitted here for readability)
如何从life
模块内部导入nib
模块?我希望无需修补sys.path就可以做到.
How do I import the nib
module from inside the life
module? I am hoping it is possible to do without tinkering with sys.path.
注意:正在运行的主模块在ptdraft
文件夹中.
Note: The main module being run is in the ptdraft
folder.
推荐答案
问题似乎与该模块位于父目录或类似内容无关.
It seems that the problem is not related to the module being in a parent directory or anything like that.
您需要将包含ptdraft
的目录添加到PYTHONPATH
You need to add the directory that contains ptdraft
to PYTHONPATH
您说import nib
与您一起工作,这可能意味着您已将ptdraft
本身(而不是其父项)添加到PYTHONPATH中.
You said that import nib
worked with you, that probably means that you added ptdraft
itself (not its parent) to PYTHONPATH.
这篇关于从父文件夹导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!