Python 从父目录导入文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/16780014/
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
Import file from parent directory?
提问by Marty Wallace
I have the following directory structure:
我有以下目录结构:
application
    tests
        main.py
    main.py
application/main.py contains some functions.
application/main.py 包含一些函数。
tests/main.py will contain my tests for these functions but I can't import the top level main.py. I get the following error:
tests/main.py 将包含我对这些函数的测试,但我无法导入顶级 main.py。我收到以下错误:
ImportError: Import by filename is not supported.
I am attempting to import using the following syntax:
我正在尝试使用以下语法导入:
import main
What am I doing wrong?
我究竟做错了什么?
回答by BrenBarn
You cannot import things from parent/sibling directories as such.  You can only import things from directories on the system path, or the current directory, or subdirectories within a package.  Since you have no __init__.pyfiles, your files do not form a package, and you can only import them by placing them on the system path.
您不能像这样从父/兄弟目录导入内容。您只能从系统路径上的目录、当前目录或包内的子目录导入内容。由于您没有__init__.py文件,因此您的文件不会形成包,您只能通过将它们放在系统路径上来导入它们。
回答by 0x90
You must add the application dir to your path:
您必须将应用程序目录添加到您的路径中:
import sys
sys.path.append("/path/to/dir")
from app import object
Or from shell:
或从外壳:
setenv PATH $PATH:"path/to/dir"
In case you use windows: Adding variable to path in windows.
如果您使用 windows:将变量添加到windows 中的路径。
Or from the command line:
或者从命令行:
set PATH=%PATH%;C:\path\to\dir
回答by Lennart Regebro
First of all you need to make your directories into packages, by adding __init__.pyfiles:
首先,您需要通过添加__init__.py文件将目录打包成包:
application
    tests
        __init__.py
        main.py
    __init__.py
    main.py
Then you should make sure that the directory above application is on sys.path. There are many ways to do that, like making the application infto a package and installing it, or just executing things in the right folder etc.
然后您应该确保应用程序上面的目录位于sys.path. 有很多方法可以做到这一点,比如将应用程序打包并安装,或者只是在正确的文件夹中执行操作等。
回答by Lennart Regebro
If you'd like your script to be more portable, consider finding the parent directory automatically:
如果您希望脚本更具可移植性,请考虑自动查找父目录:
import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# import ../db.py
import db
回答by StackUP
in python . exists for same directory, .. for parent directory to import a file from parent directory you can use ..
在蟒蛇。存在于同一目录,.. 对于父目录从父目录导入文件,您可以使用 ..
from .. import filename (without .py extension)
from .. 导入文件名(没有 .py 扩展名)
回答by Andy Fraley
To import a file in a different subdirectory of the parent directory, try something like this:
要在父目录的不同子目录中导入文件,请尝试以下操作:
sys.path.append(os.path.abspath('../other_sub_dir'))
import filename_without_py_extension
Edit: Missing closing bracket.
编辑:缺少右括号。

