Python 导入错误:没有名为“线程”的模块

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

ImportError: No module named 'thread'

pythonmitmproxy

提问by

when I run mitmproxycommand in command line, I get the following error.

当我在命令行中运行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'

I googled this error and found this stackoverflow Q&A page.

我在谷歌上搜索了这个错误并找到了这个 stackoverflow Q&A 页面。

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

pydev importerror: 没有名为线程的模块,pydev 升级后调试不再有效

according to the page above, the error occurs because module "thread" is renamed to "_thread" in python3.

根据上面的页面,发生错误是因为模块thread_thread”在python3中被重命名为“ ”。

So, I know what's causing this error, but then what?

所以,我知道是什么导致了这个错误,但是呢?

I don't know what to do now in order to get rid of this error.

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

I'm new to python. I've just installed Python and pipinto my mac OSX as shown below because I want to use mitmproxy.

我是python的新手。我刚刚安装了 Python 并安装pip到我的 mac OSX 中,如下所示,因为我想使用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

could anyone please tell me what to do now?

谁能告诉我现在该怎么办?

Additional Info

附加信息

As @linusg answered, I created "thread.py" file in "site-packages" directory and pasted the code below in "thread.py"

正如@linusg 回答的那样,我thread.py在“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")

After I did this, "ImportError: No module named 'thread'" disappeared, but now I have another ImportError, which is "import Cookie ImportError: No module named 'Cookie'".

执行此操作后,“ ImportError: No module named 'thread'”消失了,但现在我有另一个导入错误,即“ import Cookie ImportError: No module named 'Cookie'”。

It seems that in Python 3, Cookiemodule is renamed to http.cookies(stackoverflow.com/questions/3522029/django-mod-python-error).

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

Now what am I supposed to do?

现在我该怎么办?

What I have in "site-packages" directory

我在“站点包”目录中有什么

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

采纳答案by linusg

Go to you site-packagesfolder, create a file called thread.pyand paste this code in it:

转到您的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")

This creates an 'alias' for the module _threadcalled thread. While the _threadmodule is very small, you can use dir()for bigger modules:

这将为_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))

Hope this helps!

希望这可以帮助!

回答by Maximilian Hils

You are trying to run Python 2 code on Python 3, which will not work.

您正在尝试在 Python 3 上运行 Python 2 代码,这将不起作用。

As of April 2016, mitmproxy only supports Python 2.7. We're actively working to fix that in the next months, but for now you need to use Python 2 or the binaries provided at http://mitmproxy.org.

截至 2016 年 4 月,mitmproxy 仅支持 Python 2.7。我们正在积极努力在接下来的几个月中解决这个问题,但现在您需要使用 Python 2 或http://mitmproxy.org 上提供的二进制文件。

As of August 2016, the development version of mitmproxy now supports Python 3.5+. The next release (0.18) will be the first one including support for Python 3.5+.

截至 2016 年 8 月,mitmproxy 的开发版本现已支持 Python 3.5+。下一个版本 (0.18) 将是第一个包括对 Python 3.5+ 支持的版本。

As of January 2017, mitmproxy only supports Python 3.5+.

截至 2017 年 1 月,mitmproxy 仅支持 Python 3.5+。

回答by Cemal Eker

Easiest solution is to create a virtualenv with python2 and run mitmproxy on this virtualenv

最简单的解决方案是用 python2 创建一个 virtualenv 并在这个 virtualenv 上运行 mitmproxy

virtualenv -p `which python2` .env
source .env/bin/activate
pip install mitmproxy
.env/bin/mitmproxy

回答by Aayush

The name of the file saved could be threading, this would give an error as threading is a predefined class in Python. Try changing the name of your file. It would help....

保存的文件的名称可能是线程,这会产生错误,因为线程是 Python 中的预定义类。尝试更改文件的名称。它会有所帮助....