无法在父目录中导入模块
Cannot import a module in parent directory
我正在尝试让一个 python 项目在 Ubuntu 17 中运行,但我在加载模块时遇到了问题。我已经能够用一个简单的例子来隔离问题。此示例适用于 Windows 10,但不适用于 Ubuntu。帮助让它工作将不胜感激!
以下是我要执行的步骤:
首先,我在 ~/dev 中创建一个名为 code_playground 的项目目录。接下来我为这个项目创建一个 virtualenv:
~/dev$ which virtualenv
/usr/local/bin/virtualenv
~/dev$ virtualenv -p python3.6 code_playground/
Running virtualenv with interpreter /usr/bin/python3.6
Using base prefix '/usr'
New python executable in /home/user/dev/code_playground/bin/python3.6
Also creating executable in /home/user/dev/code_playground/bin/python
Installing setuptools, pip, wheel...done.
我激活虚拟环境:
~/dev/code_playground$ source ./bin/activate
(code_playground) ~/dev/code_playground$
似乎有效:
(code_playground) ~/dev/code_playground$ which python
/home/user/dev/code_playground/bin/python
我创建了一个名为 mod_a.py 的文件,其中包含一个简单的函数:
def print_name(name):
print('Your name is {0}'.format(name))
现在我创建一个名为 sub 的子目录:
(code playground) ~/dev/code_playground$ mkdir sub
(code playground) ~/dev/code_playground$ cd sub
(code playground) ~/dev/code_playground/sub$
在 sub 中,我创建了一个名为 mod_b.py 的文件,其中包含以下内容:
from mod_a import print_name
print_name('Joe')
我尝试 运行 mod_b.py,但出现错误:
(code playground) ~/dev/code_playground/sub$ python mod_b.py
Traceback (most recent call last):
File "mod_b.py", line 1, in <module>
from mod_a import print_name
ImportError: No module named mod_a
Python 解释器必须知道在哪里可以找到 module_a.py
。事实上,导入模块 module_a.py
的文件位于 module_a.py
所在目录的子目录中,"is not a thing that help much"。你可以尝试一些事情:
1- 按照@CristiFati 的建议,将您的 mod_a.py
路径(在脚本 运行 之前)添加到您的 Python 路径中。
2- 将您的 mod_a.py
路径(使用 Python 代码)添加到您的 Python 的路径中,执行类似(讨厌的):
import
sys.path.insert(0, '/home/user/dev/code_playground/bin/python')
from mod_a import print_name
...
3- 评估相对进口 (6.4.2. Intra-package References)
我正在尝试让一个 python 项目在 Ubuntu 17 中运行,但我在加载模块时遇到了问题。我已经能够用一个简单的例子来隔离问题。此示例适用于 Windows 10,但不适用于 Ubuntu。帮助让它工作将不胜感激!
以下是我要执行的步骤:
首先,我在 ~/dev 中创建一个名为 code_playground 的项目目录。接下来我为这个项目创建一个 virtualenv:
~/dev$ which virtualenv
/usr/local/bin/virtualenv
~/dev$ virtualenv -p python3.6 code_playground/
Running virtualenv with interpreter /usr/bin/python3.6
Using base prefix '/usr'
New python executable in /home/user/dev/code_playground/bin/python3.6
Also creating executable in /home/user/dev/code_playground/bin/python
Installing setuptools, pip, wheel...done.
我激活虚拟环境:
~/dev/code_playground$ source ./bin/activate
(code_playground) ~/dev/code_playground$
似乎有效:
(code_playground) ~/dev/code_playground$ which python
/home/user/dev/code_playground/bin/python
我创建了一个名为 mod_a.py 的文件,其中包含一个简单的函数:
def print_name(name):
print('Your name is {0}'.format(name))
现在我创建一个名为 sub 的子目录:
(code playground) ~/dev/code_playground$ mkdir sub
(code playground) ~/dev/code_playground$ cd sub
(code playground) ~/dev/code_playground/sub$
在 sub 中,我创建了一个名为 mod_b.py 的文件,其中包含以下内容:
from mod_a import print_name
print_name('Joe')
我尝试 运行 mod_b.py,但出现错误:
(code playground) ~/dev/code_playground/sub$ python mod_b.py
Traceback (most recent call last):
File "mod_b.py", line 1, in <module>
from mod_a import print_name
ImportError: No module named mod_a
Python 解释器必须知道在哪里可以找到 module_a.py
。事实上,导入模块 module_a.py
的文件位于 module_a.py
所在目录的子目录中,"is not a thing that help much"。你可以尝试一些事情:
1- 按照@CristiFati 的建议,将您的 mod_a.py
路径(在脚本 运行 之前)添加到您的 Python 路径中。
2- 将您的 mod_a.py
路径(使用 Python 代码)添加到您的 Python 的路径中,执行类似(讨厌的):
import
sys.path.insert(0, '/home/user/dev/code_playground/bin/python')
from mod_a import print_name
...
3- 评估相对进口 (6.4.2. Intra-package References)