【问题标题】:ImportError: No Module named six; six already installedImportError: No Module named 6;已经安装了六个
【发布时间】:2017-11-23 07:18:44
【问题描述】:

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

我正在尝试运行使用六模块的代码,但出现以下错误:

ImportError: No module named 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 卸载然后重新安装为我修复了 :)
  • 安装后看pip list是否安装了6个
  • pip list 确实显示 six (1.10.0) 已安装,但错误仍然存​​在
  • 我也遇到了这种情况。迄今为止,没有任何答案有帮助。我正在运行 Ubuntu GNOME 17.04。

标签: python macos six


【解决方案1】:

这应该可行:

pip install --ignore-installed six

更多信息在这里: https://github.com/pypa/pip/issues/3165

【讨论】:

  • 我尝试运行它并收到以下错误: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
  • 权限错误意味着你就是这样。尝试在命令中添加--user 并阅读提供的链接
【解决方案2】:

我没有看到来自six Documentation Release 1.10.0 的六的任何方法版本(),并且您得到的错误还说六没有对我有意义的属性,下面我打印所有属性并且有@987654322 @里面

>>> 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'

因此你可以得到六的版本

 python -c "import six; print (six.__version__)"

【讨论】:

  • 谢谢,为什么 version() 不起作用是有道理的。当我运行 python -c "import six; print (six.__version__)" 时,它会正确打印 1.10.0,但 ImportError 仍然存在
  • 如果模块没有被导入怎么可能呢?尝试使用 python shell 看看会发生什么
  • 我应该澄清一下——运行这里提到的小代码没有问题。我遇到问题的代码以from six import string_types 开头,我在这里看到ImportError
  • @Lucas13 string_types 是常量,你直接用:six.integer_types,现在应该好了
  • 我不确定我是否遵循 - 你是说我应该用 six.string_types 替换代码中使用的 string_types 而不是导入模块 six
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 2018-01-19
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多