python3 loading dist-packages from python2 on ubuntu - python

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"

Related

Are pip and python consistent? Seems like the answer is no. Can someone help me decrypt the documentation?

So i'm trying to implement stripe on a Django app and i'm having issues.
I installed Stripe using pip3 -install stripe and it downloaded. However when I run the server it says
ModuleNotFoundError: No module named 'stripe'
So looking around and on this I think I found some sort of an answer.
https://nomodulenamed.com/a/I-have-installed-the-package-using-pip#fail-to-install
Are pip and python consistent?
Seems like the answer is no.
pip3 -V returned pip 20.0.2 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
and
python3 -V returned Python 3.8.2
It seems that the easy fix is using python3 -m pip3 -V but that returns No module named pip3
and
python3 -m pip -V returns pip 20.1 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
but pip -V returns zsh: command not found: pip
which leaves me quite confused
more over i'm suppose to do # install your package
python -m pip <your-package-name>
so what goes in the place of
<your-package-name>
and I'm I suppose to use pip of pip3 since I use pip3 to install thing.
Since you can have more than one Python2 installation and more than one Python3 installation available on your machine, your question is better answered by understanding virtual environments.
It is precisely the reason why virtual environments exist!
when you create a python3 virtual environment there is no need to call pip3 as it is the default pip.
start by creating your virtual env (Assuming you have virtualenv installed ... if not install it on linux ubuntu by calling
sudo apt-get install virtualenv
sudo apt-get install python3-pip
python3 -m venv env
source bin/env/activate
pip install <yourpackage>
but I believe you are on macOS since you are getting zsh error,
fix your installation by using homebrew
brew install python3
pip3 install virtualenv
virtualenv -p python3 <path-to your-project>
source <path-to your-project>/bin/activate

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>

How to install pip for a specific python version

I have my deployment system running CentOS 6.
It has by default python 2.6.6 installed. So, "which python" gives me /usr/bin/python (which is 2.6.6)
I later installed python3.5, which is invoked as python3 ("which python3" gives me /usr/local/bin/python3)
Using pip, I need to install a few packages that are specific to python3. So I did pip install using:-
"sudo yum install python-pip"
So "which pip" is /usr/bin/pip.
Now whenever I do any "pip install", it just installs it for 2.6.6. :-(
It is clear that pip installation got tied to python 2.6.6 and invoking pip later, only installs packages for 2.6.6.
How can I get around this issue?
If pip isn’t already installed, then first try to bootstrap it from the standard library:
$ python3.5 -m ensurepip --default-pip
If that still doesn’t allow you to run pip:
Securely Download get-pip.py.
Run sudo python3.5 get-pip.py.
Now you can use pip3 to install packages for python3.5. For example, try:
$ sudo pip3 install ipython # isntall IPython for python3.5
Alternatively, as long as the corresponding pip has been installed, you can use pip for a specific Python version like this:
$ python3.5 -m pip install SomePackage # specifically Python 3.5
References:
Ensure you can run pip from the command line
work with multiple versions of Python installed in parallel?
I have python 3.6 and 3.8 on my Ubuntu 18.04 WSL machine. Running
sudo apt-get install python3-pip
pip3 install my_package_name
kept installing packages into Python 3.6 dist directories. The only way that I could install packages for Python 3.8 was:
python3.8 -m pip install my_package_name
That installed appropriate package into the Python 3.8 dist package directory so that when I ran my code with python3.8, the required package was available.
Example of how to install pip for a specific python version
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
/opt/local/bin/python2.7 get-pip.py
Script is from official doc: https://pip.pypa.io/en/stable/installing/
On Ubuntu 18.04.1 LTS I wanted to install pip for my second python version (python3) and the following command did the trick for me:
$ sudo apt install python3-pip

How to install django for python 3.3

I had python 2.6.1 because it was old I decide to install python 3.3.2 but, when I type "python" in my mac it prints it is version 2.6.1 and when I type python3 it shows that this is 3.3.2. I installed django 1.6 but when I check, understand that it is installed for old version of python (python 2.6.1). I want to instal it for my python 3.3.2 what should I do? any way to uninstall python 2.6.1 and when I enter python in terminal it's version be 3.3.2? I have mac os 10.6.8
You could use pip to manage the package for you. If pip is not installed.
use
sudo easy_install pip
to install it. Then
pip3 install django
would install django for your python3.
Use python3 to call python version 3 on the command line.
For projects you can use virtual environments:
$ python # this will be version 2
$ python3 -m venv myenv
$ source myenv/bin/activate
$ python # this will be version 3
where myenv will be the folder that hosts the libraries and binaries for that virtual environment. It will automcatically use the python version that was used to initialize this venv - in this case python3. This has the effect that once you activate the venv you can use python there and it will be version 3.
to setup django in pyton3
sudo apt-get install python3-pip
sudo pip3 install virtualenv
virtualenv -p /usr/bin/python3.X venv
source venv/bin/activate
congrats you have setup python3 django and now have many packages to work with
Note: X stands for the version of python
To install django for python3, you need to use pip3 instead of pip.
python defaults to python2.
pip defaults to pip for python2.
So, when you install pip using whatever package manager you have, you are essentially installing pip for python2.
To remove Python2: $sudo apt remove python
To install pip for python3:
$sudo apt install python3-pip
To install pip for python2:
$sudo apt install python-pip
Note: I am using apt package manager. You should use the package manager for your OS.

Installing pip with correct python version

I am on shared hosting and I need to install pip with the correct python version, 2.7. To install pip, I did:
$ easy_install pip
However, after it was installed I get the following:
[dave#web1 lib]$ pip --version
pip 1.0.2 from /home/premiere/dave/financials/lib/pip-1.0.2-py2.7.egg (python 2.4)
How would I re-install pip to work on the python2.7 version, which is also installed on the machine?
[premiered#web1 ~]$ python --version
Python 2.6.6
Which is strange, since it is installing to python2.4.
You may want to create a virtualenv using -p /path/to/python-2.7.binary param, and then activate it. Then all stuff you installed using pip would be correctly into your virtualenv.
If multiple versions of python are installed on the system, then you should invoke the version you want when installing. i.e.
$ python27 easy_install pip
This creates a pip file in your path that contains the specified version of python in the hashBang line.

Categories

Resources