How to use pip for pyenv?
Asked Answered
S

5

37

I have installed pyenv in my Mac to manage different python versions.

Before, I have the system default python 2.7 which is located in /Library/Frameworks/Python.framework/Versions/2.7/ and I also have python3 which is located in /usr/local/bin/python3

Now, I installed the pyenv and python 2.7.14 which is located in /Users/hao/.pyenv/shims/python2

I just curious when I want to install some library using 'pip' command, how to make sure I install the library into the right python? For example, I want to use 'pip' to install the torch or tensorflow into pyenv python 2.7.14. But don't want to install them into system default python. Also, how to change the pip3 version?

Here I using the which pip and which pip3, the results are:

haos-mbp:~ hao$ which pip
/Users/hao/.pyenv/shims/pip
haos-mbp:~ hao$ which pip3
/usr/local/bin/pip3
Sphygmograph answered 28/8, 2018 at 14:56 Comment(5)
If you want to install to your 2.7, use pip...if you wang for python 3, use pip3.Grantham
so if I use pip, how can I make sure it will be installed into the pyenv python2,7 instead of system default python 2.7. and If I also installed a python3 in pyenv, how can I make sure the pip3 will be installed into pyenv python3 instead of the python3 outside the pyenv?Sphygmograph
Possible duplicate of How to install modules in Python 2.7 insted of Python 3.6?Lollipop
/Users/hao/.pyenv/shims/python2 -m pip installLollipop
I personally use pipenv which is really awesome. You can simply do a pipenv install package-name and it will add that package to that environment only.Grantham
E
17

When using pyenv, you should be able to set your 'local' version in the directory you are working in, and then pip will rely on this version.

So in your case:

pyenv local 2.7.14
pip install package-name

See more on pyenv commands here: https://github.com/pyenv/pyenv/blob/master/COMMANDS.md

But I do think the main piece that is missing here is a 'virtual environment' to keep your Python packages independent per project (even if they share the same Python version). It is not necessary based on what you are asking, but it is a generally agreed upon best practice. See the Python docs here for more info.

Employer answered 2/1, 2020 at 22:9 Comment(5)
Unfortunately, pip will still install for system python and not for the local version.Methylnaphthalene
@Methylnaphthalene Please elaborate as to what you mean by that.Powwow
@Methylnaphthalene - once you are in the directory and have set your local version you can run which pip to find out which python version will be used.Employer
~/.pyenv/shims/pip install formatter # installs in ~/.local/lib/python2.7/site-packages but ~/.pyenv/shims/python2 repo status # still gives ModuleNotFoundError: No module named 'formatter'Paddy
Per mention of github.com/pyenv/pyenv/blob/master/COMMANDS.md#pyenv-install ...specifically pyenv exec pip install -r requirements.txt and pyenv exec pip install ... This will exec pip within the local context you set in pyenv. For example, mkdir your project/direction and set your local python version with something like pyenv global 3.9.1 and then cd into your dir, and execute pip with the aforementioned.Indict
L
9

I am sharing my realtime experience,

my system is pointing default to python2.7 even though I installed python3.6 in my machine

But when I trying to download new packages for python3.6,But it is downloading with default python2.7

so I came across this pyenv,

I installed the pyenv

after installing

 $ pyenv install --list
 $ pyenv global

pointing to default system(python2.7)

installed python3.6

$ pyenv install 3.6.9

changed from python2.7 to python3.6

$ pyenv global 3.6.9

Here, we have to notice that by installing pyenv by default pip is installed.

using pip command I installed required package which for python3.6 without adding suffix number like pip3.

 $ pip install pyOpenSSL

suppose if you want installed the package related python2.7 then change then python environment

$ pyenv global 2.7.0

and you can install your required package using pip instead of using pip3

pip install package-name
Lynden answered 10/12, 2020 at 11:4 Comment(0)
E
3

I stumbled upon this too, on my MAC.

This set of pyenv commands will help you:

