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

1

u/roztopasnik Apr 05 '23

I usually create it with ‘python3 -m venv venv’ and then activate it with ./venv/bin/activate, try if this will work for you

2

u/DisabledScientist Apr 05 '23 edited Apr 05 '23

python3 -m venv venv

This worked to create the env folder and a virtual environment.

./venv/bin/activate

This gave me an error:

-bash: ./venv/bin/activate: Permission denied

I cd'd into the venv folder to check my permissions. Here is the output:

  • drwxr-xr-x 12 Bob staff 384 Apr 5 17:55 bin

  • drwxr-xr-x 3 Bob staff 96 Apr 5 17:55 included

  • rwxr-xr-x 3 Bob staff 96 Apr 5 17:55 lib

  • -rw-r--r-- 1 Bob staff 303 Apr 5 17:55 pyvenv.cfg

I have been having problems ever since I switched from bash to zsh. Zsh was causing so many problems I switched back to bash. Switching to zsh messed up all of my permissions, but the problem remained when I switched back to bash.

Here is the output of the permissions in my home directory:

  • drwxrwxr-x 89 root admin 2848 Apr 5 15:56 Applications

  • drwxr-xr-x 75 root wheel 2400 Apr 5 15:56 Library

  • drwxr-xr-x@ 9 root wheel 288 Mar 26 2022 System

  • drwxr-xr-x 5 root admin 160 Mar 26 2022 Users

  • drwxr-xr-x 3 root wheel 96 Mar 22 18:47 Volumes

  • drwxr-xr-x@ 38 root wheel 1216 Mar 26 2022 bin

  • drwxr-xr-x 2 root wheel 64 Jan 1 2020 cores

  • dr-xr-xr-x 3 root wheel 4595 Jul 5 2022 dev

  • lrwxr-xr-x@ 1 root wheel 11 Mar 26 2022 etc -> private/etc

  • lrwxr-xr-x 1 root wheel 25 Jul 5 2022 home -> /System/Volumes/Data/home

  • drwxr-xr-x 5 root wheel 160 Sep 14 2021 opt

  • drwxr-xr-x 6 root wheel 192 Jul 5 2022 private

  • drwxr-xr-x@ 65 root wheel 2080 Mar 26 2022 sbin

  • lrwxr-xr-x@ 1 root wheel 11 Mar 26 2022 tmp -> private/tmp

  • drwxr-xr-x@ 11 root wheel 352 Mar 26 2022 usr

  • lrwxr-xr-x@ 1 root wheel 11 Mar 26 2022 var -> private/var

When I try changing permissions, it doesn't let me:

  • sudo chmod 777 Library

  • Password:

  • chmod: Unable to change file mode on Library: Operation not permitted

1

u/AndydeCleyre Apr 05 '23

It is unlikely any of that is due to zsh. Doesn't macos have some kind of "fix permissions" tool in the settings? I don't really know.