[英]ImportError: No module named when module is there
I usually run python2 but I am playing with python3. 我通常运行python2,但我正在使用python3。 Right now I am confused as to why I am getting this error.
现在,我对为什么收到此错误感到困惑。
When I run the command ./test_web_events.py
in the tests
directory, I get: 当我在
tests
目录中运行命令./test_web_events.py
时,我得到:
Traceback (most recent call last):
File "./test_web_events.py", line 21, in <module>
import qe.util.scratchstore as scratchstore
ImportError: No module named 'qe'
However my project structure has qe
directory in it: 但是我的项目结构中有
qe
目录:
/python_lib
Makefile
/qe
__init__.py
/tests
__init__.py
test_web_events.py
/util
__init__.py
scratchstore.py
/trinity
__init__.py
I tried moving my /tests
directory into /python_lib
but I am still getting the same error: 我试图将我的
/tests
目录移到/python_lib
但是仍然出现相同的错误:
MTVL1289dd026:python_lib bli1$ ls
Makefile qe rundata setup.sh tests
MTVL1289dd026:python_lib bli1$ python3 tests/test_web_events.py
Traceback (most recent call last):
File "tests/test_web_events.py", line 21, in <module>
import qe.util.scratchstore as scratchstore
ImportError: No module named 'qe'
Here is my sys.path
for python2 这是我的python2的
sys.path
>>> import sys
>>> print sys.path
['', '/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']
sys.path
for python3 python3的
sys.path
>>> print(sys.path)
['', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages']
This is most likely because you haven't added /python_lib/qe
to your PYTHONPATH
. 这很可能是因为您尚未将
/python_lib/qe
添加到PYTHONPATH
。
When you try to import a module, the interpreter will look for it only in a certain number of places, you cannot arbitrarily try to import a module from anywhere. 当您尝试导入模块时,解释器只会在一定数量的位置中寻找它,您不能随意尝试从任何地方导入模块。
The most common ways are to have a package installed via pip
, to have the module sitting in the same directory as the .py
file, or to have added the path to that module to the PYTHONPATH
. 最常见的方法是通过
pip
安装软件包,将模块与.py
文件放在同一目录中,或者将该模块的路径添加到PYTHONPATH
。
See: https://docs.python.org/2/tutorial/modules.html#the-module-search-path 请参阅: https : //docs.python.org/2/tutorial/modules.html#the-module-search-path
It seems like the latter case is most likely what you want to do. 似乎后一种情况很可能是您想要执行的操作。 This is going to be dependent on your OS, but googling it should be straightforward.
这将取决于您的操作系统,但是使用谷歌搜索应该很简单。
The issue is that /python_lib
is not in Python path. 问题是
/python_lib
不在Python路径中。 The behavior is the same on both Python 2 and 3. Python 2和3的行为相同。
In general, do not run scripts from within (inside) a Python package , run them from the top-level directory instead: 通常, 请勿从Python包内部(内部)运行脚本,而应从顶级目录运行它们:
/python_lib$ python -m qe.tests.test_web_events
Thus /python_lib
is in Python path and /python_lib/qe/tests
is not. 因此
/python_lib
在Python路径中,而/python_lib/qe/tests
在。 It assumes that there is tests/__init__.py
file. 假定存在
tests/__init__.py
文件。
Do not modify sys.path
manually. 不要手动修改
sys.path
。 It may lead to subtle bugs related to importing modules. 这可能会导致与导入模块有关的细微错误。 There are better alternatives eg, if you don't want to run the scripts from
/python_lib
, just install the development version: 还有更好的选择,例如,如果您不想从
/python_lib
运行脚本,只需安装开发版本:
(your_virtualenv)/python_lib$ pip install -e .
Make sure you have __init__.py
file in all your package folders so that you tructure looks like 确保所有包文件夹中都有
__init__.py
文件,以使结构看起来像
/python_lib
Makefile
/qe
/tests
test_web_events.py
/util
__init__.py <------------ create this file
scratchstore.py
/trinity
__init__.py
and then the you cd
to python_lib
folder and run ``export PYTHONPATH=`pwd``` 然后您将其
cd
到python_lib
文件夹并运行``export PYTHONPATH =`pwd`''
Just #!/usr/bin/env python
add this to your all scripts. 只需
#!/usr/bin/env python
将其添加到您的所有脚本中即可。 Make sure it's on the top.Like; 确保它在顶部。
#!/usr/bin/env python
import qe
I assume you added Python 3 to path , if you did, the only problem is this. 我假设您将Python 3添加到path中 ,如果这样做,唯一的问题是这样。 You do not need
init.py
or sys.path or anything. 你不需要
init.py
或sys.path中或任何东西。 This line already finding Python path automatically if you added Python 3 to path, if Python 2 still on the path then it's normal you got error. 如果您将 Python 3 添加到路径中,那么该行已经自动找到了Python路径;如果Python 2仍在该路径上,那是正常的,您会出错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.