How to install pip associated to different versions of Python - python

Currently, I am running two versions of python on Mac. The native one (2.7.10) (/usr/bin/python), and another one, which has been downloaded via home-brew (2.7.14).
I want to download two versions of pip and download packages depending on the python version I want to use.
Is this possible?

Start by checking your available Python interpreters on your command line:
[root#server ~]# ls /usr/bin/ | grep python
python2 -> python2.6
python2.6
python3 -> python3.4
python3.4
Then download and run this file using each interpreter
[root#server ~]# python2.6 get-pip.py
[root#server ~]# python3.4 get-pip.py
Then once both Python interpreters have thepip module installed, you can install packages to your specific Python interpreters using the following commands:
[root#server ~]# python2.6 -m pip install <module>
[root#server ~]# python3.4 -m pip install <module>

It's worth mentioning (for Windows Users), once you have multiple versions of Python installed, you can easily manage packages for each specific version by calling pip<major>.<minor> from a cmd window.
for example, I have Python 2.7, 3.6 and 3.7 currently installed, and I can manage my packages for each installation using pip2.7, pip3.6 and pip3.7 respectively ...
On Windows 10, $ pip3.7 install <module> works for me - haven't tested it with venv instances yet though

Related

Python and pip installation on Mac don't show version?

On Mac OS.
Downloaded the Python from their website.
Python -V return Python 2.7.16, and
python3 -V return Python 3.9.4
Installed pip with : python3 get-pip.py, got Successfully installed pip-21.0.1
But when I run pip -V
I get File "/usr/local/bin/pip", line 1.... SyntaxError: invalid syntax
After reading here a lot, i could not understand (in simple words for dumbs) :
How could you "alias" or update python to show/run in version 3+ ?
Why I can't get the pip version if it's installed ?
Use pip as a module instead
% python3 -m pip --version
pip 21.0.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
Anyone who's interested , what i had to do is to
CD the project on terminal
run python3 -m venv env (this create virtual environment of python3 inside project)
run source env/bin/activate activate it.
now pip --version or python --version will return the right results.
From my basic knowledge i understand that the mac is using python2 which we don't want to interrupt, so we install python3 and create a 'virtual environment' for it inside our project folder, once its in, we can install all other things.
to deactivate (stop the virtual env) we run deactivate

How do I deal with pip in different python versions?

I had python 3.8 installed and then I installed python 2.7. I am trying to run a python program with py -2 program.py in vs code using with python 2.7 as selected environment and I am getting an error, ImportError: No module named googlemaps even though I have already installed.
If I run the program using Python3 then it would run fine. Also when I open vs code using python 2.7 as selected runtime environment then I would get a warning Linter Pylint is not installed. If I click on install then I would get another warning There's no Pip installer available in the selected environment.
Also even though I have changed the python path from 3.7 to 2.7, default python version will still show up as 3.7 when I runPython in command line.
Things that I have tried to install the googlemaps module for python 2 after googling for solutions,
pip2 install googlemaps--upgrade
py -2 -m pip install googlemaps
If you have your python2 binary located, you can just call it directly:
/usr/bin/python2 -m pip install googlemaps
And if you're not sure where your python binary is, you can use
import sys
print(sys.executable)
to locate it.
And if you don't have pip, you should install it by downloading this file:
https://bootstrap.pypa.io/get-pip.py
then running:
/usr/bin/python2 get-pip.py
It is recommended to install Python 3.8 using Pyenv and also when you are using different versions of python, it is very useful
curl https://pyenv.run | bash
pyenv install 3.8.1
pyenv virtualenv 3.8.1 venv
pyenv local venv
with pyenv local you set your version for use.
If after this you run
pyenv version
It will output to 3.8.1
With regards to pip installation, run
whereis python
and if it outputs to
usr/bin/python2
then you can use pip for installing python2 packages and pip3 for packages compatible to python3.

python3 loading dist-packages from python2 on ubuntu

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"

Multiple Python leads to installation of boto at wrong location

I am on Centos7. I have multiple Python versions (Totally newbie in python).
One at my root inside folder name Python-2.6.6 which i installed following these steps :
wget https://www.python.org/ftp/python/2.6.6/Python-2.6.6.tgz
tar -zxvf Python-2.6.6.tgz
cd Python-2.6.6
sudo yum install gcc gcc-c++
make
make install
However there is also a Python folder at /usr/lib/python2.7/site-packages
Which i have no idea how got created .
Now i installed boto using
sudo yum -y install python-pip
sudo pip install boto
installation ended with comments
Installing collected packages: boto
Successfully installed boto-2.47.0
Now when i do python --version , I do get Python 2.6.6 which is expected
which python : /usr/local/bin/python
but when i do import boto
i get
import boto
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named boto
WHY YOU NO IMPORT?Please help
CentOs 7 is delivered with python 2.7 by default.
You installed boto with pip wich is "bind" to python 2.7, that's why you can't import boto using python 2.6. pip is bind to python2.7, cause it's the default version in CentOs 7.
You should use virtualenv. It allows you to create a python environnement with a specific python version and install the modules needed.
Example:
virtualenv -p /usr/bin/python2.7 /home/user/my_project
cd ./my_project
source bin/active
Now you're in a python virtualenv. The first command points to python2.7, but you can make it point to any python version installed (compiled, from repos etc.). Once you sourced the active file you can install modules using pip
Edit
To run a script using your virtualenv (without sourcing ./bin/active):
/home/user/my_project/bin/python /path/my_script.py
If you use this command :
source bin/activate
Then you can use pip to add a lib to the virtualenv.
Edit 2
So, you're on Centos 7, wich is provided with python 2.7. You want to use python 2.6 with a specific script.
Install python 2.6 (let's say in /usr/bin/python2.6)
Create a virtuanlenv with python 2.6 :
virtualenv -p /usr/bin/python2.6 my_venv
Enter the virtualenv
cd my_env
source bin/activate
Check the python version (should return python 2.6.x)
python
Install a module with pip :
pip install boto
boto will be installed with python 2.6, so you will be able to use your script.
If for some reason pip is not installed :
yum install python-pip

Unable to upgrade virtualenv with Python3.5 and Python2.7 on the same machine

Python 2.7 is the default version on my RHEL box. I also have Python 3.5 installed and added the following to my .bachrc file:
alias python=/usr/bin/python3.5
I run python -V and it indicates 3.5. All good.
I then run:
pip install --upgrade virtualenv
I get the following error:
No distributions at all found for virtualenv in /usr/local/lib/python2.7/site-packages
I'm wondering why 2.7 is still be referenced?
Thanks.
If you look at the contents of pip you will notice that it's just a Python script, and it has a shebang line (#!) pointing to your old python. Try this
cat $(which pip)
You probably have a pip3.5 program, and you can alias it like this
alias pip=/usr/bin/pip3.5
Otherwise you can always run pip like this
python3.5 -m pip install <package>

Categories

Resources