Python 导入错误:没有名为“队列”的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33432426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:21:10  来源:igfitidea点击:

ImportError: No module named 'Queue'

pythonpython-requests

提问by Ali Faki

I am trying to import requestsmodule, but I got this error my python version is 3.4 running on ubuntu 14.04

我正在尝试导入requests模块,但出现此错误我的 python 版本是 3.4,在 ubuntu 14.04 上运行

>>> import requests
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 10, in <module>
    from queue import LifoQueue, Empty, Full
ImportError: cannot import name 'LifoQueue'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/requests/__init__.py", line 58, in <module>
   from . import utils
  File "/usr/local/lib/python3.4/dist-packages/requests/utils.py", line 26, in <module>
    from .compat import parse_http_list as _parse_list_header
  File "/usr/local/lib/python3.4/dist-packages/requests/compat.py", line 7, in <module>
    from .packages import chardet
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/__init__.py", line 3, in <module>
    from . import urllib3
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/__init__.py", line 10, in <module>
    from .connectionpool import (
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 12, in <module>
    from Queue import LifoQueue, Empty, Full
ImportError: No module named 'Queue'

采纳答案by Ali Faki

I solve the problem my issue was I had file named queue.py in the same directory

我解决了我的问题是我在同一目录中有名为 queue.py 的文件

回答by peter

Queue is in the multiprocessing module so:

队列位于多处理模块中,因此:

from multiprocessing import Queue

回答by Arjun sharma

You need install Queuelibeither via the Python Package Index (PyPI)or from source.

您需要Queuelib通过Python Package Index (PyPI)源代码或从源代码安装。

To install using pip:-

要使用 pip 安装:-

$ pip install queuelib

To install using easy_install:-

使用 easy_install 安装:-

$ easy_install queuelib

If you have downloaded a source tarball you can install it by running the following (as root):-

如果您已经下载了源代码压缩包,您可以通过运行以下命令(以 root 用户身份)来安装它:-

python setup.py install

回答by Pacerier

import queueis lowercaseqin Python 3.

import queue在 Python 3 中是小写的q

Change Qto qand it will be fine.

更改Qq,它会被罚款。

(See code in https://stackoverflow.com/a/29688081/632951for smart switching.)

(有关智能切换,请参阅https://stackoverflow.com/a/29688081/632951 中的代码 。)

回答by Panfeng Li

In my case it should be:

就我而言,它应该是:

from multiprocessing import JoinableQueue

from multiprocessing import JoinableQueue

Since in python2, Queue has methods like .task_done(), but in python3 multiprocessing.Queuedoesn't have this method, and multiprocessing.JoinableQueuedoes.

由于在 python2 中, Queue 有类似 的方法.task_done(),但在 python3multiprocessing.Queue中没有这个方法,并且multiprocessing.JoinableQueue有。

回答by asfawh

I run into the same problem and learn that queue module defines classes and exceptions, that defines the public methods (Queue Objects).

我遇到了同样的问题,并了解到队列模块定义了类和异常,它们定义了公共方法(队列对象)。

Ex.

前任。

workQueue = queue.Queue(10)

回答by GPrathap

It's because of the Python version. In Python 3 it's import Queue as queue; on the contrary in Python 2.x it's import queue. If you want it for both environments you may use something below as mentioned here

这是因为 Python 版本。在 Python 3 中,它是 import Queue as queue; 相反,在 Python 2.x 中它是import queue. 如果您希望在这两种环境中使用它,您可以使用下面提到的内容here

try:
   import queue
except ImportError:
   import Queue as queue