Dan Donaldson
Dan Donaldson

Reputation: 1293

still finding system python with pyenv installed - installing modules

On Mac, current OS, system python is still 2.7. I've installed pyenv, and successfully installed 3.7.3.

dandonaldson@Dans-MacBook-Pro-2 gpt-2 % pyenv local
3.7.3
dandonaldson@Dans-MacBook-Pro-2 gpt-2 % pyenv global
3.7.3
dandonaldson@Dans-MacBook-Pro-2 gpt-2 % 

If I now I try to install pipx, as a way to install packages, I get

dandonaldson@Dans-MacBook-Pro-2 gpt-2 % pip install --user pipx 
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting pipx
  Using cached pipx-0.15.4.0.tar.gz (336 kB)
    ERROR: Command errored out with exit status 1:
     command: /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/fz/hkppw37x1tz20d3jv8rb3vmr0000gn/T/pip-install-u0WjuX/pipx/setup.py'"'"'; __file__='"'"'/private/var/folders/fz/hkppw37x1tz20d3jv8rb3vmr0000gn/T/pip-install-u0WjuX/pipx/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/fz/hkppw37x1tz20d3jv8rb3vmr0000gn/T/pip-pip-egg-info-bKDh7q
         cwd: /private/var/folders/fz/hkppw37x1tz20d3jv8rb3vmr0000gn/T/pip-install-u0WjuX/pipx/
    Complete output (1 lines):
    Python 3.6 or later is required. See https://github.com/pipxproject/pipx for installation instructions.
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

I'm told not to remove the python in */Frameworks, so it seems like I have to get around this while leaving that in place.

A previous shot using brew left me with

    which python
    python: aliased to /usr/local/bin/python3.8

and when I try this I get

brew uninstall python
Error: Refusing to uninstall /usr/local/Cellar/[email protected]/3.8.4
because it is required by imagemagick, which is currently installed.
You can override this and force removal with:
  brew uninstall --ignore-dependencies python

So,...

I've used rubyenv for many years, it's not this wretched… :(

Extra points: does Python and pyenv have the same behaviour of allowing python environments to have their own modules and versions?

Upvotes: 2

Views: 2184

Answers (1)

baggiponte
baggiponte

Reputation: 726

Let me try this again:

Question 1

  • can I uninstall the brew python without the world ending? I have no python projects

I am afraid not, as imagemagick is currently using it -- which means every binary that requires imagemagick requires python3.

Question 2

  • how can I get module packages to install by default in a place accessible to 3.7.3?

From what I understand, what you are trying to say more broadly is: "How can I install packages with the pip binary relative to the Python version I have installed with pyenv?".

The answer should be: pyenv should take care of it. If it does not, you should do an additional step. Please keep in mind I will assume you have installed pyenv with homebrew.

1. How to check if pyenv works

If pyenv was set up correctly, the following line will be the output of the command echo $PATH:

$(pyenv root)/shims:/usr/local/bin:/usr/bin:/bin

where $(pyenv root) is the place where pyenv is located - which most of the time is ~/.pyenv. But you can change it by setting PYENV_ROOT in your ~/.zshenv (I like to have these things in ~/.config/ to clear the clutter in my ~/ directory).

The official documentation suggests to run echo $PATH | grep --color=auto "$(pyenv root)/shims". If there is no output or it's an error (e.g., echo $? returns the error 1), then one additional step is required.

2. The additional step to set up pyenv after installation with homebrew

Run the following command in your terminal emulator:

echo 'eval "$(pyenv init --path)"' >> ~/.zprofile

(alternatively, if you have set up your ZDOTDIR environmental variable, run echo 'eval "$(pyenv init --path)"' >> $ZDOTDIR/.zprofile).

Then restart your shell. Now your path should look the way you expect it to.

3. Additional step to enable pyenv commands autocompletion

Run the following command to enable tab-completion in the terminal for pyenv commands.

echo 'eval "$(pyenv init -)"' >> ~/.zshrc # or $ZDOTDIR/.zshrc

4. Is pyenv working?

You can check again by looking at your $PATH variable:

$ echo $PATH | tr ":" "\\n" # to have it spit out in an orderly fashion
/usr/local/Caskroom/miniconda/base/condabin # don't mind this
/Users/luca/.local/bin # these are my custom scripts
/Users/luca/.pyenv/shims # this is what you are looking for!
/usr/local/bin
/usr/bin
/bin
/usr/local/sbin
/usr/sbin
/sbin
/Library/TeX/texbin
/opt/X11/bin

Do not mind the /usr/local/Caskroom/miniconda/base/condabin portion, I am mainly using conda to manage Python environments.

Then, if you run which/where pip or which/where pip3 to see which binaries it is actually using when you are attempting to install a package, you should see:

$ where pip3
/Users/luca/.pyenv/shims/pip3 # that's pyenv
/usr/local/bin/pip3 # this is homebrew
/usr/bin/pip3 # this is the systems'

$ where pip
/Users/luca/.pyenv/shims/pip # that's pyenv

This indicates that when you execute a command with pip or pip3 the binary installed with pyenv will be used and the global or local version of Python will be actually working under the hood.

An example

I installed Python 3.9.5 with pyenv and set it as a global version, then used it to install pandas:

$ pip install pandas
Collecting pandas
  Using cached pandas-1.2.4-cp39-cp39-macosx_10_9_x86_64.whl (10.7 MB)
Collecting numpy>=1.16.5
  Downloading numpy-1.20.3-cp39-cp39-macosx_10_9_x86_64.whl (16.1 MB)
     |████████████████████████████████| 16.1 MB 1.8 MB/s
Collecting pytz>=2017.3
  Using cached pytz-2021.1-py2.py3-none-any.whl (510 kB)
Collecting python-dateutil>=2.7.3
  Using cached python_dateutil-2.8.1-py2.py3-none-any.whl (227 kB)
Collecting six>=1.5
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: six, pytz, python-dateutil, numpy, pandas
Successfully installed numpy-1.20.3 pandas-1.2.4 python-dateutil-2.8.1 pytz-2021.1 six-1.16.0
WARNING: You are using pip version 21.1.1; however, version 21.1.2 is available.
You should consider upgrading via the '/Users/luca/.pyenv/versions/3.9.5/bin/python3.9 -m pip install --upgrade pip' command.

This does not seem helpful but please note that in the WARNING it says /Users/luca/.pyenv/versions/3.9.5/bin/python3.9, which indicates I am indeed using pyenv's Python.

Question 3

Extra points: does Python and pyenv have the same behaviour of allowing python environments to have their own modules and versions?

Yes! Several options for environments can be used. conda is the most used, then someone uses pyenv-virtualenv or other provided officially by Python (it should be pipenv. I have always used conda, but it does not offer any official auto-completion and sometimes is annoying. There are other alternatives, such as mamba - never used it tho.

Upvotes: 6

Related Questions