Why is the re module trying to import enum.IntFlag?

对着背影说爱祢 提交于 2020-01-02 07:03:20

问题


How is it possible that a core module is importing a non-existent name from another core module?

Specifically, the re module importing enum.IntFlag


Ways to Reproduce

It can be reproduced by launching an interpreter and attempting the import, running a program that depends on enum.IntFlag such as pip, viewing the interpreter settings page in Eclipse, using the text editor, running the interactive console in PyDev, ...

Directly importing

from enum import IntFlag

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'IntFlag'

Run a dependant program (pip)

Traceback (most recent call last):
  File "/usr/bin/pip", line 4, in <module>
    import re
  File "/usr/lib/python3.6/re.py", line 142, in <module>
    class RegexFlag(enum.IntFlag):
AttributeError: module 'enum' has no attribute 'IntFlag'

Viewing the Python Interpreter settings in Eclipse also produces the above traceback (in the window the interpreter settings would be in).

Preferences =:> PyDev =:> Interpreters =:> Python Interpreter

Also the text editor doesn't recognise a print statement.

Also, when I run the console interpreter (eg: Ctrl+Alt+Enter, eclipse fails with a popup showing this:

'Create Interactive Console' has encountered a problem.

Error initializing console.

Error Details

Click the Error Details button:

Error initializing console.
Unexpected error connecting to console.
Failed to recive suitable Hello response from pydevconsole. Last msg received: Console already exited with value: 1 while waiting for an answer.

Command Line used:  /usr/bin/python3.6 -u /usr/lib/eclipse/../../../home/scott/.eclipse/org.eclipse.platform_4.7.1_155965261_linux_gtk_x86_64/plugins/org.python.pydev_6.2.0.201711281614/pysrc/pydevconsole.py 44633 43575 44633 43575

Environment:
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/opt/cuda/bin:/usr/lib/jvm/default/bin:/opt/jython/bin/:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
XAUTHORITY=/tmp/xauth-1000-_0
XDG_DATA_DIRS=/usr/share:/usr/share:/usr/local/share
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
XDG_CURRENT_DESKTOP=KDE
QT_AUTO_SCREEN_SCALE_FACTOR=0
MAIL=/var/spool/mail/scott
MOZ_PLUGIN_PATH=/usr/lib/mozilla/plugins
QT_LINUX_ACCESSIBILITY_ALWAYS_ON=1
SESSION_MANAGER=local/scott-pc:@/tmp/.ICE-unix/976,unix/scott-pc:/tmp/.ICE-unix/976
LOGNAME=scott
PAM_KWALLET5_LOGIN=/run/user/1000/kwallet5.socket
PWD=/home/scott
XCURSOR_THEME=Azenis
PYPATH=/code/scott/Py/Path
PYTHONPATH=/home/scott/.eclipse/org.eclipse.platform_4.7.1_155965261_linux_gtk_x86_64/plugins/org.python.pydev_6.2.0.201711281614/pysrc/pydev_sitecustomize:/mnt/ssdata/scott/code/Py/Path:/home/scott:/usr/lib/python3.6/Tools/scripts:/usr/lib/python3.6/lib-dynload:/usr/lib/python3.6/site-packages:/usr/lib/python3.6
KDE_SESSION_VERSION=5
SHELL=/bin/bash
KDE_MULTIHEAD=false
KDE_FULL_SESSION=true
GTK_MODULES=canberra-gtk-module
GDK_SCALE=1
DATA=/data/scott/
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session1
PYDYNLOAD=/usr/lib/python3.6/lib-dynload
VM=/vm/scott/
XDG_SESSION_DESKTOP=KDE
SHLVL=1
PYSITE=/usr/lib/python3.6/site-packages
OXYGEN_DISABLE_INNER_SHADOWS_HACK=1
PYVER=3.6
KDE_SESSION_UID=1000
XFILESEARCHPATH=/usr/dt/app-defaults/%L/Dt
LANG=en_GB.UTF-8
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
XDG_SESSION_ID=c2
XDG_SESSION_TYPE=x11
DISPLAY=:0
PYTHONSTARTUP=/usr/lib/python3.6/startup_hook.py
GTK_RC_FILES=/etc/gtk/gtkrc:/home/scott/.gtkrc:/home/scott/.config/gtkrc
ECLIPSE_HOME=/usr/lib/eclipse
XDG_SESSION_CLASS=user
XCURSOR_SIZE=56
LIBOVERLAY_SCROLLBAR=0
DESKTOP_SESSION=/usr/share/xsessions/plasma
GDK_CORE_DEVICE_EVENTS=1
USER=scott
GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/scott/.gtkrc-2.0:/home/scott/.config/gtkrc-2.0
XDG_SEAT=seat0
CODE=/code/scott/
GS_LIB=/home/scott/.fonts
PYSCRIPTS=/usr/lib/python3.6/Tools/scripts
NLSPATH=/usr/dt/lib/nls/msg/%L/%N.cat
XDG_VTNR=1
XDG_RUNTIME_DIR=/run/user/1000
HOME=/home/scott
PYTHONIOENCODING=UTF-8
PYDEV_UMD_ENABLED=true
PYDEV_UMD_NAMELIST=guidata,guiqwt
PYDEV_UMD_VERBOSE=true

Progress

in the standard library version /usr/lib/python3.6/enum.py, the name enum.IntFlag exists in enum.__all__

Is enum being imported from the standard library version?

import enum
print(enum.__file__)
# /usr/lib/python3.6/site-packages/enum/__init__.py

It is importing from /usr/lib/python3.6/site-packages/enum/__init__.py I checked that file and there is no IntFlag. Also, it's a package init rather than a single file.

There is no /usr/lib/python3.6/site-packages/enum/enum.py. There is a README file that says that this is ver 3.4.

I could replace the contents of __init__.py with enum.py

I could delete the site-packages/enum directory


Solution

There is another enum module, a python3.4 compatibility package, that was masking the module version in the standard library.

python34 compatibility package: /usr/lib/python3.6/enum/__init__.py

the standard library module: /usr/lib/python3.6/enum.py.

I deleted the directories:

mkdir enumbackup
cd enumbackup
sudo mv /usr/lib/python3.6/enum .
sudo mv /usr/lib/python3.6/enumenum34-1.1.6.dist-info .

Now, enum.__file__ shows /usr/lib/python3.6/enum.py as it should and everything works.

from enum import IntFlag
## works

Thanks for the help! :)


回答1:


You should check to see if there's another module named enum on your python path that is shadowing the stdlib enum module. To do that, you can do:

import enum
print(enum.__file__)

If that doesn't match the python3 stdlib module path, you try should try to remove it. Ideally, you should use whatever package manager your system uses to uninstall it, as it it might be a dependency of some other package. But if that isn't possible, you can just try removing it manually.



来源:https://stackoverflow.com/questions/47878060/why-is-the-re-module-trying-to-import-enum-intflag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!