23

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
HAO CHEN
  • 1,059
  • 3
  • 16
  • 32
  • 1
    If you want to install to your 2.7, use pip...if you wang for python 3, use pip3. – Oh Great One Aug 28 '18 at 14:59
  • 1
    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? – HAO CHEN Aug 28 '18 at 15:04
  • Possible duplicate of [How to install modules in Python 2.7 insted of Python 3.6?](https://stackoverflow.com/questions/48883581/how-to-install-modules-in-python-2-7-insted-of-python-3-6) – phd Aug 28 '18 at 17:08
  • 1
    `/Users/hao/.pyenv/shims/python2 -m pip install` – phd Aug 28 '18 at 17:08
  • 1
    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. – Oh Great One Aug 28 '18 at 17:15

2 Answers2

11

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.

jmh
  • 246
  • 3
  • 7
  • 1
    Unfortunately, pip will still install for system python and not for the local version. – kaiya May 03 '22 at 18:33
3

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
Arshad Syed
  • 355
  • 2
  • 7