python列舉遇到的一個坑
阿新 • • 發佈:2018-12-09
實現了一個類繼承自列舉
from enum import Enum
class tbEmUser(Enum):
userId = 0
userName = 1
程式碼的使用是這樣的
tbEmUser.userId
結果是:
>>> tbEmUser.userId
0
時候發現換了一臺電腦之後
執行同樣的程式碼,結果成了這樣
>>> tbEmUser.userId
<tbEmUser.userId: 0>
開始以為是python版本不一致
或者庫的版本不一致
pip list DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning. aip (1.0.0.5) asn1crypto (0.24.0) baidu-aip (2.0.0.1) beautifulsoup4 (4.6.0) blinker (1.4) bottle (0.12.13) certifi (2017.11.5) chardet (3.0.4) click (6.7) cryptography (2.1.4) cssselect (1.0.3) Cython (0.28.1) Django (1.11.5) django-extensions (1.9.9) django-werkzeug-debugger-runserver (0.3.1) duplicity (0.7.17) enum (0.4.7) enum34 (1.1.6) eyeD3 (0.8.4) fasteners (0.12.0) Flask (0.12.2) funcsigs (1.0.2) html5lib (0.999999999) idna (2.6) image (1.5.15) ipaddress (1.0.17) itsdangerous (0.24) Jinja2 (2.10) keyring (10.6.0) keyrings.alt (3.0) lockfile (0.12.2) lxml (4.2.1) MarkupSafe (1.0) meld3 (1.0.2) mercurial (4.5.3) monotonic (1.0) mysqlclient (1.3.12) numpy (1.13.3) oauthlib (2.0.6) olefile (0.44) parsel (1.4.0) pathlib (1.0.1) pika (0.9.8) Pillow (4.2.1) pip (9.0.1) PyAudio (0.2.11) pycairo (1.16.2) pycrypto (2.6.1) pycurl (7.43.0.1) PyDispatcher (2.0.5) pygame (1.9.1release) pygobject (3.26.1) PyJWT (1.5.3) PyOpenGL (3.1.0) pyOpenSSL (17.5.0) pyserial (3.4) python-magic (0.4.15) pytz (2017.2) pyusb (1.0.2) pyxdg (0.25) queuelib (1.5.0) requests (2.18.4) rope (0.10.5) scour (0.36) Scrapy (1.5.0) SecretStorage (2.3.1) setuptools (39.0.1) six (1.11.0) supervisor (3.3.1) typing (3.6.2) unity-lens-photos (1.0) urllib3 (1.22) uWSGI (2.0.15) w3lib (1.19.0) webencodings (0.5) Werkzeug (0.14.1) WeRoBot (1.2.0) wheel (0.30.0) xmltodict (0.11.0) You are using pip version 9.0.1, however version 18.0 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
檢視發現enum的版本都是0.4.7
[email protected]:~$ python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
python的版本也都是2.7.15.rc1
這就奇怪了,難道跟系統的版本有關係?因為是從ubuntu16的系統換到ubuntu18的系統執行的
後來仔細看了看庫的列表,裡面有一個enum34
試著把它解除安裝掉試試看
[email protected]:~$ sudo pip uninstall enum34 [sudo] password for drivesim: The directory '/home/drivesim/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. Uninstalling enum34-1.1.6: /usr/lib/python2.7/dist-packages/enum /usr/lib/python2.7/dist-packages/enum34-1.1.6.egg-info Proceed (y/n)? y Successfully uninstalled enum34-1.1.6 The directory '/home/drivesim/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. You are using pip version 9.0.1, however version 18.0 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
再試了一下,結果居然可以了
那原因估計就是如果既有enum也有enum34這裡個庫的話,我這樣的語法
from enum import Enum
優先匯入的是enum34