python - 找不到 Python 模块 - 队列、websocketserver
问题描述
更新 - 2019 年 4 月 30 日:
感谢 xyres 帮助我。我将行更改为 queue.Queue() ,现在出现以下错误:
$ python3 netgrafio1.py
2019-04-30 16:58:45,470 - DEBUG - [asyncio] - Using selector: EpollSelector
2019-04-30 16:58:45,472 - INFO - [WebSocketServer] - Starting WebSocket server on port 8080
2019-04-30 16:58:45,472 - INFO - [WebSocketServer] - Start collector server
2019-04-30 16:58:45,473 - INFO - [WebSocketServer] - Waiting for incoming data ...
2019-04-30 16:58:45,477 - INFO - [WebServer] - Listening on 5000
Exception in thread Thread-3:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/home/ubuntu/netgrafio/lib/WebServer.py", line 114, in start_server
http_server = Thread(target=IOLoop.instance().start)
File "/home/ubuntu/.local/lib/python3.5/site-packages/tornado/ioloop.py", line 201, in instance
return IOLoop.current()
File "/home/ubuntu/.local/lib/python3.5/site-packages/tornado/ioloop.py", line 265, in current
loop = asyncio.get_event_loop()
File "/usr/lib/python3.5/asyncio/events.py", line 632, in get_event_loop
return get_event_loop_policy().get_event_loop()
File "/usr/lib/python3.5/asyncio/events.py", line 578, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-3'.
Traceback (most recent call last):
File "netgrafio1.py", line 100, in <module>
main(parse_args(sys.argv[1:]))
File "netgrafio1.py", line 95, in main
tcp_server = JSONServer(host, int(params.tcp_port), in_queue)
NameError: name 'JSONServer' is not defined
更新 - 28/04/19:
- 安装了 pip3、龙卷风、烧瓶
- 在脚本中将“队列”更改为“队列”
新错误:
ubuntu@ip-172-31-11-55:~/netgrafio$ python3 netgrafio1.py
Traceback (most recent call last):
File "netgrafio1.py", line 100, in <module>
main(parse_args(sys.argv[1:]))
File "netgrafio1.py", line 81, in main
in_queue = queue()
TypeError: 'module' object is not callable
我正在尝试运行一个脚本,遵循这个netgrafio 的链接。请注意,netgrafio1.py 是我尝试修复导入的编辑版本。
这是 netgrafio1.py 的内容:
import sys
import argparse
import logging
try:
import queue
except ImportError:
import Queue as queue
# Local packages
#from lib.TCPServer import JSONServer
from lib.WebSocketServer import WebSocketServer
from lib.WebServer import WebServer
try:
import SocketServer as socketserver
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
except ImportError:
import socketserver
from http.server import HTTPServer, BaseHTTPRequestHandler
def parse_args(params):
""" Parse cmd line arguments """
parser = argparse.ArgumentParser(
description="netgrafio - visualize your network",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# Set parameters
parser.add_argument("--tcp-port", action="store", type=int,
help="Specify TCP port to listen for JSON packets", default=8081)
parser.add_argument("--ws-port", action="store", type=int,
help="Specify WebSocket port to send JSON data to", default=8080)
parser.add_argument("--web-port", action="store", type=str,
help="Specify web port to server web application", default=5000)
parser.add_argument("--host", action="store", default="127.0.0.1",
help="Specify host to bind socket on")
args = parser.parse_args(params)
return args
def main(params):
# Global logging settings
logging.basicConfig(level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - [%(name)s] - %(message)s")
# Init in queue (producer and consumer pattern)
in_queue = Queue()
# Set default host
host = params.host
# Start WebSocket server
websocket_server = WebSocketServer(host, int(params.ws_port), in_queue)
websocket_server.start()
# Start Web Server
web_server = WebServer(host, int(params.web_port))
web_server.start()
# Start JSON server
tcp_server = JSONServer(host, int(params.tcp_port), in_queue)
tcp_server.start()
if __name__ == "__main__":
main(parse_args(sys.argv[1:]))
# EOF
当我运行这个命令时:
python netgrafio1.py -h
我收到此错误:
Traceback (most recent call last):
File "netgrafio1.py", line 44, in <module>
from lib.WebSocketServer import WebSocketServer
File "/home/ubuntu/netgrafio/lib/WebSocketServer.py", line 54, in <module>
from queue import Queue
当我运行这个命令时:
python3 netgrafio1.py
我收到此错误:
Traceback (most recent call last):
File "netgrafio1.py", line 44, in <module>
from lib.WebSocketServer import WebSocketServer
File "/home/ubuntu/netgrafio/lib/WebSocketServer.py", line 46, in <module>
import tornado.web
ImportError: No module named 'tornado'
请注意,我确实安装了龙卷风:
pip list | grep tor
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
tornado 5.1.1
我正在尝试让 netgrafio 运行,以便得到以下信息:
$ python netgrafio.py -h
usage: netgrafio.py [-h] [--tcp-port TCP_PORT] [--ws-port WS_PORT]
[--web-port WEB_PORT] [--host HOST]
netgrafio - visualize your network
optional arguments:
-h, --help show this help message and exit
--tcp-port TCP_PORT Specify TCP port to listen for JSON packets (default:
8081)
--ws-port WS_PORT Specify WebSocket port to send JSON data to (default:
8080)
--web-port WEB_PORT Specify web port to server web application (default:
5000)
--host HOST Specify host to bind socket on (default: 127.0.0.1)
解决方案
当您使用python ...
命令时,您的操作系统可能正在运行 Python 2.7。在 Python 2.7 中,没有queue
模块。在 v2.7 中,它被称为Queue
. 它queue
在 v3 中被重命名为。
当您运行时python3 ...
,您的操作系统正在运行 Python 3.x。即使这个版本有queue
模块,但你还没有安装 Tornado for Python 3。
要解决此问题,请为 Python 3 安装 Tornado:
pip3 install tornado
python3 ...
然后使用命令运行您的脚本。
更新与新错误有关。
queue
不是可调用对象;它是一个模块(即一个python文件)。请参阅该queue
模块的文档。您需要queue.Queue
在代码中使用。
in_queue = queue.Queue()
推荐阅读
- javascript - 试图创建一个有分数和时间的排行榜。本地存储?
- java - FindBugs 插件 Eclipse 发现的错误
- angular - 如何在不从根 URL 开始的情况下使角度链接工作?
- angular - Angular SSR 无需在点击链接上重新加载页面
- docker - LetsEncrypt 的 TLS 证书应该在所有容器上吗?
- sparql - 如何在 Sparql 中的多个列上选择不同的?
- reactjs - 如何使用 useReducer 和 useEffect 从 API 获取,同时仍然允许重新查询
- scala - 带有HTTPS的Scalatra?
- oracle - oracle drop table if exists pl/sql not working using exception
- javascript - Google Maps Places Javascript API