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

时间:2017-12-18 05:57:45

标签: python pip

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)

5 个答案:

答案 0 :(得分:4)

PIP is also a python package. You can use,

python3 -m pip install foo

答案 1 :(得分: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.

答案 2 :(得分:1)

这就是我最终解决自己问题的方法。但它对我没有意义。任何人都可以帮忙解释为什么它解决了这个问题?

  1. 我尝试过这里发布的解决方案(没有帮助,或者我没有理解)
  2. 我决定用conda来安装软件包,因为我的python3是用Anaconda安装的,我的python2是单独安装的。
  3. 我发现我的终端无法理解" conda"
  4. 我输入"导出PATH =〜/ anaconda / bin:$ PATH",根据conda command is not recognized on windows 10
  5. 突然,pip install正确安装包到python3。输入pip -V给我pip3。

答案 3 :(得分: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.

答案 4 :(得分:0)

说明

在大多数情况下,python2 没有安装 pip。我使用的是 macOS,我使用这些命令来运行:

解决方案

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

然后使用:

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

提示:

根据个人经验,我建议不要在 .bashrc.zshrc 中为您的 Python 版本设置别名。它会影响其他安装或系统设置(例如,如果您使用 vim)。相反,在运行代码时指定 python 的版本

python2 <file_name>
# or 
python3 <file_name> 

并安装软件包

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

这也适用于 Ananconda 附带的 python 版本。 我希望这会有所帮助。

相关问题