Python 导入相对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26849832/
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 imports relative path
提问by daniel451
I've got a project where I would like to use some python classes located in other directories.
我有一个项目,我想在其中使用位于其他目录中的一些 python 类。
Example structure:
示例结构:
/dir
+../subdirA
+../subdirB
+../mydir
The absolute path varies, because this project is run on different machines.
绝对路径不同,因为这个项目运行在不同的机器上。
When my python file with MySampleClasslocated in /mydiris executed, how do I import OtherClassRootlocated in /diror OtherClassAlocated in /subdirA?
当我的蟒蛇文件MySampleClass位于/ mydir的执行,如何导入OtherClassRoot位于/目录或OtherClassA位于/ subdirA?
I tried things like:
我试过这样的事情:
from . import MySampleClass as msc
or
或者
from ../ import MySampleClass as msc
but this always fails or gives me error messages like Attempted relative import in non-package
但这总是失败或给我错误消息,例如在非包中尝试相对导入
So, whats the right way to relatively import python files?
那么,相对导入python文件的正确方法是什么?
采纳答案by Wyrmwood
You will need an __init__.pyin the mydir directory (and it can be empty), then as long as dir is in the sys path, assuming your MySampleClass is in myfile.py and myfile.py is in mydir
您将需要__init__.pymydir 目录中的一个(它可以为空),然后只要 dir 在 sys 路径中,假设您的 MySampleClass 在 myfile.py 中,而 myfile.py 在 mydir 中
from mydir.myfile import MySampleClass
If you want to import top level functions from a file called util.py that reside in subdirA into myfile.py (where your class is), then an __init__.pymust be in subdirA and then in myfile.py
如果您想将位于 subdirA 中的名为 util.py 的文件中的顶级函数导入 myfile.py(您的类所在的位置),则__init__.py必须在 subdirA 中,然后在 myfile.py 中
from subdirA.util import somefunc, someotherfunc
The same is true of the sys path, that is, you must either start from 'dir' or add it. Everything is imported from the top level of the package (usually your project folder).
sys 路径也是如此,也就是说,您必须从 'dir' 开始或添加它。一切都是从包的顶层(通常是你的项目文件夹)导入的。
However, for module testing, where you might run a function from util in the interpreter, if you start from subdirA, you will need to add dir to the sys path, so your imports can resolve.
但是,对于模块测试,您可能会在解释器中从 util 运行函数,如果您从 subdirA 开始,则需要将 dir 添加到 sys 路径,以便您的导入可以解析。
>>> import sys
>>> sys.path.append('../dir')
but this is a hack and would be preferable to only use from the interactive interpreter when you are testing. You can also add 'dir' to your site packages in a pth file.
但这是一个 hack,最好只在测试时从交互式解释器中使用。您还可以将“dir”添加到 pth 文件中的站点包中。
To use relative imports, you would need a deeper nested folder, like subdirA/subdirofA, then in subdirofA, you could use . to back out (like from .subdirB). Really, for me, relative imports are somewhat difficult to see the utility. It's better for me to use direct imports relative to the project directory, but I could see them being useful if you wanted to nest a naive sub package, but again, still better to be explicit than implicit if possible.
要使用相对导入,您需要一个更深的嵌套文件夹,例如 subdirA/subdirofA,然后在 subdirofA 中,您可以使用 . 退出(如from .subdirB)。真的,对我来说,相对进口有点难以看出效用。我最好使用相对于项目目录的直接导入,但是如果您想嵌套一个简单的子包,我可以看到它们很有用,但同样,如果可能的话,显式比隐式更好。
also see this
也看到这个
回答by jacoolee
On dir structure as following:
目录结构如下:
/dir
+../subdirA
+../subdirB
+../mydir
combining with linux command ln, we can make things a lot simpler:
结合 linux 命令ln,我们可以让事情变得更简单:
1. cd dir/mydir
2. ln -s ../subdirA ./
3. ln -s ../subdirB ./
And, now if you want to import some_stufffrom file: dir/subdirA/fileA.pyinto your file: dir/mydir/myfile.py, simply import like this:
而且,现在如果您想some_stuff从 file:dir/subdirA/fileA.py导入到您的 file: 中dir/mydir/myfile.py,只需像这样导入:
from subdirA.fileA import some_stuff
And, the same applies to 'dir/subdirB'
并且,这同样适用于“dir/subdirB”
What's More, this works for setup.pyprocess, :)
更重要的是,这适用于setup.py流程,:)
回答by reggie
first add the relative paths to the pythonpath
首先将相对路径添加到pythonpath
import os
import sys
cwd = os.getcwd()
sys.path.append(cwd + '/../subdirA/')
sys.path.append(cwd + '/../subdirB/')
then import the modules.
然后导入模块。

