MAC OS中的python3.6和枚举模块问题

时间:2018-05-18 04:44:39

标签: python python-3.x

我有2个python版本2.7和3.6。我将我的虚拟环境设置为在python3上运行

# virtualenv -p python3 venv
# source venv/bin/activate
# pip install -r requirements.txt

当我尝试安装模块时出现以下错误,

Collecting enum==0.4.6 (from -r requirements.txt (line 12))
  Using cached https://files.pythonhosted.org/packages/0c/4e/1ea357e7783c756bb579333c1e4a026fb331371ee771f616ffedc781e531/enum-0.4.6.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/Users/ratha/projects/test711/ATGWS/venv/lib/python3.6/site-packages/setuptools/__init__.py", line 5, in <module>
        import distutils.core
      File "/Users/ratha/projects/test711/ATGWS/venv/lib/python3.6/distutils/__init__.py", line 4, in <module>
        import imp
      File "/Users/ratha/projects/test711/ATGWS/venv/lib/python3.6/imp.py", line 27, in <module>
        import tokenize
      File "/Users/ratha/projects/test711/ATGWS/venv/lib/python3.6/tokenize.py", line 33, in <module>
        import re
      File "/Users/ratha/projects/test711/ATGWS/venv/lib/python3.6/re.py", line 142, in <module>
        class RegexFlag(enum.IntFlag):
    AttributeError: module 'enum' has no attribute 'IntFlag'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/ct/v0v5ht_n32n6c_33dw1td2fc0000gp/T/pip-install-36x_vm0b/enum/

在我的代码中,我使用以下导入;

from enum import Enum

如何使用python 3版本克服此问题?

注意:我没有安装enum34模块

#pip uninstall enum34 :Skipping enum34 as it is not installed.

这里的答案并没有解决我的问题 Why Python 3.6.1 throws AttributeError: module 'enum' has no attribute 'IntFlag'?

1 个答案:

答案 0 :(得分:1)

我正在使用Python3和Jupyter开发MacOS。我收到了类似的错误消息

Failed to import the site module
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 544, in <module>
    main()
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 530, in main
    known_paths = addusersitepackages(known_paths)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 282, in addusersitepackages
    user_site = getusersitepackages()
  File "/usr/local/Cellar/python/3.6.5/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/python/3.6.5/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/python/3.6.5/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/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sysconfig.py", line 580, in get_config_vars
    import _osx_support
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_osx_support.py", line 4, in <module>
    import re
  File "/usr/local/Cellar/python/3.6.5/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'

请注意,该通话具有不同的来源。但是,两者都会导致re.py模块导致AttributeError出现enum.py

对我来说,以下方法解决了这个问题:

在我的kernel.json配置中,我明确指定了PYTHONPATH指向site-package目录。修订版看起来像这样:

{
 "argv": [
  "/usr/local/opt/python/bin/python3.6",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python",
 "env": {
     "PYTHONPATH": "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/"
 }
}

env部分已丢失。添加路径后,错误消息消失。

请注意,我使用homebrew来安装python3,但我没有使用condavirtualenv

我希望这可以帮助你:)