186

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?

sophros
  • 14,672
  • 11
  • 46
  • 75
BryanWheelock
  • 12,146
  • 18
  • 64
  • 109

22 Answers22

351

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.

Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
Håken Lid
  • 22,318
  • 9
  • 52
  • 67
  • This kills `ipython2 --> traitlets --> enum34`. (Unfortunately, `enum34` is installed in `.../2.7/site-packages/enum` not `.../2.7/site-packages/enum34` -- asking for trouble.) – denis Aug 31 '17 at 15:32
  • 1
    yes you're right -- it's just a note to anyone who tries to use both. – denis Sep 01 '17 at 14:03
  • This solved my problem, i'm using python 3.6.3 only, no other version – Wadaane Dec 06 '17 at 19:23
  • Why hasn't this answer been accepted? Did it solve the asker's problem? This worked for me! – aravk33 Mar 08 '18 at 14:41
  • 19
    In my case it gave me `Skipping enum34 as it is not installed`.. I am using 3.7 – Rotkiv Jul 13 '18 at 03:07
  • 1
    What if `print(enum.__file__)` gives me this: `/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/enum.py`. Does it mean I have a standard `enum ` library and not `enum34`? Edit: yeah, `pip uninstall enum34` gave me `Skipping enum34 as it is not installed.` – Andrey Jul 20 '18 at 12:22
  • Didn't help, sorry. – ibodi Aug 08 '18 at 17:09
  • 2
    @Rotkiv what is the solution. I'm also using 3.7 and facing same issue. – Krishna Aug 30 '18 at 10:25
  • when typing `print(enum.__file__)` I get back: `/Library/Python/2.7/site-packages/enum/__init__.py`, is this correct? – Cynthia GS Oct 19 '18 at 20:47
  • @HåkenLid yes, I get the same error: `Error in sitecustomize; set PYTHONVERBOSE for traceback: AttributeError: module 'enum' has no attribute 'IntFlag' Traceback (most recent call last): File "/usr/local/bin/jupyter", line 4, in import re File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/re.py", line 143, in class RegexFlag(enum.IntFlag): AttributeError: module 'enum' has no attribute 'IntFlag'` and uninstalling enum34 doesn't help, I get back that it isn't installed. – Cynthia GS Oct 19 '18 at 22:46
  • 2
    @HåkenLid This happens when I try to open a `jupyter notebook` by the way. – Cynthia GS Oct 20 '18 at 03:41
  • @Rotkiv I had it in my Python 2.7 which was not in my PATH (I have python 3.7) so therefore pip/poetry didn't find it. Uninstalling python 2.7 (uninstalling enum34 would certainly be sufficient) and restarted my computer solved it. – niklasolsn Jul 10 '20 at 08:49
  • `pip2.7 uninstall -y enum34` worked just fine for me. – alexopoulos7 Sep 16 '20 at 13:05
  • `print(enum.__file__)` returned `/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/enum.py` for me, but enum34 was indeed installed and was located in site packages of virtual environment, so `pip uninstall -y enum34` solved the problem – fdermishin Nov 01 '20 at 18:43
  • This worked for me when trying to install the "geocube" library – wfgeo Apr 14 '21 at 08:07
109

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

CodeWarrior
  • 2,721
  • 3
  • 18
  • 18
V. Singh
  • 1,307
  • 1
  • 9
  • 8
  • 4
    Just FYI for PyCharm users, to unset the PYTHONPATH I clicked on `Settings->Build, Execution, Deployment->Console->Python Console`. I then removed the PYTHONPATH variable from the field titled `Environment variables`. Note: at the time of this writing, I was using PyCharm 2017.1.2. – MikeyE Aug 09 '17 at 07:08
  • This works, but with no combination of paths that includes my Python 2 libraries can I get it to work. I'd love to know why. – Ulf Aslak Feb 06 '18 at 13:09
  • Yes, In windows, I restarted my cmd prompt, It worked, Maybe indirectly it reset PYTHONPATH – Karan Kaw Jul 15 '18 at 09:58
  • 2
    I come back to this once a week at least to look this up - does anyone have an explanation for *why* this works? – Owen Dec 12 '20 at 18:52
