导入错误:没有六个模块; 六已经安装

8

我在Mac OS X El Capitan上运行Python 3.6。

我试图运行使用six模块的代码,但是出现以下错误:

导入错误:没有名为six的模块.

当我搜索six时,它似乎没有问题,并且我确保它的位置包含在sys.path中。

$ pip show six
Name: six
Version: 1.10.0
Summary: Python 2 and 3 compatibility utilities
Home-page: http://pypi.python.org/pypi/six/
Author: Benjamin Peterson
Author-email: benjamin@python.org
License: MIT
Location: /usr/anaconda/lib/python3.6/site-packages

然而,当我尝试运行一些基本操作时,遇到了一个错误:
$ python -c "import six; print (six.__version__)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: module 'six' has no attribute 'version'

我尝试了卸载和重新安装,还尝试使用$ python -m pip install six进行安装,但都没有成功。

如果有人有任何想法或需要更多信息,请告诉我,谢谢。


我在周末遇到了类似的pytz问题,使用pip卸载然后重新安装解决了我的问题 :) - Sasha
安装完成后,通过 pip list 命令查看是否已安装六(six)库。 - be_good_do_good
pip list 显示已安装 six (1.10.0),但错误仍然存在。 - Lucas13
我也遇到了这个问题。到目前为止,没有任何答案能够帮助我。我正在运行Ubuntu GNOME 17.04。 - Spencer Hill
5个回答

8

我尝试运行这个程序,但是出现了以下错误:PermissionError: [Errno 13] Permission denied: '/usr/anaconda/lib/python3.6/site-packages/six-1.10.0.dist-info/DESCRIPTION.rst'我还尝试了 sudo pip install --ignore-installed six,虽然它似乎安装成功了,但我仍然无法运行原始代码 - 我仍然看到 ImportError - Lucas13
权限错误就是指你没有相应的权限。尝试在命令中添加“--user”并阅读提供的链接。 - raam86

6

我最近更新了macOS Monterey 12.4,遇到了同样的问题。

ModuleNotFoundError: No module named 'six'

所以我已经使用以下命令安装了“six”

brew install six

这解决了问题。希望这对于那些无法导入或没有“pip”的人有所帮助。

是的,在 Monterey 12.6.7 上通过 brew install streamlink 安装 streamlink 时遇到了同样的问题。这个方法解决了问题,谢谢! - undefined

1
我在six Documentation Release 1.10.0中没有看到任何关于six版本的version()方法。你得到的错误信息也说six没有这个属性,这对我来说很有道理。下面我打印出了所有的属性,里面包括__version__
>>> import six
>>> six.__dir__()
['_moved_attributes', 'remove_move', '__path__', '__author__', '_MovedItems', 'Module_six_moves_urllib', 'Module_six_moves_urllib_robotparser', 'raise_from', '_SixMetaPathImporter', 'get_function_code', 'callable', 'absolute_import', '_func_code', 'moves', '_urllib_error_moved_attributes', 'text_type', 'Module_six_moves_urllib_parse', 'iteritems', 'iterlists', 'print_', '_assertCountEqual', '__builtins__', 'sys', 'Module_six_moves_urllib_error', 'Module_six_moves_urllib_request', 'assertRegex', 'MovedModule', 'create_bound_method', '_urllib_robotparser_moved_attributes', '_func_closure', 'indexbytes', 'string_types', 'with_metaclass', 'reraise', 'exec_', 'assertRaisesRegex', 'types', 'python_2_unicode_compatible', 'get_function_globals', '_LazyModule', '_assertRaisesRegex', '_meth_self', 'itertools', '_LazyDescr', 'BytesIO', 'add_move', 'iterbytes', '_func_defaults', '__file__', 'unichr', 'get_method_function', 'create_unbound_method', 'get_unbound_function', 'Module_six_moves_urllib_response', 'functools', '__doc__', 'assertCountEqual', 'integer_types', 'PY34', '_importer', '__spec__', '_urllib_response_moved_attributes', 'Iterator', 'StringIO', '_import_module', '__package__', '__version__', 'get_function_defaults', 'operator', 'PY3', 'MAXSIZE', 'int2byte', '_urllib_request_moved_attributes', '_urllib_parse_moved_attributes', 'b', 'class_types', 'next', 'itervalues', '_add_doc', 'viewkeys', 'MovedAttribute', 'advance_iterator', '__cached__', 'u', '__loader__', '_func_globals', 'get_method_self', 'PY2', 'iterkeys', 'wraps', '_meth_func', 'byte2int', 'io', 'viewitems', 'viewvalues', '__name__', 'get_function_closure', 'binary_type', 'add_metaclass', '_assertRegex']
>>> six.__version__
'1.10.0'

因此,您可以通过以下方式获取 six 的版本:
 python -c "import six; print (six.__version__)"

谢谢,这解释了为什么 version() 没有起作用。当我运行python -c "import six; print (six.__version__)"它正确地打印出 1.10.0,但是 ImportError 仍然存在。 - Lucas13
如果模块没有被导入,那怎么可能呢?尝试使用Python shell并查看会发生什么。 - Haifeng Zhang
我应该澄清一下 - 运行这里提到的小代码没有问题。我遇到问题的代码以 from six import string_types 开头,我在这里看到了一个 ImportError - Lucas13
@Lucas13 string_types 是常量,你可以直接使用它:six.integer_types,现在应该没问题了。 - Haifeng Zhang
@Lucas13 使用 import six,然后当你需要 string_types 时,只需调用 six.string_types 即可。 - Haifeng Zhang
显示剩余2条评论

1

如果以上任何答案都无法解决您的问题 - python3.10在某些情况下需要您调用pip+python版本,因此...

我的解决方法是:pip3.10 install six

然后,Six 已经/曾经 安装到了python3中(如果您有重叠版本),但它没有安装到当前版本的python中。

您可以检查这是否会解决您的问题,因为当您输入:pip3 --version时,您可能看不到python3正在调用的相同版本。

如果您看到以下内容:

pip3 --version
pip 23.0 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

但是你的 python3 --version 显示的是:
python3 --version
Python 3.10.9
< p >如果您遇到了Python3和pip3版本不匹配的问题,< strong >使用我的答案可以解决您的问题。 < p >(您可以使用以下命令检查Python3别名调用的版本:< code >which python3)

0
我遇到的问题是我运行的脚本使用的是Python 2.7,而我在我的机器上使用的是3+。在使用venv切换到Python 2.7后,一切都正常工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接