Skip to main content
 首页 » 编程设计

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

2024年12月31日14小虾米

我有 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'?

请您参考如下方法:

我正在使用 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 模块导致 enum.py

出现 AttributeError

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

在我的 kernel.json 配置中,我明确指定了指向 site-package 目录的 PYTHONPATH。修改后的版本如下所示:

{ 
 "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 而我没有使用 conda 或 virtualenv .

希望对您有所帮助:)