Pipenv "ModuleNotFoundError: No module named 'pip'" after upgrading to python3.7

Viewed 10491

I am using ubuntu 18. The default python3 version is 3.6. I updated to 3.7 today and update the alternatives to point to python3.7.

I can use python3.7 by typing python3. I can also use pip3 --version (20.0.2).

I can activate the virtual environment by using pipenv shell. But I cannot install package using pipenv install . It gives me the following error:

pipenv.exceptions.InstallError]: ['Traceback (most recent call last):', '  File "/home/johnchan/.local/share/virtualenvs/src-lkQYyAWf/bin/pip", line 5, in <module>', '    from p
ip._internal.cli.main import main', "ModuleNotFoundError: No module named 'pip'"]
ERROR: ERROR: Package installation failed...

Running which pip3: /usr/local/bin/pip3 Running which pipenv: /usr/local/bin/pipenv

Type pip3 inside pipenv gives:

Traceback (most recent call last):
  File "/home/johnchan/.local/share/virtualenvs/src-lkQYyAWf/bin/pip3", line 5, in <module>
    from pip._internal.cli.main import main
ModuleNotFoundError: No module named 'pip'
2 Answers
python2 -m pip install --user --upgrade pip

python3 -m pip install --user --upgrade pip

After upgrading pip (or pip3, in this case) if the following occurs:

$ ~ pip3 -V
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 7, in <module>
    from pip._internal import main
ModuleNotFoundError: No module named 'pip._internal'

Force a reinstall of pip:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py --force-reinstall

Verify install:

$ ~ pip3 -V
pip 10.0.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)

Now pip3 install <package> and pip3 install --user <package> (for user-level installs) will work correctly.

There should never, ever be any reason you need to run pip in elevated mode.

(note: For Python 2.7, just replace python for python3, and pip for pip3)

Had the same issue on macOS as well, it's a common issue across platforms.

I found the answer here: Setting up a virtualenv: No module named 'pip'

Seems like it is a bug.

I install pipenv using --re flag which is equivalent to virtualenv venv --no-setuptools.

Then I run python get-pip.py inside pipenv.

It works. I can install package now.

But I don't know the reason why...

Related