r/pythonhelp • • 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.

  1. 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 venv

  • ERROR: 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 env

  • source ./env/bin/activate

  • python --version

  • Python 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 zsh

  • chown: zsh: Operation not permitted

I'm getting very frustrated.

4 Upvotes

12 comments sorted by

View all comments

Show parent comments

1

u/vglocus Apr 06 '23

What you are lacking is exec permission on the activate script. The command to change that is

chmod a+x <path to script>

So the full sequence of commands ought to be

python3 -m venv venv

chmod a+x ./venv/bin/activate

./venv/bin/activate

Does is work?

1

u/AndydeCleyre Apr 09 '23

You should not be running the activate script, but sourcing it.

1

u/vglocus Apr 10 '23

What is the difference in this case? (genuine question)

1

u/AndydeCleyre Apr 10 '23

It aims to modify the current shell's environment, setting variables and making functions available. Sourcing it makes it happen in your active environment.

You shouldn't even be able to run it without an error. I had a look and it doesn't come with a shebang, but instead:

# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly