I have a Macbook with OS X El Captain. I think that Python 2.7 comes preinstalled on it. However, I installed Python 3.5 too. When I started using Python 3, I read that if I want to install a package, I should type:
pip3 install some_package
Anyway, now when I use
pip install some_package
I get some_package installed for Python 3. I mean I can import it and use it without problems. Moreover, when I type just pip3 in the Terminal. I got this message about the usage:
Usage:
pip <command> [options]
which is the same message I get when I type just pip.
Does it mean that in previous versions, things were different, and now pip and pip3 can be used interchangeably? If so, and for the sake of argument, how can I install packages for Python 2 instead of Python 3?
Your pip is a soft link to the same executable file path with pip3.
you can use the commands below to check where your pip and pip3 real paths are:
$ ls -l `which pip`
$ ls -l `which pip3`
You may also use the commands below to know more details:
$ pip show pip
$ pip3 show pip
When we install different versions of python, we may create such soft links to
set default pip to some version.
make different links for different versions.
It is the same situation with python, python2, python3
More information below if you're interested in how it happens in different cases:
MacOS/Homebrew
Fedora/CentOS
Debian/Ubuntu
If you had python 2.x and then installed python3, your pip will be pointing to pip3.
you can verify that by typing pip --version which would be the same as pip3 --version.
On your system you have now pip, pip2 and pip3.
If you want you can change pip to point to pip2 instead of pip3.
When you install python3, pip3 gets installed. And if you don't have another python installation(like python2.7) then a link is created which points pip to pip3.
So pip is a link to to pip3 if there is no other version of python installed(other than python3).
pip generally points to the first installation.
By illustration:
pip --version
pip 19.0.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)
pip3 --version
pip 19.0.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)
python --version
Python 3.7.3
which python
/usr/bin/python
ls -l '/usr/bin/python'
lrwxrwxrwx 1 root root 7 Mar 26 14:43 /usr/bin/python -> python3
which python3
/usr/bin/python3
ls -l /usr/bin/python3
lrwxrwxrwx 1 root root 9 Mar 26 14:43 /usr/bin/python3 -> python3.7
ls -l /usr/bin/python3.7
-rwxr-xr-x 2 root root 14120 Mar 26 14:43 /usr/bin/python3.7
Thus, my in my default system python (Python 3.7.3), pip is pip3.
I think pip, pip2 and pip3 are not soft links to the same executable file path. Note these commands and results in my Linux terminal:
mrz#mrz-pc ~ $ ls -l `which pip`
-rwxr-xr-x 1 root root 292 Nov 10 2016 /usr/bin/pip
mrz#mrz-pc ~ $ ls -l `which pip2`
-rwxr-xr-x 1 root root 283 Nov 10 2016 /usr/bin/pip2
mrz#mrz-pc ~ $ ls -l `which pip3`
-rwxr-xr-x 1 root root 293 Nov 10 2016 /usr/bin/pip3
mrz#mrz-pc ~ $ pip -V
pip 9.0.1 from /home/mrz/.local/lib/python2.7/site-packages (python 2.7)
mrz#mrz-pc ~ $ pip2 -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
mrz#mrz-pc ~ $ pip3 -V
pip 9.0.1 from /home/mrz/.local/lib/python3.5/site-packages (python 3.5)
As you see they exist in different paths.
pip3 always operates on the Python3 environment only, as pip2 does with Python2. pip operates in whichever environment is appropriate to the context. For example, if you are in a Python3 venv, pip will operate on the Python3 environment.
This is a tricky subject. In the end, if you invoke pip it will invoke either pip2 or pip3, depending on how you set your system up.
If you installed Python 2.7, I think you could use pip2 and pip2.7 to install packages specifically for Python 2, like
pip2 install some_pacakge
or
pip2.7 install some_package
And you may use pip3 or pip3.5 to install pacakges specifically for Python 3.
On my Windows instance - and I do not fully understand my environment - using pip3 to install the kaggle-cli package worked - whereas pip did not. I was working in a conda environment and the environments appear to be different.
(fastai) C:\Users\redact\Downloads\fast.ai\deeplearning1\nbs>pip
--version
pip 9.0.1 from C:\ProgramData\Anaconda3\envs\fastai\lib\site-packages (python 3.6)
(fastai) C:\Users\redact\Downloads\fast.ai\deeplearning1\nbs>pip3
--version
pip 9.0.1 from c:\users\redact\appdata\local\programs\python\python36\lib\site-packages
(python 3.6)
Given an activated Python 3.6 virtualenv in somepath/venv, the following aliases resolved the various issues on a macOS Sierra where pip insisted on pointing to Apple's 2.7 Python.
alias pip='python somepath/venv/lib/python3.6/site-packages/pip/__main__.py'
This didn't work so well when I had to do sudo pip as the root user doesn't know anything about my alias or the virtualenv, so I had to add an extra alias to handle this as well. It's a hack, but it works, and I know what it does:
alias sudopip='sudo somepath/venv/bin/python somepath/venv/lib/python3.6/site-packages/pip/__main__.py'
background:
pip3 did not exist to start (command not found) with and which pip would return /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/pip, the Apple Python.
Python 3.6 was installed via macports.
After activation of the 3.6 virtualenv I wanted to work with, which python would return somepath/venv/bin/python
Somehow pip install would do the right thing and hit my virtualenv, but pip list would rattle off Python 2.7 packages.
For Python, this is batting way beneath my expectations in terms of beginner-friendliness.
In my system, I use the update alternatives.
sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip2 2
If I want to switch between them I use the following command.
sudo update-alternatives --config pip
Note: The 1st line is enough if you have only pip3 installed and not pip2.
Related
When I run virtualenv, I get this:
$ virtualenv
-bash: /usr/local/bin/virtualenv: /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory
virtualenv only started behaving this way today. It worked yesterday. It breaks because virtualenv is a Python script using a nonexistent Python interpreter:
$ head -1 $(which virtualenv)
#!/usr/local/opt/python/bin/python2.7
On my machine, /usr/local/opt/python is a symlink to a Python 3.6 directory:
$ ls -l /usr/local/opt/python
lrwxr-xr-x 1 jim admin 24 2 Mar 13:45 /usr/local/opt/python -> ../Cellar/python/3.6.4_3
As expected, the Python 3.6 directory does not contain a bin/python2.7:
$ ls /usr/local/Cellar/python/3.6.4_3/bin/
2to3 idle pip3 pydoc3.6 python3-config python3.6m-config wheel3
2to3-3.6 idle3 pip3.6 python python3.6 pyvenv
easy_install idle3.6 pydoc python-config python3.6-config pyvenv-3.6
easy_install-3.6 pip pydoc3 python3 python3.6m wheel
virtualenv clearly expects /usr/local/opt/python to contain Python 2 material, but it only contains Python 3 material.
My /usr/local/opt/python is managed by Homebrew. I don't know the provenance of my virtualenv. How do I find out where my /usr/local/bin/virtualenv came from?
Which is to blame? My virtualenv or Homebrew?
The blame for this lies with pip, not Homebrew. My /usr/local/bin/virtualenv came from pip install virtualenv, which embeds an absolute link to the Python interpreter at installation time! I have opened an issue about this unidiomatic behavior.
Same problem on my Mac. Maybe it got broken when I updated to Mojave? Who knows.
Resolved with a brew install of Python 2:
brew install python2
This now takes over from my factory-installed Python 2.7 and gives me a new virtualenv that works:
$ which virtualenv
/usr/local/bin/virtualenv
Firstly, sorry for adding a separate comment here -- I lack the reputation to add a comment to #jameshfisher's answer.
I used homebrew to update python2 on macos to the latest version:
~ ❯❯❯ python2 --version
Python 2.7.15
Which creates/updates the python2 symlink in /usr/local/bin to link to that particular brew installed update:
~ ❯❯❯ ls -ahl =python2
lrwxr-xr-x 1 michael admin 39B 3 Jul 17:11 /usr/local/bin/python2 -> ../Cellar/python#2/2.7.15_1/bin/python2
The shebang in my /usr/local/bin/virtualenv was:
~ ❯❯❯ head -1 $(which virtualenv)
#!/usr/local/opt/python/bin/python2.7
Which did not exist:
~ ❯❯❯ ls -l /usr/local/opt/python/bin/python2.7
ls: /usr/local/opt/python/bin/python2.7: No such file or directory
So modifying the shebang to #!/usr/local/bin/python2 to use the brew installed updated python2 version was the most appropriate way to go:
~ ❯❯❯ virtualenv --version
15.1.0
✨🐟✨
A bit of a red herring for me was that I had mistakenly assumed typing which python would give me the path to my version of python2:
~ ❯❯❯ which python
/Users/michael/.pyenv/shims/python
~ ❯❯❯ which python2
/usr/local/bin/python2
~ ❯❯❯ which python3
/Users/michael/.pyenv/shims/python3
~ ❯❯❯ /Users/michael/.pyenv/shims/python --version
Python 3.7.0
I had forgotten that I had set pyenv global to python 3.7.0. Please don't fall into that trap as I did! ✨😄✨
I have fresh ubuntu 16.04 setup for production.
Initially if when i type
python --version gives me python 2.7 and python3 --version gives me python 3.5
but i want python points to python3 by default, so in my ~/.bashrc
alias python=python3 and source ~/.bashrc,
After that i install pip using sudo apt-get install python-pip and when i type pip --version it prints pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7) instead that i want packages to be installed into and get from /usr/local/lib/python3.5/dist-packages.
I have django application which is written with python3 compatible code.
Update: I want to install other packages which have to load from python3 dist-packages not just pip. I don't want to remove python 2.7 from ubuntu it will break other programs, i thought alias python=python3 would install packages into python3.5 dist-packages as well.
You need to use pip3 as the command.
pip3 install coolModule
Be sure to add to your bash profile.
alias pip3="python3 -m pip"
RedHat is my OS. By default it runs python2.6. However, I needed to install python2.7. I installed it following this post. Now, when I run:
$ python -V
Python 2.7.5
however
$ sudo python -V
Python 2.6.6
So, I incurr problems when installing libraries.
I tried to install mysql-connector by:
pip install mysql-connector-python
following the installation guide for source distribution
following the installation guide for binary distribution
However, mysql-connector module is not installed on the python 2.7 version. If I run:
$ python
>>> import mysql.connector
ImportError: No module named mysql.connector
How can I install mysql module also on python version 2.7?
EDIT
Additional information:
$ which pip
/usr/bin/pip
$ sudo which pip
/usr/bin/pip
$ which easy_install
/opt/rh/python27/root/usr/bin/easy_install
$ sudo which easy_install
/usr/bin/easy_install
/usr/bin/ $ ls -al pip
-rwxr-xr-x. 1 root root 281 Feb 7 11:07 pip
/opt/rh/python27/root/usr/bin $ ls -al easy_install
-rwxr-xr-x. 1 root root 344 Aug 16 11:40 easy_install
This is exactly why you should use virtualenv.
Please check where pip and easy_installed are installed? my guess that which pip would show pip is in the python 2.6 folder. you need to to reinstall pip and easy_install to the python 2.7 folder. you can do it with :
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python
and then check which easy_install points to the right direction (you might need to alias/symlink easy_install to the new install. to override the system easy_install) then you can just easy_install pip
Edit (after OP input):
try to easy_install pip (since the basic easy_install is python 2.7)
Update:
Since for some reason python2.7 easy install is installed as root instead of your user you can change ownership of the easy_instlal (or maybe you'll need the entire python2.7 lib. try to change as little as possible) - you can do it with the chown command:
chown username:usergroup(usually your username also) path/to/change/ownership
You should install pip in python2.7.5 environment. After that pip install will work as expected
I run
python --version
and get
Python 2.7.3
I run
pip --version
and get
pip 1.5 from /usr/local/lib/python3.2/dist-packages/pip-1.5-py3.2.egg (python 3.2)
I installed pip using apt-get. How to I get the Python 2 version of pip?
I've reinstalled python and python-pip several times with apt-get. I'm also curious why these would install different Python versions.
To install pip for Python2 on Ubuntu, this worked for me
sudo apt update
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
sudo python2 get-pip.py
It's based on DareDevil7's answer but notice the url is different.
If there are both python2.7 and python3 in you ubuntu system,run this
sudo apt install python-pip
There will be pip for python3 ,pip2 for python2
I would suggest that you use pyenv to manage multiple versions of Python, because it can often get problematic. Right now the solution to the problem would depend on the configuration you have for pip and python in your bash.
One thing you can do is download the easy_install script, and use python 3 to run it and install pip for python 3 alone.
If you really want to install pip globally for your system use the get-pip.py script with the wanted python binary http://www.pip-installer.org/en/latest/installing.html#install-or-upgrade-pip
python2.7 get-pip.py
But you should consider using virtualenv / buildout to get an isolated environment
Download the tar.gz of pip from https://pypi.python.org/pypi/pip#downloads.
Unzip or Untar, Then from its untar directory install for any specific version of python using
python2.7 setup.py install
or
python3.3 setup.py install
Run the following Commands:
sudo add-apt-repository universe
sudo apt update
curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py
sudo python get-pip.py
run this
python2.7 -m ensurepip --upgrade
Its a not a good idea to install pip for python2 system wide. I guess, you need to run a project with python2. The best solution is vritualenv.
I am making an assumption that in your installation which python2 returns /usr/bin/python2.7
1. sudo apt install python2-pip-whl
2. sudo apt install python2-setuptools-whl
3 virtualenv -p /usr/bin/python2.7 YOURPROJECT
4. source YOURPROJECT/bin/activate
The last command will now activate the virtual environment and in this environment, your python is python2.7 and pip is installed for python2.7 as well.
You can deactivate your virtual environment using deactivate.
OS X (Mavericks) has Python 2.7 stock installed. But I do all my own personal Python stuff with 3.3. I just flushed my 3.3.2 install and installed the new 3.3.3. So I need to install pyserial again. I can do it the way I've done it before, which is:
Download pyserial from pypi
untar pyserial.tgz
cd pyserial
python3 setup.py install
But I'd like to do like the cool kids do, and just do something like pip3 install pyserial. But it's not clear how I get to that point. And just that point. Not interested (unless I have to be) in virtualenv yet.
UPDATE: This is no longer necessary as of Python3.4. pip3 is installed as part of the general Python3 installation.
I ended up posting this same question on the python mailing list, and got the following answer:
# download and install setuptools
curl -O https://bootstrap.pypa.io/ez_setup.py
python3 ez_setup.py
# download and install pip
curl -O https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py
Which solved my question perfectly. After adding the following for my own:
cd /usr/local/bin
ln -s ../../../Library/Frameworks/Python.framework/Versions/3.3/bin/pip pip
So that I could run pip directly, I was able to:
# use pip to install
pip install pyserial
or:
# Don't want it?
pip uninstall pyserial
I had to go through this process myself and chose a different way that I think is better in the long run.
I installed homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
then:
brew doctor
The last step gives you some warnings and errors that you have to resolve. One of those will be to download and install the Mac OS X command-line tools.
then:
brew install python3
This gave me python3 and pip3 in my path.
pieter$ which pip3 python3
/usr/local/bin/pip3
/usr/local/bin/python3
Install Python3 on mac
1. brew install python3
2. curl https://bootstrap.pypa.io/get-pip.py | python3
3. python3
Use pip3 to install modules
1. pip3 install ipython
2. python3 -m IPython
:)
Here is my simple solution:
If you have python2 and python3 both installed in your system, the pip upgrade will point to python2 by default. Hence, we must specify the version of python(python3) and use the below command:
python3 -m pip install --upgrade pip
This command will uninstall the previously installed pip and install the new version- upgrading your pip.
This will save memory and declutter your system.
Image - How the upgrading of pip in Python3 works on MacOS
Plus:
when you install requests with python3, the command is:
pip3 install requests
not
pip install requests
brew install python3
create alias in your shell profile
eg. alias pip3="python3 -m pip" in my .zshrc
➜ ~ pip3 --version
pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)
To use Python EasyInstall (which is what I think you're wanting to use), is super easy!
sudo easy_install pip
so then with pip to install Pyserial you would do:
pip install pyserial
On Mac OS X Mojave python stands for python of version 2.7 and python3 for python of version 3. The same is pip and pip3. So, to upgrade pip for python 3 do this:
~$ sudo pip3 install --upgrade pip
Also, it's worth to mention that Max OSX/macOS users can just use Homebrew to install pip3.
$> brew update
$> brew install python3
$> pip3 --version
pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)
On MacOS 10.12
download pip: pip as get-pip.py
download python3: python3
install python3
open terminal: python3 get-pip.py
pip3 is available
pip is installed automatically with python2 using brew:
brew install python3
pip3 --version
simply run following on terminal if you don't have pip installed on your mac.
sudo easy_install pip
download python 3 here: python3
once you're done with these 2 steps, make sure to run the following to verify whether you've installed them successfully.
python3 --version
pip3 --version
For a fresh new Mac, you need to follow below steps:-
Make sure you have installed Xcode
sudo easy_install pip
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew doctor
brew doctor
brew install python3
And you are done, just type python3 on terminal and you will see python 3 installed.
I had the same problem with python3 and pip3. Decision: solving all conflicts with links and other stuff when do
brew doctor
After that
brew reinstall python3
I'm using MacOS 11.6 and in my case I did two things;
Install Python 3 from https://www.python.org/downloads/macos/ clicking on first link Download macOS 64-bit universal2 installer from Stable Releases list
Create alias for Python3 running the following command on Terminal echo "alias python='/usr/bin/python3'" > ~/.bash_profile
After that open a new terminal and type python -V to check if the version of python 3 is enabled by default for your use so now you can use the command pip3 to install your packages like pip3 install somepackage
use port to install it, it is better than brew, in case you have an old machine.
sudo port install py38-pip