我刚刚在我的Mac(macOS Sierra 10.12.2)上安装了python 3的anaconda,许多软件包在导入时会发出错误。这似乎与打字包有关,但我无法弄清楚发生了什么。如果我这样做
python -c 'import sklearn'
(蟒蛇正在Python 3.5.2 :: Anaconda 4.2.0 (x86_64)
)
我收到此错误:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/geromepistre/anaconda3/lib/python3.5/site-packages/sklearn/__init__.py", line 57, in <module>
from .base import clone
File "/Users/geromepistre/anaconda3/lib/python3.5/site-packages/sklearn/base.py", line 6, in <module>
import copy
File "/Users/geromepistre/anaconda3/lib/python3.5/copy.py", line 51, in <module>
import types
File "/Users/geromepistre/types.py", line 1, in <module>
import typing
File "/Users/geromepistre/anaconda3/lib/python3.5/typing.py", line 4, in <module>
import contextlib
File "/Users/geromepistre/anaconda3/lib/python3.5/contextlib.py", line 5, in <module>
from functools import wraps
File "/Users/geromepistre/anaconda3/lib/python3.5/functools.py", line 22, in <module>
from types import MappingProxyType
ImportError: cannot import name 'MappingProxyType'
我试图深入研究错误中提到的软件包,但它变得更加混乱。 import types
给出了相同的错误,以及import contextlib
.但是,如果我先import contextlib
(并得到错误),那么import types
不会出错。我在import sklearn
上收到一个全新的错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/geromepistre/anaconda3/lib/python3.5/site-packages/sklearn/__init__.py", line 57, in <module>
from .base import clone
File "/Users/geromepistre/anaconda3/lib/python3.5/site-packages/sklearn/base.py", line 6, in <module>
import copy
File "/Users/geromepistre/anaconda3/lib/python3.5/copy.py", line 114, in <module>
types.BuiltinFunctionType, type(Ellipsis),
AttributeError: module 'types' has no attribute 'BuiltinFunctionType'
您似乎正在从主目录运行 Python,根据第一个回溯,该目录包含一个名为 types.py
的文件。由于当前目录(.
Unix/Linux术语)是sys.path
中的第一个条目,Anaconda在标准库或站点包之前搜索模块/文件。
出于这个原因,不惜一切代价避免为标准 lib 或站点包中的模块命名文件。将文件重命名types.py
为 my_types.py
或其他什么应该可以解决问题。