Pip for python2 while python3 pip exists - python

I have both python2 and python3 in my system. But when I try :
python -m pip install sklearn
bash shows :
/usr/bin/python: No module named pip
And
pip --version
Yields :
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
Python versions are Python 2.7.18 and Python 3.8.2.
How to correct this error by having two pip versions?
This is a similar question, but did not solve the issue.
OS is Ubuntu.

Thanks to HK boy
The following works, it installs pip explicitly for the python version specified :
curl -O https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
I already had pip3 before hand, so to install packages for python3, pip3 install <package> is enough.

Related

Python not using proper pip

I'm running CentOS 8 that came with native Python 3.6.8. I needed Python 3.7 so I installed Python 3.7.0 from sources. Now, python command is unknown to the system, while commands python3 and python3.7 both use Python 3.7.
All good until now, but I can't seem to get pip working.
Command pip returns command not found, while python3 -m pip, python3.7 -m pip, python3 -m pip3, and python3.7 -m pip3 return No module named pip. Only pip command that works is pip3.
Now whatever package I install via pip3 does not seem to install properly. Example given, pip3 install tornado returns Requirement already satisfied, but when I try to import tornado in Python 3.7 I get ModuleNotFoundError: No module named 'tornado'. Not the same thing can be said when I try to import it in Python 3.6, which works flawlessly. From this, I understand that my pip only works with Python 3.6, and not with 3.7.
Please tell me how can I use pip with Python 3.7, thank you.
It looks like your python3.7 does not have pip.
Install pip for your specific python by running python3.7 -m easy_install pip.
Then, install packages by python3.7 -m pip install <package_name>
Another option is to create a virtual environment from your python3.7. The venv brings pip into it by default.
You create venv by python3.7 -m venv <venv_name>
I think the packages you install will be installed for the previous version of Python. I think you should update the native OS Python like this:
Install the python3.7 package using apt-get
sudo apt-get install python 3.7
Add python3.6 & python3.7 to update-alternatives:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
Update python3 to point to Python 3.7:
`sudo update-alternatives --config python3
Test the version:
python3 -V

Pip3 trying to install in wrong python directory

I recently installed Fedora 26, and it came with python3.6 already installed. However, the application I plan on running uses python3.4 instead. I want to continue using python3.4 so I installed it using dnf. So when I type pip3 -V to se what version of pip3 I'm using, I get this: pip 9.0.1 from /usr/lib/python3.6/site-packages (python 3.6). How would I make it so that pip3 installs in pip 9.0.1 from /usr/lib/python3.4/site-packages (python 3.4) instead of python3.6?
Here are some things I have tried.
When running this command python3.4 -m pip, I get this error: /usr/bin/python3.4: No module named pip. When I tried to reinstall pip, it reinstalls pip-3.6. Also, I noticed that I don't have the pip-3.4 package.
Any help is greatly appreciated.
Download this script
Open your terminal and execute it using the command
[root#server ~]# python3.4 get-pip.py
This should install pip to your Python 3.4 installation
Then you can access your pip package manager for your Python 3.4 installation using
[root#server ~]# python3.4 -m pip install <module>
Output on my computer:
>> ekavala#elx750xhv:~$ python3.4 --version
> Python 3.4.3
>> ekavala#elx750xhv:~$ python3.4 -m pip -V
> pip 9.0.1 from /usr/local/lib/python3.4/dist-packages (python 3.4)

Install python package if the pip will install too?

I download the python3 package , and install it, and I find the pip3 is installed too.
aircraftdeMacBook-Pro:~ ldl$ which python3
aircraftdeMacBook-Pro:~ ldl$ which pip3
....there I install the .dmg for python3 I download from the upper link.
aircraftdeMacBook-Pro:~ ldl$ which python3
/usr/local/bin/python3
aircraftdeMacBook-Pro:~ ldl$ python3 --version
Python 3.5.2
aircraftdeMacBook-Pro:~ ldl$ which pip3
/usr/local/bin/pip3
So , I want to confirm if the pip3 package is include in the .dmg file, because when install the python3 the pip3 is installed too.
from the command lines you can see I only instal the python3 dmg, but the pip3 is installed too.
This is as expected.
Newer releases of Python 3 (3.4+?) come with pip pre-installed, as well as with the ensurepip module.

pip and pip3 - both pointing to python3.5?

I started to experiment with shade today; and installed it for both python2 and python3 on my ubuntu 16.04 system; using:
sudo pip install shade
respectively
sudo pip3 install shade
Both commands passed; I didn't really pay attention.
Then I tried to run this little test script:
from shade import *
simple_logging(debug=True)
conn = openstack_cloud(cloud='myopenstack')
images = conn.list_images()
for image in images:
print(image)
Using python3, I got a certificate error (which is fine, I would be rather surprised to find our internal infrastructure to use correct certificates).
But just to be sure, I wanted to run with python2.7, too; and I am told:
ImportError: No module named shade
So, I had a closer look what pip and pip3 have to say:
> pip -V
pip 9.0.1 from /usr/local/lib/python3.5/dist-packages (python 3.5)
> pip3 -V
pip 9.0.1 from /usr/local/lib/python3.5/dist-packages (python 3.5)
It looks like both pip and pip3 are actually working on my python3 installation, but when I do:
> python --version
Python 2.7.12
> python3 --version
Python 3.5.2
Any idea, anybody? What could be causing this, or how to actually install shade for python2/pip?
As requested:
> for i in pip pip3 python python3 ; do type $i ; done
pip is /usr/local/bin/pip
pip3 is /usr/local/bin/pip3
python is /usr/bin/python
python3 is /usr/bin/python3
pip3 looks like the default option pip uses. Try using pip2 instead to explicitly install a Python 2 package.
Just change the first line of the /usr/local/bin/pip to:
#!/usr/bin/python
and the first line of /usr/local/bin/pip3 to:
#!/usr/bin/python3
And then it will act normally:
> pip -V
pip 9.0.1 from /usr/local/lib/python2.7/dist-packages (python 2.7)
> pip3 -V
pip 9.0.1 from /usr/local/lib/python3.5/dist-packages (python 3.5)
python -m pip
This will use the version of pip that python can see.
Your local install of python3's pip in /usr/local/bin/ shadows the system install of pip in /usr/bin. In order to use the system's default pip, you can:
Specify the path explicitly: sudo /usr/bin/pip install shade
Delete the local install of pip: sudo rm /usr/local/bin/pip
Change your $PATH to prefer /usr/bin to /usr/local/bin
Use the pip2 alias from /usr/bin: sudo pip2 install shade
Install python3 from the system repositories: sudo apt-get install python3. Don't forget to delete your local python3 install if you go this route.
I ran into this problem (where pip got associated with Python 3) and it's due to the pip installer updating pip to point to Python 3. I think this is very confusing behavior because historically everyone has associated pip with Python 2 and pip3 with Python 3. We got around this by doing the following:
# install pip
curl https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py
sudo python3 /tmp/get-pip.py
sudo python /tmp/get-pip.py # install the python2 version last so the pip binary is not overwritten with the python3 version
rm /tmp/get-pip.py
The key here is installing the Python 2 pip after installing the Python 3 pip.

how to use python2.7 pip instead of default pip

I just installed python 2.7 and also pip to the 2.7 site package.
When I get the version with:
pip -V
It shows:
pip 1.3.1 from /usr/lib/python2.6/site-packages (python 2.6)
How do I use the 2.7 version of pip located at:
/usr/local/lib/python2.7/site-packages
There should be a binary called "pip2.7" installed at some location included within your $PATH variable.
You can find that out by typing
which pip2.7
This should print something like '/usr/local/bin/pip2.7' to your stdout. If it does not print anything like this, it is not installed. In that case, install it by running
$ wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
$ sudo python2.7 get-pip.py
Now, you should be all set, and
which pip2.7
should return the correct output.
An alternative is to call the pip module by using python2.7, as below:
python2.7 -m pip <commands>
For example, you could run python2.7 -m pip install <package> to install your favorite python modules. Here is a reference: https://stackoverflow.com/a/50017310/4256346.
In case the pip module has not yet been installed for this version of python, you can run the following:
python2.7 -m ensurepip
Running this command will "bootstrap the pip installer". Note that running this may require administrative privileges (i.e. sudo). Here is a reference: https://docs.python.org/2.7/library/ensurepip.html and another reference https://stackoverflow.com/a/46631019/4256346.
as noted here, this is what worked best for me:
sudo apt-get install python3 python3-pip python3-setuptools
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
pip has now dropped support for python2, therefore you can't use python2 pip
You can't find python2-pip in apt-get anymore, and you won't get pip when installing python2 from source
You can still install python modules using apt-get. To install a python prepend ‘python-’ to the module name
apt-get install python-six # install six

Categories