从 Python 中的单独路径导入模块
Importing modules from separate paths in Python
我正在尝试从单独的路径导入模块,但它返回的错误是 "module not found." 它从执行脚本的目录导入模块,但不会更改目录并从所述目录导入。
print(os.getcwd())
当我 运行 这样做时,在抛出找不到模块的错误之前,它会输出父目录,例如我将使用 test\import\modules.
我将 运行 \import\ 中的脚本从 \import\ 导入 test_0.pyd和来自 \modules 的 test_1.pyd(test.py 和 test_0 位于 \import\ 和 test_1 位于 \modules。此外,我尝试了相对导入,每个目录都包含 init.py).
import test_0 # this would work
from modules import test_1 # throws error that module isn't found
所以我 运行 打印命令和它 returns 它试图从 test\ 导入,我已经尝试更改目录但是它'我会说当我打印时工作目录改变了,但仍然输出它找不到模块。非常感谢任何帮助,谢谢。
编辑
http://prntscr.com/6ch7fq - 执行 test.py
http://prntscr.com/6ch80q - 导入目录
你在modules/directory中有__init__.py
个文件吗?这是 python 将其视为一个包所必需的。
查看What is __init__.py for?
当您从一个目录启动 python 时,该目录将添加到您的 PYTHONPATH
,因此可以从该目录及以下目录导入模块,前提是您有一个 __init__.py
每个目录,包括您 运行 python 来自的顶层。看这里:
~/Development/imports $ tree
.
├── __init__.py
├── mod1
│ ├── __init__.py
│ ├── a.py
├── mod2
│ ├── __init__.py
│ ├── b.py
├── top.py
所以当我们从 ~/Development/imports/
开始 python 时,我们可以访问 top
mod1.a
和 mod2.b
:
~/Development/imports $ python
Python 2.7.8 (default, Nov 3 2014, 11:21:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import top
>>> import mod1.a
>>> import mod2.b
>>> import sys
但是当我们从 mod1
内部开始 python 时,我们不允许离开 mod1
回到 top
或 mod2
:
~/Development/imports/mod1 $ python
Python 2.7.8 (default, Nov 3 2014, 11:21:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
>>> from .. import top
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Attempted relative import in non-package
>>> from ..mod2 import b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Attempted relative import in non-package
相对导入 from ..mod2 import b
仅适用于您启动的顶级模块下方的模块,因为它们都隐含在 python 路径中。
您无法从您开始的模块 外部 转义,除非将该特定路径添加到 PYTHONPATH
或 sys.path
:
~/Development/imports/mod1 $ PYTHONPATH=../ python
Python 2.7.8 (default, Nov 3 2014, 11:21:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
>>> import top
>>> import top.mod2.b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mod2.b
>>> import sys
>>> sys.path.append('~/Development/imports/mod2/')
>>> from mod2 import b
>>>
因此,您需要确保所有目录中都有一个 __init__.py
文件。您还需要确保从正确的位置开始 python,通常是顶级目录。您不能从目录结构的一半开始 python 并期望返回到顶部,或者侧身到另一个 directory/module.
我正在尝试从单独的路径导入模块,但它返回的错误是 "module not found." 它从执行脚本的目录导入模块,但不会更改目录并从所述目录导入。
print(os.getcwd())
当我 运行 这样做时,在抛出找不到模块的错误之前,它会输出父目录,例如我将使用 test\import\modules.
我将 运行 \import\ 中的脚本从 \import\ 导入 test_0.pyd和来自 \modules 的 test_1.pyd(test.py 和 test_0 位于 \import\ 和 test_1 位于 \modules。此外,我尝试了相对导入,每个目录都包含 init.py).
import test_0 # this would work
from modules import test_1 # throws error that module isn't found
所以我 运行 打印命令和它 returns 它试图从 test\ 导入,我已经尝试更改目录但是它'我会说当我打印时工作目录改变了,但仍然输出它找不到模块。非常感谢任何帮助,谢谢。
编辑 http://prntscr.com/6ch7fq - 执行 test.py http://prntscr.com/6ch80q - 导入目录
你在modules/directory中有__init__.py
个文件吗?这是 python 将其视为一个包所必需的。
查看What is __init__.py for?
当您从一个目录启动 python 时,该目录将添加到您的 PYTHONPATH
,因此可以从该目录及以下目录导入模块,前提是您有一个 __init__.py
每个目录,包括您 运行 python 来自的顶层。看这里:
~/Development/imports $ tree
.
├── __init__.py
├── mod1
│ ├── __init__.py
│ ├── a.py
├── mod2
│ ├── __init__.py
│ ├── b.py
├── top.py
所以当我们从 ~/Development/imports/
开始 python 时,我们可以访问 top
mod1.a
和 mod2.b
:
~/Development/imports $ python
Python 2.7.8 (default, Nov 3 2014, 11:21:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import top
>>> import mod1.a
>>> import mod2.b
>>> import sys
但是当我们从 mod1
内部开始 python 时,我们不允许离开 mod1
回到 top
或 mod2
:
~/Development/imports/mod1 $ python
Python 2.7.8 (default, Nov 3 2014, 11:21:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
>>> from .. import top
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Attempted relative import in non-package
>>> from ..mod2 import b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Attempted relative import in non-package
相对导入 from ..mod2 import b
仅适用于您启动的顶级模块下方的模块,因为它们都隐含在 python 路径中。
您无法从您开始的模块 外部 转义,除非将该特定路径添加到 PYTHONPATH
或 sys.path
:
~/Development/imports/mod1 $ PYTHONPATH=../ python
Python 2.7.8 (default, Nov 3 2014, 11:21:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
>>> import top
>>> import top.mod2.b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mod2.b
>>> import sys
>>> sys.path.append('~/Development/imports/mod2/')
>>> from mod2 import b
>>>
因此,您需要确保所有目录中都有一个 __init__.py
文件。您还需要确保从正确的位置开始 python,通常是顶级目录。您不能从目录结构的一半开始 python 并期望返回到顶部,或者侧身到另一个 directory/module.