简体   繁体   中英

how to install package for python3 using pip? Now it always install for python2

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)

PIP is also a python package. You can use,

python3 -m pip install foo

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.

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.

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

Referhere for more on conda environments.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM