为什么 Python 3.6.1 抛出 AttributeError: module 'enum' has no attribute 'IntFlag'?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43124775/
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
Why Python 3.6.1 throws AttributeError: module 'enum' has no attribute 'IntFlag'?
提问by BryanWheelock
I just installed Python 3.6.1 for MacOS X
我刚刚为 MacOS X 安装了 Python 3.6.1
When I attempt to run the Console(or run anything with Python3), this error is thrown:
当我尝试运行控制台(或使用 Python3 运行任何东西)时,会抛出此错误:
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?
类 IntFlag 存在于 enum.py 中。那么,为什么会抛出 AttributeError 呢?
回答by H?ken Lid
It's because your enum
is not the standard library enum
module. You probably have the package enum34
installed.
这是因为您enum
的不是标准库enum
模块。您可能已经enum34
安装了该软件包。
One way check if this is the case is to inspect the property enum.__file__
检查是否是这种情况的一种方法是检查财产 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.
从 python 3.6 开始, enum34 库不再与标准库兼容。该库也是不必要的,因此您只需将其卸载即可。
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.
如果您需要代码在 <=3.4 和 >3.4 的 python 版本上运行,您可以enum-compat
根据需要尝试使用。它只安装enum34
在没有标准库枚举的旧版本 python 上。
回答by V. Singh
Not sure whether you still have this issue. I had a similar issue and I was able to resolve it simply by unsetting PYTHONPATH
不知道你是否还有这个问题。我有一个类似的问题,我可以通过取消设置来解决它PYTHONPATH
$ unset PYTHONPATH
$ unset PYTHONPATH
回答by mickmackk
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.
对我来说,在安装 gcloud 组件 app-engine-python 以集成到 pycharm 后发生此错误。卸载模块有帮助,即使 pycharm 现在没有上传到 app-engine。
回答by Joseph Abraham
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
如果有人因为在 PyCharm 中运行谷歌应用引擎 Python 3.7 标准环境项目时出现此错误而来到这里,那么您需要做的就是
- 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
- 确保要运行的配置是针对 Flask 的,而不是 Google App Engine 配置。
- 然后在 Preferences >> Languages & Framework >> Google App Engine 下禁用 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.
总体目标是您的应用程序应该是完全可移植的,并且可以在任何标准 Python 环境中运行。您编写的是标准 Python 应用程序,而不是 App Engine Python 应用程序。作为这一转变的一部分,您不再需要为应用的核心功能使用专有的 App Engine API 和服务。目前,App Engine API 在 Python 3.7 运行时中不可用。
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
我想当我们在 PyCharm 中创建一个 python 3.7 项目作为 Google 应用程序引擎项目时,它仍然尝试以与 python2.7 应用程序相同的方式执行
回答by dannypaz
DISCLAIMER:Please, @juanpa.arrivillaga, if you see this answer, feel free to write your own and I will remove this post.
免责声明:@juanpa.arrivillaga,如果你看到这个答案,请随意写下你自己的,我会删除这篇文章。
@juanpa.arrivillaga had mentioned above:
@juanpa.arrivillaga上面提到过:
Is there a file name enum.py in your working directory, by any chance?
您的工作目录中是否有文件名 enum.py ?
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
.
这是我遇到的问题。我当时不知道 python 上的 enum 模块,并且将我的测试文件命名为enum.py
.
Since the file name isthe module name, there was a conflict. More info on modules here: https://docs.python.org/2/tutorial/modules.html
由于文件名是模块名,所以存在冲突。关于模块的更多信息:https: //docs.python.org/2/tutorial/modules.html
回答by chmoder
Disabling "Google App Engine Support" in pycharm preferences fixed this issue for me.
在 pycharm 首选项中禁用“Google App Engine Support”为我解决了这个问题。
回答by bandaangosta
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
).
H?ken Lid 的回答帮助解决了我的问题(谢谢!),在我的情况下,Python3.7 在 Docker 容器中运行 Flask ( 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 myfinal Dockerfile (stripped to the relevant lines):
就我而言,enum34
是由另一个库 ( pip install smartsheet-python-sdk
)安装的。对于那些遇到类似 Docker 容器问题的人,这是我的最终 Dockerfile(删除到相关行):
FROM tiangolo/uwsgi-nginx-flask:python3.7-alpine3.7
...
RUN pip install -r requirements.txt
RUN pip uninstall -y enum34
...
回答by user3804602
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:
如果您必须为 python2 和 python3 保留 PYTHONPATH,您可以编写别名语句来在您的 bash_profile 中设置正确的 PYTHONPATH:
Hardcode your PYTHONPATH2, and PYTHONPATH3 variables in your ~/.bash_profile, and add the following aliases at the end of it:
在 ~/.bash_profile 中硬编码 PYTHONPATH2 和 PYTHONPATH3 变量,并在其末尾添加以下别名:
alias python='export PYTHONPATH=${PYTHONPATH2};python'
alias python3='export PYTHONPATH=${PYTHONPATH3};python3'
My python
(refers to python2) as I use python2 more often.
我的python
(指的是 python2),因为我更频繁地使用 python2。
回答by Gaurav
When ever I got this problem:
当我遇到这个问题时:
AttributeError: module 'enum' has no attribute 'IntFlag'
AttributeError: 模块“enum”没有属性“IntFlag”
simply first i run the command:
首先我运行命令:
unset PYTHONPATH
and then run my desired command then got success in that.
然后运行我想要的命令,然后就成功了。
回答by bunkus
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:
我的计算机上安装了 Python 2 和 Python 3。出于某种奇怪的原因,当调用 re 模块时,我在 Python 3 的 sys.path 中还有一个到 Python2 的 sitepackage 库目录的路径。如果我运行 Python 3 并导入 enum 并且print(enum.__file__)
系统不显示此 Python 2 站点包路径。所以一个非常粗糙和肮脏的黑客是,直接修改导入 enum 的模块(遵循回溯路径)并在导入 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.
那解决了我的问题。