繁体   English   中英

ImportError:当模块在那里时,没有模块被命名

[英]ImportError: No module named when module is there

我通常运行python2,但我正在使用python3。 现在,我对为什么收到此错误感到困惑。

当我在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'

但是我的项目结构中有qe目录:

/python_lib
   Makefile
   /qe
      __init__.py
      /tests
         __init__.py
         test_web_events.py
      /util
         __init__.py
         scratchstore.py
      /trinity
         __init__.py

我试图将我的/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'

这是我的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']

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']

这很可能是因为您尚未将/python_lib/qe添加到PYTHONPATH

当您尝试导入模块时,解释器只会在一定数量的位置中寻找它,您不能随意尝试从任何地方导入模块。

最常见的方法是通过pip安装软件包,将模块与.py文件放在同一目录中,或者将该模块的路径添加到PYTHONPATH

请参阅: https//docs.python.org/2/tutorial/modules.html#the-module-search-path

似乎后一种情况很可能是您想要执行的操作。 这将取决于您的操作系统,但是使用谷歌搜索应该很简单。

问题是/python_lib不在Python路径中。 Python 2和3的行为相同。

通常, 请勿从Python包内部(内部)运行脚本,而应从顶级目录运行它们:

/python_lib$ python -m qe.tests.test_web_events

因此/python_lib在Python路径中,而/python_lib/qe/tests在。 假定存在tests/__init__.py文件。

不要手动修改sys.path 这可能会导致与导入模块有关的细微错误。 还有更好的选择,例如,如果您不想从/python_lib运行脚本,只需安装开发版本:

(your_virtualenv)/python_lib$ pip install -e .

确保所有包文件夹中都有__init__.py文件,以使结构看起来像

/python_lib
   Makefile
   /qe
      /tests
         test_web_events.py
      /util
         __init__.py                <------------ create this file
         scratchstore.py
      /trinity
      __init__.py

然后您将其cdpython_lib文件夹并运行``export PYTHONPATH =`pwd`''

只需#!/usr/bin/env python将其添加到您的所有脚本中即可。 确保它在顶部。

#!/usr/bin/env python
import qe

我假设您将Python 3添加到path中 ,如果这样做,唯一的问题是这样。 不需要 init.py或sys.path中或任何东西。 如果您 Python 3 添加到路径中,那么该行已经自动找到了Python路径;如果Python 2仍在该路径上,那是正常的,您会出错。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM