Reputation: 1319
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
Upvotes: 39
Views: 118147
Reputation: 3094
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
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
Upvotes: 6
Reputation: 139
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)
Upvotes: 2
Reputation: 349
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.
Good guide is here.
Upvotes: 4
Reputation: 403
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
Upvotes: 9
Reputation: 470
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.
Upvotes: 19