ImportError: No module named 'thread' - python

when I run mitmproxy command in command line, I get the following error.
% 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.
pydev importerror: no module named thread, debugging no longer works after pydev upgrade
according to the page above, the error occurs because module "thread" is renamed to "_thread" in 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 pip into my mac OSX as shown below because I want to use 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"
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'".
It seems that in Python 3, Cookie module is renamed to 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/

In Python 3 instead of:
import thread
Do:
import _thread

You are trying to run Python 2 code on Python 3, which will not work.
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.
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+.
As of January 2017, mitmproxy only supports Python 3.5+.

Go to you site-packages folder, create a file called thread.py and paste this code in it:
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 _thread called thread. While the _thread module is very small, you can use dir() for bigger modules:
# 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!

Easiest solution is to create a virtualenv with python2 and run mitmproxy on this virtualenv
virtualenv -p `which python2` .env
source .env/bin/activate
pip install mitmproxy
.env/bin/mitmproxy

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....

Related

i tried to install pyinstaller with pip install pyinstaller and got this: [duplicate]

I just installed Python 3.6.1 for MacOS X
When I attempt to run the Console(or run anything with Python3), this error is thrown:
AttributeError: module 'enum' has no attribute 'IntFlag'
$ /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
Failed to import the site module
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 544, in <module>
main()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 530, in main
known_paths = addusersitepackages(known_paths)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 282, in addusersitepackages
user_site = getusersitepackages()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 258, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 248, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sysconfig.py", line 601, in get_config_var
return get_config_vars().get(name)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sysconfig.py", line 580, in get_config_vars
import _osx_support
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_osx_support.py", line 4, in <module>
import re
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 142, in <module>
class RegexFlag(enum.IntFlag):
AttributeError: module 'enum' has no attribute 'IntFlag'
The class IntFlag exists within enum.py. So, why is the AttributeError being thrown?
It's because your enum is not the standard library enum module. You probably have the package enum34 installed.
One way check if this is the case is to inspect the property enum.__file__
import enum
print(enum.__file__)
# standard library location should be something like
# /usr/local/lib/python3.6/enum.py
Since python 3.6 the enum34 library is no longer compatible with the standard library. The library is also unnecessary, so you can simply uninstall it.
pip uninstall -y enum34
If you need the code to run on python versions both <=3.4 and >3.4, you can try having enum-compat as a requirement. It only installs enum34 for older versions of python without the standard library enum.
Not sure whether you still have this issue. I had a similar issue and I was able to resolve it simply by unsetting PYTHONPATH
$ unset PYTHONPATH
For me this error occured after installing of gcloud component app-engine-python in order to integrate into pycharm. Uninstalling the module helped, even if pycharm is now not uploading to app-engine.
If anyone coming here because of getting this error while running a google app engine Python 3.7 standard environment project in PyCharm then all you need to do is
Make sure the configuration to run is for Flask, not Google App Engine configuration.
Then disable Google App Engine support under Preferences >> Languages & Framework >> Google App Engine
The reason being as per this link
The overall goal is that your app should be fully portable and run in any standard Python environment. You write a standard Python app, not an App Engine Python app. As part of this shift, you are no longer required to use proprietary App Engine APIs and services for your app's core functionality. At this time, App Engine APIs are not available in the Python 3.7 runtime.
I guess when we create a python 3.7 project in PyCharm as a Google app engine project it still tries to do the same way it does for a python2.7 app
I got this issue while installing transformers library from HuggingFace. It was due to the fact package enum34 was installed in my environment which was overriding built-in enum in Python. This package was probably installed by something as for forward compatibility which is no longer needed with Python 3.6+. So the solution is simply,
pip uninstall -y enum34
DISCLAIMER: Please, #juanpa.arrivillaga, if you see this answer, feel free to write your own and I will remove this post.
#juanpa.arrivillaga had mentioned above:
Is there a file name enum.py in your working directory, by any chance?
This was the issue I encountered. I was not aware of the enum module on python at the time and had named my test file enum.py.
Since the file name is the module name, there was a conflict. More info on modules here: https://docs.python.org/2/tutorial/modules.html
Installing version 1.1.8 of enum34 worked for me.
I was able to fix this by adding enum34 = "==1.1.8" to pyproject.toml.
Apparently enum34 had a feature in v1.1.8 that avoided this error, but
this regressed in v1.1.9+. This is just a workaround though. The
better solution would be for packages to use environment markers so
you don't have to install enum34 at all unless needed.
Source: https://github.com/python-poetry/poetry/issues/1122
Disabling "Google App Engine Support" in pycharm preferences fixed this issue for me.
Håken Lid's answer helped solved my problem (thanks!) , in my case present in Python3.7 running Flask in a Docker container (FROM tiangolo/uwsgi-nginx-flask:python3.7-alpine3.7).
In my case, enum34 was being installed by another library (pip install smartsheet-python-sdk).
For those coming with a similar Docker container problem, here is my final Dockerfile (stripped to the relevant lines):
FROM tiangolo/uwsgi-nginx-flask:python3.7-alpine3.7
...
RUN pip install -r requirements.txt
RUN pip uninstall -y enum34
...
In case you have to keep PYTHONPATH for both python2 and python3, you can write alias statements to set the proper PYTHONPATH in your bash_profile:
Hardcode your PYTHONPATH2, and PYTHONPATH3 variables in your ~/.bash_profile, and add the following aliases at the end of it:
alias python='export PYTHONPATH=${PYTHONPATH2};python'
alias python3='export PYTHONPATH=${PYTHONPATH3};python3'
My python (refers to python2) as I use python2 more often.
I have Python 2 and Python 3 installed on my computer. For some strange reason I have in the sys.path of Python 3 also a path to the sitepackage library directory of Python2 when the re module is called. If I run Python 3 and import enum and print(enum.__file__) the system does not show this Python 2 path to site-packages. So a very rough and dirty hack is, to directly modify the module in which enum is imported (follow the traceback paths) and insert the following code just before importing enum:
import sys
for i, p in enumerate(sys.path):
if "python27" in p.lower() or "python2.7" in p.lower(): sys.path.pop(i)
import enum
That solved my problem.
When ever I got this problem:
AttributeError: module 'enum' has no attribute 'IntFlag'
simply first i run the command:
unset PYTHONPATH
and then run my desired command then got success in that.
I did by using pip install <required-library> --ignore-installed enum34
Once your required library is installed, look for warnings during the build.
I got an Error like this:
Using legacy setup.py install for future, since package 'wheel' is not installed
ERROR: pyejabberd 0.2.11 has requirement enum34==1.1.2, but you'll have enum34 1.1.10 which is incompatible.
To fix this issue now run the command: pip freeze | grep enum34. This will give you the version of the installed enum34. Now uninstall it by pip uninstall enum34 and reinstall the required version as pip install "enum34==1.1.20"
If you having this issue when running tests in PyCharm, make sure the second box is unchecked in the configurations.
I had this problem in ubuntu20.04 in jupyterlab in my virtual env kernel with python3.8 and tensorflow 2.2.0. Error message was
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/hu-mka/.local/lib/python2.7/site-packages/ipykernel_launcher.py", line 15, in <module>
from ipykernel import kernelapp as app
File "/home/hu-mka/.local/lib/python2.7/site-packages/ipykernel/__init__.py", line 2, in <module>
from .connect import *
File "/home/hu-mka/.local/lib/python2.7/site-packages/ipykernel/connect.py", line 13, in <module>
from IPython.core.profiledir import ProfileDir
File "/home/hu-mka/.local/lib/python2.7/site-packages/IPython/__init__.py", line 48, in <module>
from .core.application import Application
File "/home/hu-mka/.local/lib/python2.7/site-packages/IPython/core/application.py", line 23, in <module>
from traitlets.config.application import Application, catch_config_error
File "/home/hu-mka/.local/lib/python2.7/site-packages/traitlets/__init__.py", line 1, in <module>
from .traitlets import *
File "/home/hu-mka/.local/lib/python2.7/site-packages/traitlets/traitlets.py", line 49, in <module>
import enum
ImportError: No module named enum
problem was that in symbolic link in /usr/bin/python was pointing to python2. Solution:
cd /usr/bin/
sudo ln -sf python3 python
Hopefully Python 2 usage will drop off completely soon.
Even I had this issue while running python -m grpc_tools.protoc --version
Had to set the PYTHONPATH till site-packages and shutdown all the command prompt windows and it worked. Hope it helps for gRPC users.
Unfortunately none of the suggestions helped me, but after some more googling this
pip install aenum
solved it for me
I faced the same issue, but I couldn't change the environment the script is run inside. So the fixes here were not applicable for me.
What I did instead was
import os
import sys
os.environ["PYTHONPATH"] = os.pathsep.join(
(p for p in os.environ["PYTHONPATH"].split(os.pathsep) if "python2" not in p)
)
sys.path = [p for p in sys.path if "python2" not in p]
# Now that the paths are clean, we can import re
import re
2022 UPDATE
If you are using a modern version of Python (in my case 3.9.x), you don't need "enum34" at all, as this library is described as "Python 3.4 Enum backported to 3.3, 3.2, 3.1, 2.7, 2.6, 2.5, and 2.4"
So in my case, the solution was to get rid of it entirely:
pip uninstall enum34
Now Pycharm runs my app perfectly.
My problem was that I marked a directory of mine as a Test Sources Root in pycharm which caused the python ast package to look for enum within <my_test_src_root_dir>/__init__.py (which was empty). I found this by clicking on the line that threw and then controlling (mac CMD-B) into the enum package and seeing that it went to the __init__.py file of the marked directory.
The file that threw:
"/opt/homebrew/Cellar/python#3.10/3.10.2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py"
The line that threw:
from enum import IntEnum, auto
The error:
ImportError: cannot import name 'IntEnum' from 'enum' (.../path/to/_init_.py)
If anyone is having this problem when trying to run Jupyter kernel from a virtualenv, just add correct PYTHONPATH to kernel.json of your virtualenv kernel (Python 3 in example):
{
"argv": [
"/usr/local/Cellar/python/3.6.5/bin/python3.6",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3 (TensorFlow)",
"language": "python",
"env": {
"PYTHONPATH": "/Users/dimitrijer/git/mlai/.venv/lib/python3.6:/Users/dimitrijer/git/mlai/.venv/lib/python3.6/lib-dynload:/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6:/Users/dimitrijer/git/mlai/.venv/lib/python3.6/site-packages"
}
}
ensure there is no file or folder named enum in your codebase

Cannot install custom python module

I am having trouble installing a custom python module I have written.
Here are my steps so far:
Navigate to the module directory C:\Users\myname\repos\mymodulename where the setup.py file is in the anaconda prompt. Type: python setup.py install
The command prompt returns (plus some other things)
Extracting mymodulename-0.1-py3.7.egg to c:\users\myname\appdata\local\continuum\anaconda3\lib\site-packages
mymodulename 0.1 is already the active version in easy-install.pth
Installing myclass-script.py script to C:\Users\myname\AppData\Local\Continuum\anaconda3\Scripts
Installing myclass.exe script to C:\Users\myname\AppData\Local\Continuum\anaconda3\Scripts
Installed c:\users\myname\appdata\local\continuum\anaconda3\lib\site-packages\mymodulename-0.1-py3.7.egg
Processing dependencies for mymodulename==0.1
...
Using c:\users\myname\appdata\local\continuum\anaconda3\lib\site-packages
Finished processing dependencies for mymodulename==0.1
To me that looks like it has installed. Opening up the console and trying to import:
>>> import mymodulename.myclassas ce
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'mymodulename'
>>>
It appears in hasn't. Checking the list of modules in anaconda with help('modules') confirms that it has not been imported.
I thought that perhaps i had installed it to the wrong environment:
(base) C:\Users\myname>conda env list
# conda environments:
#
base * C:\Users\myname\AppData\Local\Continuum\anaconda3
py2 C:\Users\myname\AppData\Local\Continuum\anaconda3\envs\py2
Only a python 2 environment which mymodule wouldn't be compatible with.
Does anyone have any suggestions about what I can try to resolve this? Happy to elaborate on any of the points
Thanks in advance.
EDIT: Some more information that may be relevant.
This package was initially installed in site-packages. I have reinstalled there and the package works. The reason I moved the package is because I am aware it is bad practice to store custom packages there.

Instaling airflow on python virtual environment [duplicate]

I just installed Python 3.6.1 for MacOS X
When I attempt to run the Console(or run anything with Python3), this error is thrown:
AttributeError: module 'enum' has no attribute 'IntFlag'
$ /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
Failed to import the site module
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 544, in <module>
main()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 530, in main
known_paths = addusersitepackages(known_paths)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 282, in addusersitepackages
user_site = getusersitepackages()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 258, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 248, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sysconfig.py", line 601, in get_config_var
return get_config_vars().get(name)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sysconfig.py", line 580, in get_config_vars
import _osx_support
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_osx_support.py", line 4, in <module>
import re
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 142, in <module>
class RegexFlag(enum.IntFlag):
AttributeError: module 'enum' has no attribute 'IntFlag'
The class IntFlag exists within enum.py. So, why is the AttributeError being thrown?
It's because your enum is not the standard library enum module. You probably have the package enum34 installed.
One way check if this is the case is to inspect the property enum.__file__
import enum
print(enum.__file__)
# standard library location should be something like
# /usr/local/lib/python3.6/enum.py
Since python 3.6 the enum34 library is no longer compatible with the standard library. The library is also unnecessary, so you can simply uninstall it.
pip uninstall -y enum34
If you need the code to run on python versions both <=3.4 and >3.4, you can try having enum-compat as a requirement. It only installs enum34 for older versions of python without the standard library enum.
Not sure whether you still have this issue. I had a similar issue and I was able to resolve it simply by unsetting PYTHONPATH
$ unset PYTHONPATH
For me this error occured after installing of gcloud component app-engine-python in order to integrate into pycharm. Uninstalling the module helped, even if pycharm is now not uploading to app-engine.
If anyone coming here because of getting this error while running a google app engine Python 3.7 standard environment project in PyCharm then all you need to do is
Make sure the configuration to run is for Flask, not Google App Engine configuration.
Then disable Google App Engine support under Preferences >> Languages & Framework >> Google App Engine
The reason being as per this link
The overall goal is that your app should be fully portable and run in any standard Python environment. You write a standard Python app, not an App Engine Python app. As part of this shift, you are no longer required to use proprietary App Engine APIs and services for your app's core functionality. At this time, App Engine APIs are not available in the Python 3.7 runtime.
I guess when we create a python 3.7 project in PyCharm as a Google app engine project it still tries to do the same way it does for a python2.7 app
I got this issue while installing transformers library from HuggingFace. It was due to the fact package enum34 was installed in my environment which was overriding built-in enum in Python. This package was probably installed by something as for forward compatibility which is no longer needed with Python 3.6+. So the solution is simply,
pip uninstall -y enum34
DISCLAIMER: Please, #juanpa.arrivillaga, if you see this answer, feel free to write your own and I will remove this post.
#juanpa.arrivillaga had mentioned above:
Is there a file name enum.py in your working directory, by any chance?
This was the issue I encountered. I was not aware of the enum module on python at the time and had named my test file enum.py.
Since the file name is the module name, there was a conflict. More info on modules here: https://docs.python.org/2/tutorial/modules.html
Installing version 1.1.8 of enum34 worked for me.
I was able to fix this by adding enum34 = "==1.1.8" to pyproject.toml.
Apparently enum34 had a feature in v1.1.8 that avoided this error, but
this regressed in v1.1.9+. This is just a workaround though. The
better solution would be for packages to use environment markers so
you don't have to install enum34 at all unless needed.
Source: https://github.com/python-poetry/poetry/issues/1122
Disabling "Google App Engine Support" in pycharm preferences fixed this issue for me.
Håken Lid's answer helped solved my problem (thanks!) , in my case present in Python3.7 running Flask in a Docker container (FROM tiangolo/uwsgi-nginx-flask:python3.7-alpine3.7).
In my case, enum34 was being installed by another library (pip install smartsheet-python-sdk).
For those coming with a similar Docker container problem, here is my final Dockerfile (stripped to the relevant lines):
FROM tiangolo/uwsgi-nginx-flask:python3.7-alpine3.7
...
RUN pip install -r requirements.txt
RUN pip uninstall -y enum34
...
In case you have to keep PYTHONPATH for both python2 and python3, you can write alias statements to set the proper PYTHONPATH in your bash_profile:
Hardcode your PYTHONPATH2, and PYTHONPATH3 variables in your ~/.bash_profile, and add the following aliases at the end of it:
alias python='export PYTHONPATH=${PYTHONPATH2};python'
alias python3='export PYTHONPATH=${PYTHONPATH3};python3'
My python (refers to python2) as I use python2 more often.
I have Python 2 and Python 3 installed on my computer. For some strange reason I have in the sys.path of Python 3 also a path to the sitepackage library directory of Python2 when the re module is called. If I run Python 3 and import enum and print(enum.__file__) the system does not show this Python 2 path to site-packages. So a very rough and dirty hack is, to directly modify the module in which enum is imported (follow the traceback paths) and insert the following code just before importing enum:
import sys
for i, p in enumerate(sys.path):
if "python27" in p.lower() or "python2.7" in p.lower(): sys.path.pop(i)
import enum
That solved my problem.
When ever I got this problem:
AttributeError: module 'enum' has no attribute 'IntFlag'
simply first i run the command:
unset PYTHONPATH
and then run my desired command then got success in that.
I did by using pip install <required-library> --ignore-installed enum34
Once your required library is installed, look for warnings during the build.
I got an Error like this:
Using legacy setup.py install for future, since package 'wheel' is not installed
ERROR: pyejabberd 0.2.11 has requirement enum34==1.1.2, but you'll have enum34 1.1.10 which is incompatible.
To fix this issue now run the command: pip freeze | grep enum34. This will give you the version of the installed enum34. Now uninstall it by pip uninstall enum34 and reinstall the required version as pip install "enum34==1.1.20"
If you having this issue when running tests in PyCharm, make sure the second box is unchecked in the configurations.
I had this problem in ubuntu20.04 in jupyterlab in my virtual env kernel with python3.8 and tensorflow 2.2.0. Error message was
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/hu-mka/.local/lib/python2.7/site-packages/ipykernel_launcher.py", line 15, in <module>
from ipykernel import kernelapp as app
File "/home/hu-mka/.local/lib/python2.7/site-packages/ipykernel/__init__.py", line 2, in <module>
from .connect import *
File "/home/hu-mka/.local/lib/python2.7/site-packages/ipykernel/connect.py", line 13, in <module>
from IPython.core.profiledir import ProfileDir
File "/home/hu-mka/.local/lib/python2.7/site-packages/IPython/__init__.py", line 48, in <module>
from .core.application import Application
File "/home/hu-mka/.local/lib/python2.7/site-packages/IPython/core/application.py", line 23, in <module>
from traitlets.config.application import Application, catch_config_error
File "/home/hu-mka/.local/lib/python2.7/site-packages/traitlets/__init__.py", line 1, in <module>
from .traitlets import *
File "/home/hu-mka/.local/lib/python2.7/site-packages/traitlets/traitlets.py", line 49, in <module>
import enum
ImportError: No module named enum
problem was that in symbolic link in /usr/bin/python was pointing to python2. Solution:
cd /usr/bin/
sudo ln -sf python3 python
Hopefully Python 2 usage will drop off completely soon.
Even I had this issue while running python -m grpc_tools.protoc --version
Had to set the PYTHONPATH till site-packages and shutdown all the command prompt windows and it worked. Hope it helps for gRPC users.
Unfortunately none of the suggestions helped me, but after some more googling this
pip install aenum
solved it for me
I faced the same issue, but I couldn't change the environment the script is run inside. So the fixes here were not applicable for me.
What I did instead was
import os
import sys
os.environ["PYTHONPATH"] = os.pathsep.join(
(p for p in os.environ["PYTHONPATH"].split(os.pathsep) if "python2" not in p)
)
sys.path = [p for p in sys.path if "python2" not in p]
# Now that the paths are clean, we can import re
import re
2022 UPDATE
If you are using a modern version of Python (in my case 3.9.x), you don't need "enum34" at all, as this library is described as "Python 3.4 Enum backported to 3.3, 3.2, 3.1, 2.7, 2.6, 2.5, and 2.4"
So in my case, the solution was to get rid of it entirely:
pip uninstall enum34
Now Pycharm runs my app perfectly.
My problem was that I marked a directory of mine as a Test Sources Root in pycharm which caused the python ast package to look for enum within <my_test_src_root_dir>/__init__.py (which was empty). I found this by clicking on the line that threw and then controlling (mac CMD-B) into the enum package and seeing that it went to the __init__.py file of the marked directory.
The file that threw:
"/opt/homebrew/Cellar/python#3.10/3.10.2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py"
The line that threw:
from enum import IntEnum, auto
The error:
ImportError: cannot import name 'IntEnum' from 'enum' (.../path/to/_init_.py)
If anyone is having this problem when trying to run Jupyter kernel from a virtualenv, just add correct PYTHONPATH to kernel.json of your virtualenv kernel (Python 3 in example):
{
"argv": [
"/usr/local/Cellar/python/3.6.5/bin/python3.6",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3 (TensorFlow)",
"language": "python",
"env": {
"PYTHONPATH": "/Users/dimitrijer/git/mlai/.venv/lib/python3.6:/Users/dimitrijer/git/mlai/.venv/lib/python3.6/lib-dynload:/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6:/Users/dimitrijer/git/mlai/.venv/lib/python3.6/site-packages"
}
}
ensure there is no file or folder named enum in your codebase

Error with TensorFlow MNIST [duplicate]

Since updating from Homebrew Python 2.7.11 (from 2.7.10) I'm suddenly unable to test register my package on PyPi from the PyCharm IDE console.
Running (as an "External Tool")
python -B setup.py register -r pypitest
I now get
Traceback (most recent call last):
File "setup.py", line 22, in <module>
from setuptools import setup
File "/usr/local/lib/python2.7/site-packages/setuptools/__init__.py", line 12, in <module>
from setuptools.extension import Extension
File "/usr/local/lib/python2.7/site-packages/setuptools/extension.py", line 8, in <module>
from .dist import _get_unpatched
File "/usr/local/lib/python2.7/site-packages/setuptools/dist.py", line 16, in <module>
from setuptools.depends import Require
File "/usr/local/lib/python2.7/site-packages/setuptools/depends.py", line 6, in <module>
from setuptools import compat
File "/usr/local/lib/python2.7/site-packages/setuptools/compat.py", line 17, in <module>
import httplib
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 80, in <module>
import mimetools
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/mimetools.py", line 6, in <module>
import tempfile
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 32, in <module>
import io as _io
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/io.py", line 51, in <module>
import _io
ImportError: dlopen(/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyCodecInfo_GetIncrementalDecoder
Referenced from: /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Expected in: flat namespace
in /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Process finished with exit code 1
I'm not sure how to proceed. I only get this issue if I execute from within my IDE's console. If I do it directly at the system command line (Terminal on OS X) I have no problems.
OS X 10.11.3; Homebrew Python 2.7.11; PyCharm 5.0.3
tl;dr: Fix this issue by doing one of the following:
type hash -r python, OR
log out and log in.
EDIT: An answer to my related question makes it clear what's happening here. When you install a new version of python, you may need to run hash -r python to tell bash to reset the "cached" location to the python executable.
In my case, I was typing python, which was on my $PATH at /usr/local/bin/python. But bash was still using the old cache location /usr/bin/python. So, the old executable was called, but the new path was provided to python in sys.argv[0]. This means that the old executable was running, but the new sys.executable value caused all the wrong modules to get loaded (including the io module).
I'm having the same problem. I installed python 2.7.11 via an installer from Python.org. Strangely, the issue seems to be related to some subtle difference between how OSX launches python when I invoke it from the shell using the full path vs. using just the word python.
So, for me, this works (invoking python via the full path /usr/local/bin/python):
$ which python
/usr/local/bin/python
$ /usr/local/bin/python -c "import io"
$
... but this doesn't:
$ python -c "import io"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/io.py", line 51, in <module>
import _io
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyCodecInfo_GetIncrementalDecoder
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Expected in: flat namespace
in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
So, as a workaround, you can try doing the same thing.
Elsewhere, I've posted a separate question about this puzzling behavior. Maybe somehow merely calling python invokes some strange mix of the 2.7.11 executable with the 2.7.10 dylibs??
According to https://github.com/klen/python-mode/issues/634:
I had the same issue, but successfully fixed. In my case I compiled
python and vim with homebrew, when PYTHON_PATH has been specified and
set to one of my dev environments, where I also had some libraries,
including io. Workaround was simple: open new terminal, make sure that
you do not have custom PYTHON_PATH, uninstall python, uninstall vim.
Reinstall both of them.
and
Problem solved.
Culprit is the update from python 2.7.10 to 2.7.11.
If you are using conda package control, simply run "conda install
python=2.7.10" will solve this problem.
This doesn't give the root cause though. Since this happens with _io, this looks like a bug in python 2.7.11 (unlikely, there would be a world-scale outcry and a prompt fix if it was) or some packaging bug or version mismatch specifically with the homebrew version (and maybe some related ones, too).
Try to import _io in the console and if it succeeds, check if it was loaded from the same path.
Reinstall python.
brew unlink python && brew reinstall python
Secure the path
export PYTHONPATH=$PYTHONPATH:/usr/local/bin/
BACKUP and Change the order of "paths" file.
sudo nano /etc/paths
it seems, the order of paths, it is decisive to run python properly. In my case, the result was:
#sudo nano /etc/paths
/usr/bin
/usr/local/bin
/bin
/usr/sbin
/sbin
On my mac, path is like this.
$ which python
/usr/local/bin/python
Now I can run both:
$ /usr/local/bin/python -c "import io"
$ python -c "import io"
I had the same issue, it is successfully fixed by just replacing the _io.so file.
sudo find / -name _io.so
copy the path of the _io.so file which DOES NOT belong to python-2.7.11. For example, copy the path of _io.so which is under python-2.7.5:
/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Replace the /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so file with the _io.so that you just found.
This happened to me as well in MacVim. I solved it by making sure :python print(sys.path) is using system Python (e.g. /Library/Python/2.7/...)
Since I installed MacVim via Homebrew, I just did that by:
Spawn a new shell that had which python -> /usr/bin/python. For my case I needed to remove the pyenv line from my .bash_profile. If you installed Python via Homebrew you may want to brew unlink python first
brew reinstall macvim
If your problem is caused by anaconda, it is unnecessary to remove //anaconda directory.
Just open your ~/.bash_profile, find the line
export PATH="//anaconda/bin:$PATH
and comment it out, then restart your terminal session.
Another quick workaround if you don't mind sticking with Python 2.7.10 is to specify the path of the Python interpreter executable that will be used for the virtualenv. On OSX that path is usually /usr/bin/python:
virtualenv venv --python=/usr/bin/python
Can't add comment (?) so this just to share my exp., downgrade to 2.7.10 works fr me.
I got this error after a failed NLTK download, I needed to uninstall anaconda:
sudo rm -rf ~/anaconda
update PATH variable
This happened when I already had tried to create a venv in a folder, and mistakenly was trying to initialize a second one! So I just removed venv directory and re-ran the command. Very likely this is not the answer to this solution, but searching my error brought me here, so it may help some others who are stuck.
I solved this issue by removing the symbolic link that was in /usr/local/bin and copying the actual python binary, that was pointed to by said link, there.
I had the same issue when I tried to use PyCharm. Solved by setting "python interpreter" in project configuration to point to the python virtual env I wanted to use, which was an Anaconda env. Somehow the interpreter path was missing the "anaconda" portion of ~/.../anaconda/.../_io.so. No need to uninstall anaconda.

Cant Find HashLib Module After Server Update

I run Python Scripts on our Dreamhost Server. Our Python scripts use Python 2.7 - we made a custom installation because Dreamhost uses Python 2.6. Everything worked fine for 1 year.
Dreamhost did a server update yesturday and now our scripts fail to find a specific module - MD5. The scripts output the error below when we go to import hashlib.
What do I need to do to rectify this?
Should I reinstall Python 2.7?
Should I reinstall Pip and Easy_Install?
Should I reinstall VirtualEnv?
Is there something else you recommend I do?
Error from all Python scripts:
/home/user/script.py in ()
import hashlib
hashlib undefined
/home/user/python/lib/python2.7/hashlib.py in ()
# version not supporting that algorithm.
try:
globals()[__func_name] = __get_hash(__func_name)
except ValueError:
import logging builtin globals = <built-in function globals, __func_name = 'md5', __get_hash = <function __get_builtin_constructor /home/user/python/lib/python2.7/hashlib.py in __get_builtin_constructor(name='md5')
return _sha.new
elif name in ('MD5', 'md5'):
import _md5
return _md5.new
elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
_md5 undefined
<type 'exceptions.ImportError': No module named _md5
args = ('No module named _md5',)
message = 'No module named _md5'
I was having the exact same issue. I run Python 2.7 in my own virtualenv.
I am trying to avoid reinstalling python and run a Django 1.7 application.
The following approach works for me.
STEP 1. (This step might not be necessary)
I uninstalled pythonbrew since it says here: http://wiki.dreamhost.com/Python
that pythonbrew has been deprecated.
If you were doing this from scratch pyenv is way to go, but you don't need to reinstall
virtualenv, etc. Just get rid of pythonbrew to start with.
$ rm -Rf ~/.pythonbrew
Removed references in .bashrc to pythonbrew
STEP 2.
There is no need to re-install virtualenv. Just create a new virtual env
$~/env> virtualenv myNewEnvironment
$~/env/myNewEnvironment/bin> source activate
$ pip freeze
You have a clean slate now, start rebuilding dependencies from scratch.
At least is solves the "import hashlib" issue. This gives you a clean
version of python properly linked to the new Ubuntu OS.
(myNewEnvironment):~> which python
~/env/myNewEnvironment/bin/python
(myNewEnvironment):~> python
Python 2.7.3 (default, Feb 27 2014, 19:58:35)
[GCC 4.6.3] on linux2
installed on Ubuntu 12.04 (which is the new OS)
Verify:
import hashlib should not throw error
STEP 3.
pip install Django
pip install MySQL-python
Its also probably safer to complete/recheck the remaining steps listed out in
http://wiki.dreamhost.com/Django (or appropriate wiki page for your framework)
For now this allows me to get my site up and running, (but) there is a warning
that I am ignoring for now until I figure out more:
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
Good luck!

Categories

Resources