15

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.

Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
mickmackk
  • 191
  • 2
  • 4
  • 5
    I got this error running Flask under PyCharm, and fixed it by disabling AppEngine support from the project (Preferences -> Languages & Frameworks -> Google App Engine). The 2nd gen runtime for Python3 doesn't use the app engine dev server so this makes sense (it's for Python 2.x). – Brett Mar 27 '19 at 22:04
13

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

Joseph Abraham
  • 347
  • 4
  • 10
  • So, when writing for Google App Engine from Pycharm, must one manually upload their code to the cloud or will Pycharm still be able to upload to App Engine? – Ben Sep 29 '19 at 21:19
  • @Ben I am not sure what you mean by manually uploading code to the cloud(Google App Engine). If you are referring to deploying to App Engine its using the command `gcloud app deploy` . That has got nothing to do with pycharm. – Joseph Abraham Dec 04 '19 at 23:30
  • 1
    I am just an amateur at this but I thought that enabling App Engine support in Pycharm gave you the ability to use a menu command to deploy your code. But, when you disable app engine support in Pycharm (i.e. to use Python 3.7), you need to go to the command line to deploy your code. Do I have that right or did I misinterpret how the app engine stuff worked under 2.7? – Ben Dec 05 '19 at 13:52
  • @Ben That's because Pycharm app engine support tools are targeted for Python 2.7. As I have mentioned in my answer Google considers Python 3.7 Standard App engine apps as standard Python App. And I believe this is the reason why Pycharm too behaves this way for Python 3.7 app engine apps. – Joseph Abraham Dec 05 '19 at 23:30
13

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
Shital Shah
  • 63,284
  • 17
  • 238
  • 185
  • Solves the problem on my `Linux 5.9.11-3-MANJARO` with Python 3.8, while installing `curtsies`. – MKaama Feb 04 '21 at 11:32
11

@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

Spevacus
  • 584
  • 2
  • 13
  • 23
dannypaz
  • 1,294
  • 1
  • 12
  • 16
11

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

Joshua Wolff
  • 2,687
  • 1
  • 25
  • 42
8

Disabling "Google App Engine Support" in pycharm preferences fixed this issue for me.

pycharm app engine preferences

chmoder
  • 876
  • 10
  • 19
5

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
...
bandaangosta
  • 61
  • 1
  • 2
3

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.

0xdb
  • 3,539
  • 1
  • 21
  • 37
Gaurav Yadav
  • 57
  • 1
  • 4
2

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.

Håken Lid
  • 22,318
  • 9
  • 52
  • 67
bunkus
  • 975
  • 11
  • 19
2

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.

2

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"

Sidharth Rai
  • 119
  • 1
  • 1
2

If you having this issue when running tests in PyCharm, make sure the second box is unchecked in the configurations.

Yushan ZHANG
  • 523
  • 5
  • 18
  • This worked for me. I had to edit the configuration of the 'Play' test button in pycharm. When running tests with `pytest` from the command line, it worked regardless. Could you maybe add a sentence or two of what this setting does and how it causes the problem posted by OP? Thank you! – orpheus May 04 '22 at 15:51
1

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Markus Kaukonen
  • 334
  • 4
  • 10
  • **This solution can make horrible mess in your system!** You should use virtualenv instead of changing default python for the whole system. – Alex Baranowski Jul 21 '20 at 11:01
  • Thanks for pointing that out. I have not yet noticed any problems (but I always avoid python2 as much as possible). Python2 lovers shoud definately not do this. – Markus Kaukonen Jul 22 '20 at 08:08
1

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.

sscarduzio
  • 5,938
  • 5
  • 42
  • 54
0

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.

Ezio
  • 376
  • 5
  • 21
0

Unfortunately none of the suggestions helped me, but after some more googling this

pip install aenum

solved it for me

0

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
CodeMonkey
  • 4,067
  • 1
  • 31
  • 43
0

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)

orpheus
  • 1,060
  • 2
  • 15
  • 23
-2

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"
}
}
dimitrijer
  • 24
  • 1
  • 5
  • Your user directory is hardcoded here. This solution will not work for novices: please explain what it is doing and where to find the relevant files. – Tom Kelly Jun 11 '18 at 09:56
-2

ensure there is no file or folder named enum in your codebase

  • 2
    **This has already been mentioned in other answers ([this one, for example](https://stackoverflow.com/a/49082077/2227743)).** When answering older questions that already have answers, please make sure you provide either a novel solution or a significantly better explanation than existing answers. – Eric Aya Aug 29 '21 at 12:30