python 3中的队列导入错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29687837/
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 04:52:38  来源:igfitidea点击:

queue ImportError in python 3

pythonpython-3.x

提问by Liondancer

I am not sure why I am getting this ImportError. queue.Queue()is in the documentation.

我不知道为什么我会得到这个ImportErrorqueue.Queue()在文档中。

https://docs.python.org/3/library/queue.html?highlight=queue#queue.Queue

https://docs.python.org/3/library/queue.html?highlight=queue#queue.Queue

I am using it in a function like so:

我在这样的函数中使用它:

node_queue = queue.Queue()

node_queue = queue.Queue()

error:

错误:

Traceback (most recent call last):
  File "./test_jabba.py", line 15, in <module>
    from utils import gopher, jsonstream, datagen, event_gen, tree_diff, postal
  File "/Users/bli1/Development/QE/TrinityTestFramework/poc/utils/tree_diff.py", line 5, in <module>
    import queue
ImportError: No module named queue

Line 5 is import queue:

第 5 行是import queue

#!/usr/bin/env python3
import sys                      # access to basic things like sys.argv
import os                       # access pathname utilities
import argparse                 # for command-line options parsing
import queue

采纳答案by sorin

A kinda standard cross py2-py3 compatible version:

一种标准的跨 py2-py3 兼容版本:

try: 
    import queue
except ImportError:
    import Queue as queue

回答by Mike Vella

Replace #!/usr/bin/env python3with #!/usr/bin/python3

替换#!/usr/bin/env python3#!/usr/bin/python3

If your env isn't set up correctly then #!/usr/bin/env python3may not work. If #!/usr/bin/python3gives the same error then try running /usr/bin/python3 --versionin your shell as a sanity check.

如果您的 env 设置不正确,则#!/usr/bin/env python3可能无法正常工作。如果#!/usr/bin/python3出现相同的错误/usr/bin/python3 --version,请尝试在您的 shell 中运行作为健全性检查。

If you don't get a sensible output from /usr/bin/python3 --versionthen you have odd installation of python 3 and I suggest installing it using your package manager (apt-get, yum, homebrewor whatever you prefer - this will probably fix the !#/usr/bin/envissue).

如果你没有得到合理的输出,/usr/bin/python3 --version那么你有奇怪的 python 3 安装,我建议使用你的包管理器安装它(apt-get, yumhomebrew或者你喜欢的任何东西 - 这可能会解决这个!#/usr/bin/env问题)。

回答by m9_psy

Another way to avoid version problems is:

另一种避免版本问题的方法是:

import sys
is_py2 = sys.version[0] == '2'
if is_py2:
    import Queue as queue
else:
    import queue as queue

回答by Dhawaleswar

for ImportError: No module named 'Queue'in Python3, just replace the sentence "import Queue" with "import queue as Queue".

对于ImportError: No module named 'Queue'in Python3,只需将句子“ import Queue”替换为“ import queue as Queue”。