1

I've created a blank, brand-new Python project using PyCharm. From the command line, going into that project correctly activates the virtual environment.

$ [~/Developer] cd pythonProject Switching pipenv: pythonProject-IbA-tQW [🐍Python 3.12.0] $ [~/Developer/pythonProject] 

In that virtual environment, python and pipenv both work:

$ [~/Developer/pythonProject] python --version Python 3.12.0 $ [~/Developer/pythonProject] pipenv --version pipenv, version 2022.6.7 $ [~/Developer/pythonProject] 

However, pip refuses to run, which makes it impossible to install packages.

$ [~/Developer/pythonProject] pip install icecream Traceback (most recent call last): File "/Users/andrew/.local/share/virtualenvs/pythonProject-IbA-tQW-/bin/pip", line 5, in <module> from pip._internal.cli.main import main ... File "/Users/andrew/.local/share/virtualenvs/pythonProject-IbA-tQW-/lib/python3.12/site-packages/pip/_internal/locations/_distutils.py", line 9, in <module> from distutils.cmd import Command as DistutilsCommand ModuleNotFoundError: No module named 'distutils' $ [~/Developer/pythonProject] 

Maybe it's picking up the wrong pip. Let's see what version it is…

$ [~/Developer/pythonProject] pip --version Traceback (most recent call last): File "/Users/andrew/.local/share/virtualenvs/pythonProject-IbA-tQW-/bin/pip", line 5, in <module> from pip._internal.cli.main import main ... File "/Users/andrew/.local/share/virtualenvs/pythonProject-IbA-tQW-/lib/python3.12/site-packages/pip/_internal/locations/_distutils.py", line 9, in <module> from distutils.cmd import Command as DistutilsCommand ModuleNotFoundError: No module named 'distutils' $ [~/Developer/pythonProject] 

So I'm not actually trying to do anything with pip — just see what version it is. Apparently I can't do that.

Outside the virtual environment, it's fine:

$ [~/Developer] pip --version pip 23.3.1 from /usr/local/lib/python3.11/site-packages/pip (python 3.11) $ [~/Developer] 

I'm really starting to be quite bothered by the difficulty in Python virtual environments. It's a bit of a trainwreck, to be honest. Every time I need to upgrade a Python version it's a major issue.

What do I have to do to install a package in a Python 3.12 virtual environment when pip refuses to work?


Edit: Poetry doesn't help, as it defers to pip for package removal. Partial poetry output:

The following error occurred when trying to handle this error: EnvCommandError Command ['/Users/andrew/.local/share/virtualenvs/pythonProject-IbA-tQW-/bin/python', '-m', 'pip', 'uninstall', 'icecream', '-y'] errored with the following return code 1 Output: Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main ... File "/Users/andrew/.local/share/virtualenvs/pythonProject-IbA-tQW-/lib/python3.12/site-packages/pip/_internal/locations/_distutils.py", line 9, in <module> from distutils.cmd import Command as DistutilsCommand ModuleNotFoundError: No module named 'distutils' 
3
  • poetry can be a good "alternative" to pip (even if poetry uses pip). You can definitely try it Commented Dec 17, 2023 at 0:14
  • poetry works to add packages, but it can't remove them — same problem, as its removal is a veneer over pip. Commented Dec 17, 2023 at 0:20
  • I had the same problem until I updated PyCharm to the latest version. I have a pro license but I don't think that matters. Try updating and recreate your venv. Commented Dec 17, 2023 at 0:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.