Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ImportError:当模块存在时,没有命名的模块_Python_Python 3.x - Fatal编程技术网

Python ImportError:当模块存在时,没有命名的模块

Python ImportError:当模块存在时,没有命名的模块,python,python-3.x,Python,Python 3.x,我通常跑蟒蛇2,但我在玩蟒蛇3。现在我不明白为什么我会犯这个错误 当我在tests目录中运行命令/test\u web\u 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 'q

我通常跑蟒蛇2,但我在玩蟒蛇3。现在我不明白为什么我会犯这个错误

当我在
tests
目录中运行命令
/test\u web\u 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'
我尝试将我的
/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'
python3的系统路径

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

见:


看起来后一种情况最有可能是你想要做的。这取决于你的操作系统,但谷歌搜索应该很简单。

确保你的所有软件包文件夹中都有
\uuuu init\uuuuupy.py
文件,这样你的结构看起来就像

/python_lib
   Makefile
   /qe
      /tests
         test_web_events.py
      /util
         __init__.py                <------------ create this file
         scratchstore.py
      /trinity
      __init__.py
/python\u lib
生成文件
/量化宽松
/测验
test_web_events.py
/利用率
__初始化.py只是
#/usr/bin/env python
将其添加到所有脚本中。确保它在上面

#!/usr/bin/env python
import qe

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

问题是
/Python\u lib
不在Python路径中。Python2和Python3上的行为都是相同的

通常,从顶级目录运行它们:

/python_lib$ python -m qe.tests.test_web_events
因此,
/python_-lib
在python路径中,而
/python_-lib/qe/tests
不在。它假定存在
tests/\uuuuu init\uuuuu.py
文件

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

(your_virtualenv)/python_lib$ pip install -e .

上面说它在test_web_events.py中python_lib dir中有init.py文件吗?@Andrew_Lvov与此无关,他没有试图导入
python_lib
@mattm,你是对的。Andrew_Lvov的意思是一个
\u init_.py
文件(它可以是空的)。然后修复PYTHONPATH。通过运行
export PYTHONPATH=/path/to/python_lib
,他们都有这个吗?你确定吗?@Liondancer那你做错了什么。我经常使用
模块
,这一直是个问题。确保你所有的脚本都在顶部有这个。我认为这与Python2和3路径有关=/我通常使用2,但我拉了一个项目,它是用3编写的。我知道我已经安装了3个,但我不知道如何来回切换
sys。path
不是
PYTHONPATH
在我将
/tests
移动到
/python_lib
=/您是否从
/python_lib
运行它?如果您打印
sys.path
;您应该在其中看到这个目录。更新您的问题并包含完整的回溯–您不会得到相同的错误。是的,我确实在
python_lib
question is updated中运行了它@狮子座:这是我的错。我已经删除了坏掉的
python测试/test\u web\u events.py
命令。它需要类似的东西(在您的例子中,搜索
qe
而不是
twisted
)。不确定这意味着什么=/
MTVL1289dd026:python_lib bli1$python3-m qe.tests.test_web_events.py/Library/Frameworks/python.framework/Versions/3.4/bin/python3:查找“qe.tests.tests.test_web_events.py”规范时出错(:“module”对象没有属性“\uuuu path””
/python_lib$ python -m qe.tests.test_web_events
(your_virtualenv)/python_lib$ pip install -e .