2

I've been puzzled for a while. I can't install/upgrade any package for my python35 now. I have python27 and python35(via anaconda) on mac. Whenever I want to install a python package via pip, the one paired with python2 was invoked.

Go to anaconda folder(where my python35 installed): ls -al | grep pip I got the result:

-rwxrwxr-x    1 xx  staff      113 Jul 26  2016 conda-pipbuild
-rwxr-xr-x    1 xx  staff      230 Dec 17 21:40 pip
-rwxr-xr-x    1 xx  staff      230 Dec 17 21:40 pip3
-rwxr-xr-x    1 xx  staff      230 Dec 17 21:40 pip3.5

In my .bashrc file, I defined:

 alias python2=/usr/bin/python2.7
 alias python=~/anaconda/bin/python3.5

When I type python2:

$ python2
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

When I type python:

   $ python
    Python 3.5.2 |Anaconda custom (x86_64)| (default, Jul  2 2016, 17:52:12) 
    [GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.

When type: pip -V

pip 9.0.1 from /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg (python 2.7)

When type pip3 -V

-bash: pip3: command not found

which pip

/usr/local/bin/pip

To reinstall pip3, I downloaded get-pip.py from https://pip.pypa.io/en/latest/installing/ From the doc it says, it will install the correct pip according to the version of Python runs the script. Therefore, I ran

python get-pip.py 
Requirement already up-to-date: pip in /Users/xxx/anaconda/lib/python3.5/site-packages

Below shows When I try to install a package with pip:

pip install tushare --upgrade
Collecting tushare
  Using cached tushare-1.0.7-py2-none-any.whl
Requirement already up-to-date: lxml>=3.8.0 in /Library/Python/2.7/site-packages (from tushare)
Lisa
  • 4,126
  • 12
  • 42
  • 71
  • Depending on how you installed python3 you may find that you actually have a `pip3.5` or something similar. If you want to access it when typing `pip3`, you should set an alias for it. Better yet, package your code into virtual environments and when you're inside a venv you will be able to access pip3 with `pip` – Charles D Pantoga Dec 18 '17 at 06:06
  • what path should I put for the pip3 alias? I guess I have is somewhere here:/Users/xxx/anaconda/lib/python3.5/site-packages according to get-pip.py @CharlesAddis – Lisa Dec 18 '17 at 06:09
  • 1
    Possible duplicate of [How to install pip with Python 3?](https://stackoverflow.com/questions/6587507/how-to-install-pip-with-python-3) – Paul Rooney Dec 18 '17 at 06:12
  • @Lisa you can put the alias wherever you want, but typically that would go in /usr/local/bin if this is a python version you installed yourself otherwise it would go in /usr/bin if it is the system installed version of python. Other places you could put it are `~/bin` if you have a bin directory in your homedir. It all depends on your PATH – Charles D Pantoga Dec 18 '17 at 06:14
  • @Gahan that's a mac. – n. m. could be an AI Dec 18 '17 at 06:15
  • You probably have several pip scripts in different folders on your system and either PATH selects one of them or /usr/bin/pip symlinks to one of them. Try finding all executable files called pip\* on the system. – n. m. could be an AI Dec 18 '17 at 06:21
  • I saw this post, it cannot solve my problem@PaulRooney – Lisa Dec 18 '17 at 06:25

5 Answers5

5

PIP is also a python package. You can use,

python3 -m pip install foo
Prakash Palnati
  • 3,231
  • 22
  • 35
  • When I use sudo python3 -m pip install foo, it still uses python2. However, when I use python3 -m pip install foo, it works (using python3). How is this possible? – Lisa Dec 18 '17 at 06:30
  • 1
    run `sudo python3` and `python3` in the terminal and see what it gives you. – Prakash Palnati Dec 18 '17 at 06:39
2

The pip that comes with Anaconda does not get its symbolic link for pip3 by default. You can check which pip you are using by

which pip

Likely, it is not the pip from your Anaconda 3 installation. The way to fix it is pretty simple: Create the symbolic link yourself. Since Anaconda3's binary folder is already in your path (you can check it by which python or which python3), you can go to the anaconda3/bin folder

ls -al | grep pip

You may be able to see something like this

-rwxrwxr-x 1 youraccount youraccount     120 Jul 13 21:58 pip

Then create a symbolic link that points to it

ls -s pip pip3

That's it. You can try which pip3 again to see if that is the pip you want to refer to.

EDIT

I notice that you are using alias to access the python of Anaconda. As there are a lot of useful tools under anaconda/bin, it is necessary to put the entire folder into your path. At the same time, it is better to remove the alias in case of anything weird happening in the future.

To add anaconda/bin to your path, first you need to check what your PATH variable in bash looks like

echo $PATH

I guess you don't have anaconda/bin anywhere in the printout. Otherwise, you should be able to use pip3 without an issue.

If you use all default choices of anaconda, you should have this line in your .bashrc or .profile or .bash_profile

export PATH="/Users/youraccount/anaconda3/bin:$PATH"

If you already have this, run source .bashrc (or the file that contains the line), you should be able to see anaconda/bin in your PATH.

If not, put the line there, and source the file or restart the terminal. Also, remove the alias for python3.

Last, start this answer post from top to create the symbolic link for pip with the name you want (say, pip3)

This should solve your problem.

Yo Hsiao
  • 678
  • 7
  • 12
  • I have 4 different verion when type: ls -al | grep pip, how should I proceed?@Yo Hsiao -rwxrwxr-x 1 xx staff 113 Jul 26 2016 conda-pipbuild -rwxr-xr-x 1 xx staff 230 Dec 17 21:40 pip -rwxr-xr-x 1 x staff 230 Dec 17 21:40 pip3 -rwxr-xr-x 1 x staff 230 Dec 17 21:40 pip3.5 – Lisa Dec 18 '17 at 06:18
  • @lisa If you read the `pip`, `pip3`, `pip3.5` in plain text, you will see they are all identical. My version does not come with `pip3` nor `pip3.5`. In this case, you don't need to create a symbolic link. You just need to put the whole folder in your `PATH`. – Yo Hsiao Dec 18 '17 at 20:58
1

This is how I finally solved my own issue. But it doesn't make sense to me. Anyone could help to explain why it solved the problem?

  1. I tried solutions posted here(doesn't help or I didn't understand)
  2. I decided to use conda to install the package, since my python3 was installed with Anaconda, and my python2 was installed seperately.
  3. I found my terminal cannot understand "conda"
  4. I typed "export PATH=~/anaconda/bin:$PATH", according to conda command is not recognized on windows 10
  5. suddenly, pip install conrrectly installs package to python3. Type pip -V gives me pip3.
Lisa
  • 4,126
  • 12
  • 42
  • 71
0

Firstly, Install pip3 :

sudo apt-get update

sudo apt install python3-pip

now check the version of pip3 :

pip3 -V

Install package using pip3 for python3 :

sudo  pip3 install django

Much python packages require also the dev package, so install it too:

sudo apt-get install python3-dev

You can also create virtual environment for python3 and install package for pyhton3 using pip :

virtualenv -p /usr/bin/python3 envs
source envs/bin/activate
pip install package-name

Refer here for more on conda environments.

Chirag Maliwal
  • 442
  • 11
  • 25
  • 1
    In my experience, this may be tricky as the pip3 installed through apt-get may not be in the `anaconda3` folder. If you already have python3 on your system, this pip3 will go with it. Once using it to install packages, the packages will be part of the original python3 in the system as opposed to the Anaconda. Correct me if I am mistaken. – Yo Hsiao Dec 18 '17 at 06:16
  • @YoHsiao, you are right at your point. We can also create virtual environment for python3 and install package for python3. – Chirag Maliwal Dec 18 '17 at 06:25
  • 1
    Was apt-get ported to Mac OS while I was sleeping? – n. m. could be an AI Dec 18 '17 at 07:37
  • @n.m. If you want the equivalent to apt-get or yum on Mac OS X You can use `brew install PACKAGE_NAME` to install the package available. – Chirag Maliwal Dec 18 '17 at 09:11
0

Description

In most cases, python2 doesnt come with pip installed. I am using macOS and I used these commands to run:

Solution

curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
sudo python2 get-pip.py 

Then use:

pip2 install <package_name> 
#or 
python2 -m pip install <package_name> 

Hint:

From a personal experience, I suggest not alias your python version in the .bashrc or .zshrc. It will affect other installations or set up by your system (for example if you use vim). Instead, specify the version of python when you run your code with

python2 <file_name>
# or 
python3 <file_name> 

and to install packages

pip3 install <package_name>
# or 
pip2 install <package_name>

This also works for the python version that comes with Ananconda. I hope this is helpful.

Dr Neo
  • 724
  • 6
  • 11