我刚安装了Python(尝试了3.5.2和2.7.12,结果完全相同)。我已经尝试使用谷歌搜索并查看问题,但无法找到任何内容。
我正在尝试运行的代码只是基础教程的开头:
from sklearn import datasets
iris = datasets.load_iris()
digits = datasets.load_digits()
错误为ImportError: cannot import name 'datasets'
。
我尝试过重新安装所有东西。同样的结果一遍又一遍。我在带有El Capitan的Macbook上也是新安装的。
我用pyenv安装了Python,并通过pip scipy和numpy。顺便提一下,我还将pip升级到了最新版本。
import _frozen_importlib # frozen
import _imp # builtin
import sys # builtin
import '_warnings' # <class '_frozen_importlib.BuiltinImporter'>
import '_thread' # <class '_frozen_importlib.BuiltinImporter'>
import '_weakref' # <class '_frozen_importlib.BuiltinImporter'>
import '_frozen_importlib_external' # <class '_frozen_importlib.FrozenImporter'>
import '_io' # <class '_frozen_importlib.BuiltinImporter'>
import 'marshal' # <class '_frozen_importlib.BuiltinImporter'>
import 'posix' # <class '_frozen_importlib.BuiltinImporter'>
import _thread # previously loaded ('_thread')
import '_thread' # <class '_frozen_importlib.BuiltinImporter'>
import _weakref # previously loaded ('_weakref')
import '_weakref' # <class '_frozen_importlib.BuiltinImporter'>
# installing zipimport hook
import 'zipimport' # <class '_frozen_importlib.BuiltinImporter'>
# installed zipimport hook
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/encodings/__pycache__/__init__.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/encodings/__init__.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/encodings/__pycache__/__init__.cpython-35.pyc'
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/codecs.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/codecs.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/codecs.cpython-35.pyc'
import '_codecs' # <class '_frozen_importlib.BuiltinImporter'>
import 'codecs' # <_frozen_importlib_external.SourceFileLoader object at 0x108ac7390>
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/encodings/__pycache__/aliases.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/encodings/aliases.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/encodings/__pycache__/aliases.cpython-35.pyc'
import 'encodings.aliases' # <_frozen_importlib_external.SourceFileLoader object at 0x108af7f60>
import 'encodings' # <_frozen_importlib_external.SourceFileLoader object at 0x108ac0f60>
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/encodings/__pycache__/utf_8.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/encodings/utf_8.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/encodings/__pycache__/utf_8.cpython-35.pyc'
import 'encodings.utf_8' # <_frozen_importlib_external.SourceFileLoader object at 0x108b07d30>
import '_signal' # <class '_frozen_importlib.BuiltinImporter'>
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/encodings/__pycache__/latin_1.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/encodings/latin_1.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/encodings/__pycache__/latin_1.cpython-35.pyc'
import 'encodings.latin_1' # <_frozen_importlib_external.SourceFileLoader object at 0x108af97f0>
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/io.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/io.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/io.cpython-35.pyc'
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/abc.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/abc.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/abc.cpython-35.pyc'
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/_weakrefset.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/_weakrefset.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/_weakrefset.cpython-35.pyc'
import '_weakrefset' # <_frozen_importlib_external.SourceFileLoader object at 0x108b10470>
import 'abc' # <_frozen_importlib_external.SourceFileLoader object at 0x108af9c50>
import 'io' # <_frozen_importlib_external.SourceFileLoader object at 0x108af99e8>
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/site.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/site.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/site.cpython-35.pyc'
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/os.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/os.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/os.cpython-35.pyc'
import 'errno' # <class '_frozen_importlib.BuiltinImporter'>
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/stat.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/stat.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/stat.cpython-35.pyc'
import '_stat' # <class '_frozen_importlib.BuiltinImporter'>
import 'stat' # <_frozen_importlib_external.SourceFileLoader object at 0x108b954a8>
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/posixpath.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/posixpath.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/posixpath.cpython-35.pyc'
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/genericpath.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/genericpath.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/genericpath.cpython-35.pyc'
import 'genericpath' # <_frozen_importlib_external.SourceFileLoader object at 0x108b97d30>
import 'posixpath' # <_frozen_importlib_external.SourceFileLoader object at 0x108b957f0>
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/_collections_abc.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/_collections_abc.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/_collections_abc.cpython-35.pyc'
import '_collections_abc' # <_frozen_importlib_external.SourceFileLoader object at 0x108ba03c8>
import 'os' # <_frozen_importlib_external.SourceFileLoader object at 0x108b24278>
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/_sitebuiltins.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/_sitebuiltins.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/_sitebuiltins.cpython-35.pyc'
import '_sitebuiltins' # <_frozen_importlib_external.SourceFileLoader object at 0x108b24550>
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/sysconfig.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/sysconfig.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/sysconfig.cpython-35.pyc'
import 'sysconfig' # <_frozen_importlib_external.SourceFileLoader object at 0x108bd9668>
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/_sysconfigdata.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/_sysconfigdata.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/_sysconfigdata.cpython-35.pyc'
import '_sysconfigdata' # <_frozen_importlib_external.SourceFileLoader object at 0x108bdff28>
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/_osx_support.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/_osx_support.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/_osx_support.cpython-35.pyc'
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/re.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/re.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/re.cpython-35.pyc'
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/sre_compile.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/sre_compile.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/sre_compile.cpython-35.pyc'
import '_sre' # <class '_frozen_importlib.BuiltinImporter'>
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/sre_parse.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/sre_parse.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/sre_parse.cpython-35.pyc'
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/sre_constants.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/sre_constants.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/sre_constants.cpython-35.pyc'
import 'sre_constants' # <_frozen_importlib_external.SourceFileLoader object at 0x108c0a6a0>
import 'sre_parse' # <_frozen_importlib_external.SourceFileLoader object at 0x108bfed68>
import 'sre_compile' # <_frozen_importlib_external.SourceFileLoader object at 0x108bf5da0>
import '_locale' # <class '_frozen_importlib.BuiltinImporter'>
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/copyreg.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/copyreg.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/copyreg.cpython-35.pyc'
import 'copyreg' # <_frozen_importlib_external.SourceFileLoader object at 0x108c47470>
import 're' # <_frozen_importlib_external.SourceFileLoader object at 0x108bf1eb8>
import '_osx_support' # <_frozen_importlib_external.SourceFileLoader object at 0x108bf1080>
# /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/_bootlocale.cpython-35.pyc matches /Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/_bootlocale.py
# code object from '/Users/fredrik/.pyenv/versions/3.5.2/lib/python3.5/__pycache__/_bootlocale.cpython-35.pyc'
import '_bootlocale' # <_frozen_importlib_external.SourceFileLoader object at 0x108c47390>
import 'site' # <_frozen_importlib_external.SourceFileLoader object at 0x108b17d68>
Python 3.5.2 (default, Aug 30 2016, 00:56:52)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
# /Users/fredrik/code/scikit-learn/__pycache__/sklearn.cpython-35.pyc matches /Users/fredrik/code/scikit-learn/sklearn.py
# code object from '/Users/fredrik/code/scikit-learn/__pycache__/sklearn.cpython-35.pyc'
Traceback (most recent call last):
File "sklearn.py", line 1, in <module>
from sklearn import datasets
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/fredrik/code/scikit-learn/sklearn.py", line 1, in <module>
from sklearn import datasets
ImportError: cannot import name 'datasets'
# clear builtins._
# clear sys.path
# clear sys.argv
# clear sys.ps1
# clear sys.ps2
# clear sys.last_type
# clear sys.last_value
# clear sys.last_traceback
# destroy sklearn
# clear sys.path_hooks
# clear sys.path_importer_cache
# clear sys.meta_path
# clear sys.__interactivehook__
# clear sys.flags
# clear sys.float_info
# restore sys.stdin
# restore sys.stdout
# restore sys.stderr
# cleanup[2] removing encodings.latin_1
# cleanup[2] removing _locale
# cleanup[2] removing __main__
# destroy __main__
# cleanup[2] removing os.path
# cleanup[2] removing _sysconfigdata
# destroy _sysconfigdata
# cleanup[2] removing _bootlocale
# destroy _bootlocale
# cleanup[2] removing sysconfig
# destroy sysconfig
# cleanup[2] removing zipimport
# cleanup[2] removing sre_compile
# cleanup[2] removing _stat
# cleanup[2] removing _collections_abc
# destroy _collections_abc
# cleanup[2] removing posixpath
# cleanup[2] removing _imp
# cleanup[2] removing _weakrefset
# destroy _weakrefset
# cleanup[2] removing marshal
# cleanup[2] removing _frozen_importlib_external
# cleanup[2] removing posix
# cleanup[2] removing sre_constants
# destroy sre_constants
# cleanup[2] removing builtins
# cleanup[2] removing site
# destroy site
# cleanup[2] removing sre_parse
# cleanup[2] removing _weakref
# cleanup[2] removing encodings
# destroy encodings
# cleanup[2] removing errno
# cleanup[2] removing encodings.utf_8
# cleanup[2] removing _codecs
# cleanup[2] removing os
# cleanup[2] removing _frozen_importlib
# cleanup[2] removing _warnings
# cleanup[2] removing sys
# cleanup[2] removing codecs
# cleanup[2] removing abc
# cleanup[2] removing _io
# cleanup[2] removing stat
# cleanup[2] removing encodings.aliases
# cleanup[2] removing copyreg
# cleanup[2] removing io
# destroy io
# destroy abc
# cleanup[2] removing _osx_support
# destroy _osx_support
# cleanup[2] removing _thread
# cleanup[2] removing _sre
# cleanup[2] removing genericpath
# cleanup[2] removing re
# cleanup[2] removing _signal
# cleanup[2] removing _sitebuiltins
# destroy zipimport
# destroy _signal
# destroy _sitebuiltins
# destroy posixpath
# destroy errno
# destroy _stat
# destroy genericpath
# destroy stat
# destroy os
# destroy re
# destroy sre_compile
# destroy copyreg
# destroy sre_parse
# destroy _sre
# destroy _locale
# cleanup[3] wiping encodings.latin_1
# cleanup[3] wiping _imp
# cleanup[3] wiping marshal
# cleanup[3] wiping _frozen_importlib_external
# destroy marshal
# cleanup[3] wiping posix
# destroy posix
# cleanup[3] wiping _weakref
# cleanup[3] wiping encodings.utf_8
# cleanup[3] wiping _codecs
# cleanup[3] wiping _frozen_importlib
# destroy _frozen_importlib_external
# destroy _weakref
# destroy _imp
# cleanup[3] wiping _warnings
# destroy _warnings
# cleanup[3] wiping codecs
# cleanup[3] wiping _io
# destroy io
# cleanup[3] wiping encodings.aliases
# cleanup[3] wiping _thread
# destroy _thread
# cleanup[3] wiping sys
# cleanup[3] wiping builtins
# destroy _frozen_importlib
答案 0 :(得分:4)
错误是由您命名为/Users/fredrik/code/scikit-learn/sklearn.py
您的本地文件正在覆盖sklearn
库,因此您只需要将项目中的sklearn.py
文件重命名为其他文件即可。