pyenv versions - which python is activated (in pyenv) where * is placed 
pyenv local <your_python_version> - set locally which python in pyenv you'd like to use. 
pyenv which python - shows path to which python is activated in pyenv 
pyenv exec python helloworld.py - run your helloworld.py using pyenv

so, if you want to install something using PIP not to your system python, but to pyenv python, you should use this:

pyenv pip list - list your packages 
pyenv pip install django - install something, django for example, then repeat list command above

thats all. I was using just pip3 install and was installing everything globally on my system python, that was an error. use pyenv in each command at the start instead.

note (!): besides local there are global and shell levels too. shell overrides local and global, local overrides global.
enter image description here

Good guide is here.

Emanuele answered 12/11, 2022 at 18:50 Comment(2)
should be pyenv exec pip list... pyenv pip list does not work as its not part of pyenvCalif
pyenv 2.3.20-3-g38ac7472 says no such command pip.Paddy
C
3

just to add.

first look at shims. this shows where pyenv is linking the packages to.

pyenv shims

pyenv will try to be smart and install them in the packages defined for the version you installed i.e. ./.local/lib/python3.10/site-packages in the case for 3.10.x

so you can either first set a good global version

pyenv install --list  # show installable versions
pyenv install 3.9     # install 3.9.x latest
pyenv global 3.9      # set your global to use 3.9.x
pyenv local 3.10      # set your current session to use 3.10.x
pyenv local           # check which version you are using
python --version      # to reconfirm

there are a few ways to install packages.. just be consistent. its already good that you are using pyenv

# use pyenv to execute the current python which calls pip for this version
pyenv exec python -m pip --version
pyenv exec python -m pip install ansible

# (not recommended) example ansible guide way to install it under user directory which will mess things up, by trying to do additional symlinks and rely on paths. a good example is ansible with warnings.
pip install ansible --user

# (recommended) normal python invoked pip (to install ansible); note you need to install ansible everytime you switch to another version other than 3.10. you might want to install it globally.
pyenv local 3.10
python -m pip install ansible

additional good way

what i recommend is to have a .python-version file with one line defining the version (see below), in your current folder / home folder or project folder root; this locks the version. pyenv will automatically pickup and switch to the current version defined for this project etc and then you can run install -r requirements.txt to ensure packages are installed.

3.10
Calif answered 12/2, 2023 at 12:20 Comment(0)
B
1

python3 -m pip is an explicit way of ensuring you will use the pip you intended to use

$ pyenv version
3.9.16 (set by /home/lucjan/.pyenv/version)
2.7.18 (set by /home/lucjan/.pyenv/version)
$ python3 -m pip --version
pip 23.0 from /home/lucjan/.pyenv/versions/3.9.16/lib/python3.9/site-packages/pip (python 3.9)
$ python2 -m pip --version
pip 19.2.3 from /home/lucjan/.pyenv/versions/2.7.18/lib/python2.7/site-packages/pip (python 2.7)

Example of installing a package

python3 -m pip install requests

If you have trouble making pyenv version return proper versions use pyenv global

$ pyenv global 3.9.16 2.7.18
$ pyenv version
3.9.16 (set by /home/lucjan/.pyenv/version)
2.7.18 (set by /home/lucjan/.pyenv/version)

The first entry in pyenv global will be the default python version

$ pyenv global 3.9.16 2.7.18
$ python --version
Python 3.9.16
$ python -m pip --version
pip 23.0 from /home/lucjan/.pyenv/versions/3.9.16/lib/python3.9/site-packages/pip (python 3.9)
Bracy answered 7/2, 2023 at 9:11 Comment(2)
this probably wouldnt work as if you see the path.. python3 is tied to python3.9.. if you have python 3.8 / python 3.9 / python 3.10 ... python3 -m pip will be tied to the version you used. for example, if you tried to install ansible this way, once you go from python 3.9 to 3.10.. ansible installations would void.Calif
This doesn't help. ~/.pyenv/shims/python2 -m pip install formatter still installs in the same place, ~/.local/lib/python2.7/site-packages, and /.pyenv/shims/python2 repo status still fails to find it.Paddy

© 2022 - 2024 — McMap. All rights reserved.