How do I import a module(python file) that resides in the parent directory?
如何导入位于父目录中的模块(python文件)?
Both directories have a __init__.py
file in them but I still cannot import a file from the parent directory?
两个目录都有一个__init__。py文件,但我仍然不能从父目录导入文件?
In this folder layout, Script B is attempting to import Script A:
在这个文件夹布局中,脚本B试图导入脚本A:
Folder A:
__init__.py
Script A:
Folder B:
__init__.py
Script B(attempting to import Script A)
The following code in Script B doesn't work:
脚本B中的以下代码不起作用:
import ../scriptA.py # I get a compile error saying the "." is invalid
3 个解决方案
#1
54
You don't import scripts in Python you import modules. Some python modules are also scripts that you can run directly (they do some useful work at a module-level).
在Python中不导入脚本,而是导入模块。一些python模块也是可以直接运行的脚本(它们在模块级做一些有用的工作)。
In general it is preferable to use absolute imports rather than relative imports.
一般来说,最好是使用绝对进口而不是相对进口。
toplevel_package/
├── __init__.py
├── moduleA.py
└── subpackage
├── __init__.py
└── moduleB.py
In moduleB
:
在moduleB:
from toplevel_package import moduleA
If you'd like to run moduleB.py
as a script then make sure that parent directory for toplevel_package
is in your sys.path
.
如果你想要运行moduleB。py作为脚本,然后确保toplevel_package的父目录位于您的sys.path中。
#2
24
From the docs:
从文档:
from .. import scriptA
You can do this in packages, but not in scripts you run directly. From the link above:
您可以在包中执行此操作,但不能在直接运行的脚本中执行此操作。从上面的链接:
Note that both explicit and implicit relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application should always use absolute imports.
注意,显式和隐式相对导入都基于当前模块的名称。由于主模块的名称总是“__main__”,所以用于作为Python应用程序的主模块的模块应该总是使用绝对导入。
If you create a script that imports A.B.B, you won't receive the ValueError.
如果您创建了一个导入A.B.的脚本。B,你不会收到ValueError。
#3
4
If you want to run the script directly, you can:
如果你想直接运行脚本,你可以:
- Add the FolderA's path to the environment variable (
PYTHONPATH
). - 将FolderA的路径添加到环境变量(PYTHONPATH)。
- Add the path to
sys.path
in the your script. - 添加到sys的路径。您的脚本中的路径。
Then:
然后:
import module_you_wanted
#1
54
You don't import scripts in Python you import modules. Some python modules are also scripts that you can run directly (they do some useful work at a module-level).
在Python中不导入脚本,而是导入模块。一些python模块也是可以直接运行的脚本(它们在模块级做一些有用的工作)。
In general it is preferable to use absolute imports rather than relative imports.
一般来说,最好是使用绝对进口而不是相对进口。
toplevel_package/
├── __init__.py
├── moduleA.py
└── subpackage
├── __init__.py
└── moduleB.py
In moduleB
:
在moduleB:
from toplevel_package import moduleA
If you'd like to run moduleB.py
as a script then make sure that parent directory for toplevel_package
is in your sys.path
.
如果你想要运行moduleB。py作为脚本,然后确保toplevel_package的父目录位于您的sys.path中。
#2
24
From the docs:
从文档:
from .. import scriptA
You can do this in packages, but not in scripts you run directly. From the link above:
您可以在包中执行此操作,但不能在直接运行的脚本中执行此操作。从上面的链接:
Note that both explicit and implicit relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application should always use absolute imports.
注意,显式和隐式相对导入都基于当前模块的名称。由于主模块的名称总是“__main__”,所以用于作为Python应用程序的主模块的模块应该总是使用绝对导入。
If you create a script that imports A.B.B, you won't receive the ValueError.
如果您创建了一个导入A.B.的脚本。B,你不会收到ValueError。
#3
4
If you want to run the script directly, you can:
如果你想直接运行脚本,你可以:
- Add the FolderA's path to the environment variable (
PYTHONPATH
). - 将FolderA的路径添加到环境变量(PYTHONPATH)。
- Add the path to
sys.path
in the your script. - 添加到sys的路径。您的脚本中的路径。
Then:
然后:
import module_you_wanted