Python pip: ImportError cannot import name 'ensure_str' from 'six'. On multiple pip commands
Asked Answered
Y

3

16

I noticed that something was wrong when I first wanted to install the tqdm package for python3. Running pip install tqdm i revieved

ImportError: cannot import name 'ensure_str' from 'six' (/home/carl/.local/lib/python3.8/site-packages/six.py).

There seem to be a similar problem when running a variety of pip commands such as pip freeze and pip list, but not pip show [package] or pip -h.

I am unure where to start and what to make of the situation and would greatly appreciate help. I will post the full trace below for context.

Full traceback: pip install [any package]

Traceback (most recent call last):
  File "/usr/bin/pip", line 11, in <module>
    load_entry_point('pip==20.0.2', 'console_scripts', 'pip')()
  File "/usr/lib/python3.8/site-packages/pip/_internal/cli/main.py", line 73, in main
    command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
  File "/usr/lib/python3.8/site-packages/pip/_internal/commands/__init__.py", line 96, in create_command
    module = importlib.import_module(module_path)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/usr/lib/python3.8/site-packages/pip/_internal/commands/install.py", line 24, in <module>
    from pip._internal.cli.req_command import RequirementCommand
  File "/usr/lib/python3.8/site-packages/pip/_internal/cli/req_command.py", line 20, in <module>
    from pip._internal.operations.prepare import RequirementPreparer
  File "/usr/lib/python3.8/site-packages/pip/_internal/operations/prepare.py", line 16, in <module>
    from pip._internal.distributions import (
  File "/usr/lib/python3.8/site-packages/pip/_internal/distributions/__init__.py", line 2, in <module>
    from pip._internal.distributions.wheel import WheelDistribution
  File "/usr/lib/python3.8/site-packages/pip/_internal/distributions/wheel.py", line 5, in <module>
    from pip._internal.utils.wheel import pkg_resources_distribution_for_wheel
  File "/usr/lib/python3.8/site-packages/pip/_internal/utils/wheel.py", line 12, in <module>
    from pip._vendor.six import PY2, ensure_str
ImportError: cannot import name 'ensure_str' from 'six' (/home/carl/.local/lib/python3.8/site-packages/six.py) 

Traceback: pip freeze

Traceback (most recent call last):
  File "/usr/bin/pip", line 11, in <module>
    load_entry_point('pip==20.0.2', 'console_scripts', 'pip')()
  File "/usr/lib/python3.8/site-packages/pip/_internal/cli/main.py", line 73, in main
    command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
  File "/usr/lib/python3.8/site-packages/pip/_internal/commands/__init__.py", line 96, in create_command
    module = importlib.import_module(module_path)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/usr/lib/python3.8/site-packages/pip/_internal/commands/freeze.py", line 12, in <module>
    from pip._internal.operations.freeze import freeze
  File "/usr/lib/python3.8/site-packages/pip/_internal/operations/freeze.py", line 17, in <module>
    from pip._internal.req.constructors import (
  File "/usr/lib/python3.8/site-packages/pip/_internal/req/__init__.py", line 11, in <module>
    from .req_file import parse_requirements
  File "/usr/lib/python3.8/site-packages/pip/_internal/req/req_file.py", line 25, in <module>
    from pip._internal.req.constructors import (
  File "/usr/lib/python3.8/site-packages/pip/_internal/req/constructors.py", line 28, in <module>
    from pip._internal.req.req_install import InstallRequirement
  File "/usr/lib/python3.8/site-packages/pip/_internal/req/req_install.py", line 30, in <module>
    from pip._internal.operations.install.wheel import install_wheel
  File "/usr/lib/python3.8/site-packages/pip/_internal/operations/install/wheel.py", line 33, in <module>
    from pip._internal.utils.wheel import parse_wheel
  File "/usr/lib/python3.8/site-packages/pip/_internal/utils/wheel.py", line 12, in <module>
    from pip._vendor.six import PY2, ensure_str
ImportError: cannot import name 'ensure_str' from 'six' (/home/carl/.local/lib/python3.8/site-packages/six.py)

EDIT: I was suggested to look at https://mcmap.net/q/751508/-attributeerror-39-module-39-object-has-no-attribute-39-ensure_str-39. And It says that 'ensure_str' is included in six 1.12 and forward, however I only have version 1.11. This explains why it cant be found. However I cannot fix this by for example running pip install --upgrade six nor pip uninstall six or pip check six. Because any of those commands give the same "cannot import name" error. Help to solve this would be greatly appreciated.

Yorkist answered 11/5, 2020 at 10:38 Comment(11)
Does this answer your question? AttributeError: 'module' object has no attribute 'ensure_str'Litho
Upgrade your six. If you cannot upgrade it with pip upgrade it from pypi.org/project/six/#filesLitho
Thanks for the hep everyone! I have manage to more accurately locate what the error is now. It works to do everything as long as I do sudo before. This since six is installed in root /usr/lib/python3.8/site-packages/. However pip tried to find it in /home/myname/.local/lib/python3.8/...... probably since I have tried to have pip work withouth sudo before. Any good tips on how to migrate everything from /usr/lib/ to /home/myname/.local/lib?Yorkist
Do not migrate — reinstall with pip install --user without sudo.Litho
I cannot install anything with pip install --user since if I do not use sudo all my pip commands that is depended on six seems to give the cannot import name 'ensure_str. At least that is what I think happens.Yorkist
You need to upgrade all your installations of six. There're certainly many.Litho
Yes but if I do sudo pip install --upgrade six it upgrades the installation that has its package located in /root/.local/lib/python3.8/site-packages, if and if i do pip install --upgrade six or pip install --upgrade --user sixI get the cannot import ensure_str from six (/home/myname/.local/lib/python3.8/site-packages/six.py) error. So the problem is that I cant upgrade with pip so I will try and upgrade it from [pypi.org/project/six/#files] again.Yorkist
To upgrade six without pip: download six-1.14.0.tar.gz, extract and run python setup.py installLitho
Thanks, when I do that I get the following error, Where sould I install the package? error: can't create or remove files in install directory [Errno 13] Permission denied: '/usr/lib/python3.8/site-packages/test-easy-install-456615.write-test' The installation directory you specified (via --install-dir, --prefix, or the distutils default setting) was: /usr/lib/python3.8/site-packages/ Yorkist
python setup.py install --userLitho
I finally solved it thanks for the help @phd, I couldn't use python setup install --user howerver i forced the installation to happen in /home/myname/.loval/ib/python3.8/site-packages/ and then i took the new six.py file and replaced the one that was giving an error there.Yorkist
K
5

I had this problem within my virtual environment when installing from the requirements.txt file that came with the code. My system Python (3.8.5) ran just fine and on the system I had six==1.14.0, but the requirements.txt file had six==1.11.0. Thus, whenever I ran pip install -r requirements.txt within my venv things would break because it would install the older version of six.

My solution was to delete the first venv I created, change the line in requirements.txt to six==1.14.0 (the working version), rebuild my venv, and then install using the newly modified requirements.txt file. In hindsight this seems like common sense, but in the moment it didn't occur to me. In short: don't overwrite the working version of six with the older, non-compatible version.

Kaneshakang answered 22/1, 2021 at 22:1 Comment(0)
H
2
  1. Open the file /home/carl/.local/lib/python3.8/site-packages/six.py
  2. Replace the content of file with the latest versión in https://github.com/benjaminp/six/blob/master/six.py

It works for me.

Taken from: https://www.reddit.com/r/learnpython/comments/igsaza/importerror_cannot_import_name_ensure_str_from/g2vqftq/?utm_source=reddit&utm_medium=web2x&context=3

Hygrometry answered 23/4, 2021 at 22:18 Comment(0)
B
0

my problem was PyCharm couldn't install any packages, i used @HaroldMideros's answer except the location was $pycharm_install_dir/plugins/python/helpers/six.py

Bread answered 29/7, 2021 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.