Home > Blockchain >  Pip3 on mac os x 12.5 does not work even with virtual environment activated
Pip3 on mac os x 12.5 does not work even with virtual environment activated

Time:09-06

I currently cannot successfully use venv to install python packages on my mac via pip3. I am currently running 12.5 Monterey.

Without a venv activated, running which python3 yields

/Library/Frameworks/Python.framework/Versions/3.10/bin/python3

I installed Python3.10.6 from python.org, so this is not the system version of python that ships with osx 12.5. After activating a virtual environment:

$ source ~/py_envs/my_env/bin/activate

I can then run the following commands and get the following output:

$ which python3
/Users/admin/py_envs/my_env/bin/python3
$ which pip3
/Users/admin/py_envs/my_env/bin/pip3

So far, this all seems correct. However when I attempt to install a package via pip3 with the virtual environment activated, I get the following output:

$ pip3 install redis
Collecting redis
  Using cached redis-4.3.4-py3-none-any.whl (246 kB)
Collecting deprecated>=1.2.3
  Using cached Deprecated-1.2.13-py2.py3-none-any.whl (9.6 kB)
Collecting async-timeout>=4.0.2
  Using cached async_timeout-4.0.2-py3-none-any.whl (5.8 kB)
Collecting packaging>=20.4
  Using cached packaging-21.3-py3-none-any.whl (40 kB)
Collecting wrapt<2,>=1.10
  Using cached wrapt-1.14.1-cp310-cp310-macosx_11_0_arm64.whl (35 kB)
Collecting pyparsing!=3.0.5,>=2.0.2
  Using cached pyparsing-3.0.9-py3-none-any.whl (98 kB)
Installing collected packages: wrapt, pyparsing, async-timeout, packaging, deprecated, redis
Successfully installed async-timeout-4.0.2 deprecated-1.2.13 packaging-21.3 pyparsing-3.0.9 redis-4.3.4 wrapt-1.14.1
ERROR: Exception:
Traceback (most recent call last):
  File "/Users/admin/py_envs/rec_env/lib/python3.10/site-packages/pip/_internal/cli/base_command.py", line 167, in exc_logging_wrapper
    status = run_func(*args)
  File "/Users/admin/py_envs/rec_env/lib/python3.10/site-packages/pip/_internal/cli/req_command.py", line 247, in wrapper
    return func(self, options, args)
  File "/Users/admin/py_envs/rec_env/lib/python3.10/site-packages/pip/_internal/commands/install.py", line 520, in run
    self._handle_target_dir(
  File "/Users/admin/py_envs/rec_env/lib/python3.10/site-packages/pip/_internal/commands/install.py", line 530, in _handle_target_dir
    ensure_dir(target_dir)
  File "/Users/admin/py_envs/rec_env/lib/python3.10/site-packages/pip/_internal/utils/misc.py", line 105, in ensure_dir
    os.makedirs(path)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/os.py", line 215, in makedirs
    makedirs(head, exist_ok=exist_ok)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/os.py", line 215, in makedirs
    makedirs(head, exist_ok=exist_ok)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/os.py", line 215, in makedirs
    makedirs(head, exist_ok=exist_ok)
  [Previous line repeated 2 more times]
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/os.py", line 225, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 1] Operation not permitted: '/System/Library/Frameworks/Python.framework'

[notice] A new release of pip available: 22.2.1 -> 22.2.2
[notice] To update, run: pip install --upgrade pip

So it appears the version of global version of python3 is being referenced, even if I've activated my virtual environment. I have tried running pip install --upgrade pip which yields similar results. I have also tried installing python3 via homebrew, but attempting to install pacakges with pip still results in the same error. So far, I have not been able to find a solution on SO or elsewhere. Does anyone have any insight on this issue? Thanks in advance.

CodePudding user response:

try running type pip3 to see if it is aliased. if it is aliased unalias pip3. and try again. if that fails I suggest using pyenv to manage your python versions. remove all versions of python installed via homebrew and use pyenv to install python3. hope this helps.

CodePudding user response:

To upgrade the pre-installed Python3 on macOS to the latest or higher version(s), you can use pyenv, which can be installed using Homebrew:

  1. brew install pyenv
  2. pyenv install [Python version]
  3. You can see the list of the Python versions by running pyenv versions

and finally, to set a version as a default one, just run: pyenv global [Python version]

To set up a path for pyenv in the terminal (in my case => zsh), I manually add the following lines to .zshrc

export PYENV_ROOT="$HOME/.pyenv"

export PATH="$PYENV_ROOT/bin:$PATH"

eval "$(pyenv init --path)"

  • Related