'pipenv `ModuleNotFoundError: No module named 'pip.basecommand`

I'm trying to create a virtual environment using pipenv. When I give the command pipenv install, I get the following error

I am using pipenv==2018.6.25

Traceback (most recent call last): File "c:\programdata\anaconda3\lib\site-packages\pipenv\vendor\requirementslib_compat.py", line 33, in do_import _tmp = importlib.import_module(internal) File "c:\programdata\anaconda3\lib\importlib__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import File "", line 971, in _find_and_load File "", line 953, in _find_and_load_unlocked ModuleNotFoundError: No module named 'pip._internal.basecommand'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "c:\programdata\anaconda3\lib\runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "c:\programdata\anaconda3\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:\ProgramData\Anaconda3\Scripts\pipenv.exe__main__.py", line 5, in File "c:\programdata\anaconda3\lib\site-packages\pipenv__init__.py", line 23, in from .cli import cli File "c:\programdata\anaconda3\lib\site-packages\pipenv\cli.py", line 26, in from .utils import is_valid_url File "c:\programdata\anaconda3\lib\site-packages\pipenv\utils.py", line 59, in from .vendor.requirementslib import Requirement File "c:\programdata\anaconda3\lib\site-packages\pipenv\vendor\requirementslib__init__.py", line 6, in from .models import Requirement, Lockfile, Pipfile File "c:\programdata\anaconda3\lib\site-packages\pipenv\vendor\requirementslib\models__init__.py", line 8, in from .requirements import Requirement File "c:\programdata\anaconda3\lib\site-packages\pipenv\vendor\requirementslib\models\requirements.py", line 15, in from .markers import PipenvMarkers File "c:\programdata\anaconda3\lib\site-packages\pipenv\vendor\requirementslib\models\markers.py", line 6, in from .utils import validate_markers, filter_none File "c:\programdata\anaconda3\lib\site-packages\pipenv\vendor\requirementslib\models\utils.py", line 9, in from .._compat import Link File "c:\programdata\anaconda3\lib\site-packages\pipenv\vendor\requirementslib_compat.py", line 55, in Command = do_import("basecommand", "Command") File "c:\programdata\anaconda3\lib\site-packages\pipenv\vendor\requirementslib_compat.py", line 35, in do_import _tmp = importlib.import_module(pip9) File "c:\programdata\anaconda3\lib\importlib__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ModuleNotFoundError: No module named 'pip.basecommand'

Anybody has any idea how I can correct this error?



Solution 1:[1]

I just ran into a similar error during pipenv lock.

After updating pipenv via pip install --user --upgrade pipenv and pip via python -m pip install --upgrade pip the lock command resulted in an error message rather than the traceback. This error message stated:

There are incompatible versions in the resolved dependencies.

Solution 2:[2]

You may want to upgrade pipenv to the latest version. I had the same issue, now I installed version 2018.11.26, pipenv lock works flawlessly.

  • Run pip install pipenv --upgrade, this will install the latest pipenv. If there's permission error use sudo as a prefix or add --user parameter.
  • Once it's installed re-source the PATH by running source towards your shell config file, in my case source ~/.bashrc

Solution 3:[3]

I encountered this issue with pip 18.0 in Mac for virtual environment using pipenv (failing the pipfile.lock creation with this error message: "ModuleNotFoundError: No module named 'pip.basecommand'"). It was solved by pip install --user --upgrade pipenv to update pipenv (to pipenv-2018.11.14.).

Solution 4:[4]

I had this problem with Python 3.10 and Python 3.9 together. I'm using Ubuntu 22.04 LTS. My packages only work in 3.9 and that's what the environment is set to. 3.10 is system Python.

The way I solved it was to use pipenv installed globally to shell into the 3.9 environment. Then when in there:

pip3 install pipenv --upgrade

If you run which pipenv outside of the virtual environment you'll get 3.10 and it will use /usr/bin/pipenv

If you run which pipenv inside of the virtual environment you'll get 3.9 and it will use the new virtual environment version which is 3.9 and should then pipenv install correctly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yveso
Solution 2 aluk.e
Solution 3 Rohullah Abdullahpour
Solution 4 Ryu Kent