导入错误:找不到名为“thread”的模块。

24
在命令行中运行 mitmproxy 命令时,我收到以下错误。
% mitmproxy
Traceback (most recent call last):
  File "/usr/local/bin/mitmproxy", line 7, in <module>
    from libmproxy.main import mitmproxy
  File "/usr/local/lib/python3.5/site-packages/libmproxy/main.py", line 5, in <module>
    import thread
ImportError: No module named 'thread'

我在Google上搜索了这个错误,找到了这个stackoverflow的Q&A页面。

pydev importerror: no module named thread, debugging no longer works after pydev upgrade

根据上面的页面,这个错误是由于在Python3中将模块 "thread" 重命名为 "_thread" 所导致的。

所以,我知道是什么导致了这个错误,但接下来该怎么做呢?

我不知道现在应该怎么做才能摆脱这个错误。

我是Python的新手。我刚刚按照下面的方式在我的mac OSX上安装了Python和pip,因为我想使用mitmproxy

% which pip
/usr/local/bin/pip
% pip --version
pip 8.1.1 from /usr/local/lib/python3.5/site-packages (python 3.5)

% which python
/usr/bin/python
% which python3
/usr/local/bin/python3

% python --version
Python 2.7.10
% python3 --version
Python 3.5.1

有人能告诉我现在该怎么做吗?

额外信息

如@linusg所回答的,我在“site-packages”目录中创建了“thread.py”文件,并将下面的代码粘贴到“thread.py”中。

from _thread import *
__all__ = ("error", "LockType", "start_new_thread", "interrupt_main", "exit", "allocate_lock", "get_ident", "stack_size", "acquire", "release", "locked")

我做了这个之后,"ImportError: No module named 'thread'" 消失了,但现在我有另一个 ImportError,是 "import Cookie ImportError: No module named 'Cookie'"。

看起来在 Python 3 中,Cookie 模块被重命名为 http.cookies (stackoverflow.com/questions/3522029/django-mod-python-error)。

那么我现在该怎么办呢?

"site-packages" 目录中的内容

% ls /usr/local/lib/python3.5/site-packages                                                  (git)-[master]
ConfigArgParse-0.10.0.dist-info/                mitmproxy-0.15.dist-info/
OpenSSL/                                        netlib/
PIL/                                            netlib-0.15.1.dist-info/
Pillow-3.0.0.dist-info/                         passlib/
PyYAML-3.11.dist-info/                          passlib-1.6.5.dist-info/
__pycache__/                                    pathtools/
_cffi_backend.cpython-35m-darwin.so*            pathtools-0.1.2.dist-info/
_markerlib/                                     pip/
_watchdog_fsevents.cpython-35m-darwin.so*       pip-8.1.1.dist-info/
argh/                                           pkg_resources/
argh-0.26.1.dist-info/                          pyOpenSSL-0.15.1.dist-info/
backports/                                      pyasn1/
backports.ssl_match_hostname-3.5.0.1.dist-info/ pyasn1-0.1.9.dist-info/
blinker/                                        pycparser/
blinker-1.4.dist-info/                          pycparser-2.14.dist-info/
certifi/                                        pyparsing-2.0.7.dist-info/
certifi-2016.2.28.dist-info/                    pyparsing.py
cffi/                                           pyperclip/
cffi-1.6.0.dist-info/                           pyperclip-1.5.27.dist-info/
click/                                          setuptools/
click-6.2.dist-info/                            setuptools-19.4-py3.5.egg-info/
configargparse.py                               sitecustomize.py
construct/                                      six-1.10.0.dist-info/
construct-2.5.2.dist-info/                      six.py
cryptography/                                   test/
cryptography-1.1.2.dist-info/                   thread.py
easy_install.py                                 tornado/
hpack/                                          tornado-4.3.dist-info/
hpack-2.0.1.dist-info/                          urwid/
html2text/                                      urwid-1.3.1.dist-info/
html2text-2015.11.4.dist-info/                  watchdog/
idna/                                           watchdog-0.8.3.dist-info/
idna-2.1.dist-info/                             wheel/
libmproxy/                                      wheel-0.26.0-py3.5.egg-info/
lxml/                                           yaml/
lxml-3.4.4.dist-info/
5个回答

45

在Python 3中,不再使用:

import thread

做:

import _thread

8
您正在尝试在Python 3上运行Python 2代码,这是不可行的。
截至2016年4月,mitmproxy仅支持Python 2.7。我们正在积极努力解决这个问题,在未来几个月内会有所改进。但是目前您需要使用Python 2或在http://mitmproxy.org提供的二进制文件。
截至2016年8月,mitmproxy的开发版本现在支持Python 3.5+。下一个版本(0.18)将是第一个包括对Python 3.5+支持的版本。
截至2017年1月,mitmproxy仅支持Python 3.5+。

2
该解决方案错误地尝试“修复”Python 3安装以运行Python 2代码,这是行不通的。 OP的最初目标是能够运行mitmproxy,因此这回答了他如何运行mitmproxy的问题(这是人们搜索的内容)。 - Maximilian Hils

0
最简单的解决方案是创建一个带有Python2的虚拟环境,并在该虚拟环境中运行mitmproxy。
virtualenv -p `which python2` .env
source .env/bin/activate
pip install mitmproxy
.env/bin/mitmproxy

.env/bin/mitmproxy -bash:无此文件或目录 - IgorGanapolsky
你应该使用pip install mitmproxy在这个虚拟环境中安装mitmproxy。 - Cemal Eker
现在我遇到了一个错误:无法找到满足要求的版本mimtmproxy(来自版本:) 找不到匹配的发行版mimtmproxy - IgorGanapolsky
答案中有一个错别字,请使用正确的拼写再试一次。 :) - Maximilian Hils
@MaximilianHils 但是现在我遇到了这个错误:使用解释器 /Users/igorganapolsky/.env/bin/python 运行 virtualenv,请使用 系统 python 运行此脚本 - IgorGanapolsky
1
@IgorGanapolsky 在你遇到错误时,先deactivate虚拟环境,然后重新执行命令(我知道现在已经很晚了,这条评论是为了参考)。 - Eineki

0

前往你的 site-packages 文件夹,创建一个名为 thread.py 的文件,并将以下代码粘贴到其中:

from _thread import *
__all__ = ("error", "LockType", "start_new_thread", "interrupt_main", "exit", "allocate_lock", "get_ident", "stack_size", "acquire", "release", "locked")

这将为模块_thread创建一个名为thread的“别名”。虽然_thread模块非常小,但您可以在更大的模块中使用dir()

# Examle for the Cookies module which was renamed to http.cookies:
# Cookies.py in site-packages
import http.cookies
__all__ = tuple(dir(http.cookies))

希望这能帮到你!


现在我又遇到了另一个错误,如下所示。 File "/usr/local/lib/python3.5/site-packages/libmproxy/models/http.py", line 2, in <module> import Cookie ImportError: No module named 'Cookie' - user5809117
在Python3中,Cookie模块似乎已被重命名为http.cookies。(https://dev59.com/G0_Ta4cB1Zd3GeqPEuS8) - user5809117
1
请使用相同的方法处理 cookies 模块。 - linusg
1
如果这个答案对你的问题有帮助,请接受它。 - linusg
from http.cookies import * all = ("error", "LockType", "start_new_thread", "interrupt_main", "exit", "allocate_lock", "get_ident", "stack_size", "acquire", "release", "locked") - user5809117
显示剩余4条评论

-1
文件保存的名称可能是“threading”,这会导致错误,因为“threading”是Python中预定义的类。尝试更改文件名。这将有所帮助...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接