r/pythonhelp • u/DisabledScientist • Apr 05 '23
How to use Python virtual environments in terminal mac?
Hello,
I have a few questions regarding Python virtual environments on Mac. I am running Python 3.11.2.
Apparently, venv is installed with newer versions of Python, but when I type
pip3 show venv, I get this message:WARNING: Package(s) not found: venv. So, I tried installing it:
pip3 install venvERROR: Could not find a version that satisfies the requirement venv (from versions: none)ERROR: No matching distribution found for venv
So I installed virtualenv instead. Do I need to have the version of Python that I am trying to make a virtual environment with installed on my system? When I run:
virtualenv -p python3.9 env
I get the error message:
RuntimeError: failed to find interpreter for Builtin discover of python_spec='python3.9'
Even when I DO have the version of Python installed on my system when I run virtualenv and check the Python version, it's still running Python system default:
virtualenv -p python3.8 envsource ./env/bin/activatepython --versionPython 3.11.2(system default)
Maybe I should just start from scratch, uninstall everything that has to do with Python on my system, and reinstall. How would I do this?
I have Python installed to these locations:
/Users/myname/Library/Python/3.8
/Users/myname/Library/Python/3.7
/Library/Frameworks/Python.framework/Versions/3.8
/Library/Frameworks/Python.framework/Versions/3.11
My .zshrc file has these Python paths:
alias python=/Library/Frameworks/Python.framework/Versions/3.11/bin/python3
alias python3=/Library/Frameworks/Python.framework/Versions/3.11/bin/python3
export PATH="/usr/local/bin/python3.11:$PATH"
I know I need to change permissions on my zsh shell. When I run:
sudo chown myname zshchown: zsh: Operation not permitted
I'm getting very frustrated.
1
u/AndydeCleyre Apr 05 '23
I think your bit about zsh is confused.
Never try to install venv with pip, that's not how it's distributed.
I suggest using rtx to install multiple versions of Python and switch between them, then use venv (not virtualenv) to create virtual environments. Use the version of Python you want each venv to use to create each venv.