Python:从父文件夹导入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4636976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: Importing a file from a parent folder
提问by Sascha
...Now I know this question has been asked many times & I have looked at these other threads. Nothing so far has worked, from using sys.path.append('.') to just import foo
...现在我知道这个问题已经被问过很多次了,我已经看过这些其他主题了。到目前为止没有任何效果,从使用 sys.path.append('.') 到 import foo
I have a python file that wishes to import a file (that is in its parent directory). Can you help me figure out how my child file can successfully import its a file in its parent directory. I am using python 2.7
我有一个希望导入文件(位于其父目录中)的 python 文件。你能帮我弄清楚我的子文件如何成功地将它的一个文件导入到它的父目录中。我正在使用 python 2.7
The structure is like so (each directory also has the __init__.py file in it):
结构是这样的(每个目录中也有__ init__.py 文件):
StockTracker/
__Comp/
____a.py
____SubComp/
______b.py
StockTracker/
__ Comp/
____ a.py
____ 子组件/
__ ____b.py
Inside b.py, I would like to import a.py: So I have tried each of the following but I still get an error inside b.py saying "There is no such module a"
在 b.py 中,我想导入 a.py: 所以我已经尝试了以下每一个,但我仍然在 b.py 中收到一个错误,说“没有这样的模块 a”
import a
import .a
import Comp.a
import StockTracker.Comp.a
import os
import sys
sys.path.append('.')
import a
sys.path.remove('.')
回答by Thomas K
from .. import a
Should do it. This will only work on recent versions of Python--from 2.6, I believe [Edit: since 2.5].
应该做。这仅适用于 Python 的最新版本——从 2.6 开始,我相信 [编辑:自 2.5]。
Each level (Comp and Subcomp) must also be have an __init__.pyfile for this to work. You've said that they do.
每个级别(Comp 和 Subcomp)也必须有一个__init__.py文件才能工作。你说过他们会的。
回答by David German
If the Comp directory is in your PYTHONPATH environment variable, plain old
如果 Comp 目录在你的 PYTHONPATH 环境变量中,那么简单
import a
will work.
将工作。
If you're using Linux or OS X, and launching your program from the bash shell, you can accomplish that by
如果您使用的是 Linux 或 OS X,并从 bash shell 启动您的程序,您可以通过
export PYTHONPATH=$PYTHONPATH:/path/to/Comp
For Windows, take a look at these links:
对于 Windows,请查看以下链接:
EDIT:
编辑:
To modify the path programmatically, you were on the right track in your original question. You just need to add the parent directory instead of the current directory.
要以编程方式修改路径,您在原始问题中走在正确的轨道上。您只需要添加父目录而不是当前目录。
sys.path.append("..")
import a
回答by DixonD
When packages are structured into subpackages (as with the sound package in the example), you can use absolute imports to refer to submodules of siblings packages. For example, if the module sound.filters.vocoder needs to use the echo module in the sound.effects package, it can use from sound.effects import echo.
Starting with Python 2.5, in addition to the implicit relative imports described above, you can write explicit relative imports with the from module import name form of import statement. These explicit relative imports use leading dots to indicate the current and parent packages involved in the relative import. From the surround module for example, you might use:
from . import echo from .. import formats from ..filters import equalizer
当包被构造成子包时(如示例中的声音包),您可以使用绝对导入来引用兄弟包的子模块。例如,如果模块 sound.filters.vocoder 需要使用 sound.effects 包中的 echo 模块,则可以使用 from sound.effects import echo。
从 Python 2.5 开始,除了上述隐式相对导入之外,您还可以使用 import 语句的 from 模块导入名称形式编写显式相对导入。这些显式相对导入使用前导点来指示相关导入中涉及的当前包和父包。例如,从环绕声模块中,您可以使用:
from . import echo from .. import formats from ..filters import equalizer
Quote from here http://docs.python.org/tutorial/modules.html#intra-package-references
从这里引用http://docs.python.org/tutorial/modules.html#intra-package-references

