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 pythonProjectSwitching pipenv: pythonProject-IbA-tQW [🐍Python 3.12.0]$ [~/Developer/pythonProject]
In that virtual environment, python
and pipenv
both work:
$ [~/Developer/pythonProject] python --versionPython 3.12.0$ [~/Developer/pythonProject] pipenv --versionpipenv, version 2022.6.7$ [~/Developer/pythonProject]
However, pip
refuses to run, which makes it impossible to install packages.
$ [~/Developer/pythonProject] pip install icecreamTraceback (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 DistutilsCommandModuleNotFoundError: No module named 'distutils'$ [~/Developer/pythonProject]
Maybe it's picking up the wrong pip
. Let's see what version it is…
$ [~/Developer/pythonProject] pip --versionTraceback (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 DistutilsCommandModuleNotFoundError: 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 --versionpip 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'