Package management for coexisting Python 2 and 3 - python

I have been using Python 2.7 for a while now and installing packages using pip install without any issue. I just started using python 3 for a certain code and realized how confusing having different versions of Python can get.
I have Fedora 25, the default Python version is 2.7.13 and the default Python 3 version is Python 3.5.3,
I want to be able to use python 2.7 and python 3, my general question is:
What are the best practices when installing packages for both Python 2 and Python 3 on one machine?
As I mentioned using pip install in Python 2.7 works fine, but what about Python 3? I can:
use pip3 install
use python3 -m pip install
Which one should I use and how does it affect the python 2 version of the module? pip3 is not installed on Fedora 25, which raises a new question: how should I install it? as I understand I can:
use dnf install python3-pip (it is unclear if that actually works when pip for Python 2.7 is installed)
use python3 get-pip.py
Finally, would it be a good idea to create a Python 2 and a Python 3 virtual environment to address this issue?
From what I have read on the internet there does not seem to be a clear consensus on these questions, I hope this thread will clarify.

pip3 install and python3 -m pip install — both work perfectly and don't have any impact on Python 2. You can have as many Pythons in your system as you want; I for one have Python 2.7, 3.4, 3.5 and 3.6. To distinguish different versions of pip I use versioned names: pip3.4 install.
And of course I use virtual environments and virtualenvwrapper quite intensively.

Related

Modules installed but not found by Python in Raspberry Pi 3

I have a Raspberry Pi 3 with Raspbian and I upgraded python version from 3.7 to 3.8. If I type python --version in the terminal the correct version appears as the system version. However none of the modules that I have installed AFTER the version change seem to work. Python gives ModuleNotFoundError when trying to import ANY of the modules that I have installed.
I can see the modules with pip freeze but Python seems to not be able to find them.
I followed this instructions to purge 3.7. I reinstalled pip after purging python 3.7 but pip as again installed in /home/pi/.local/lib/python3.7/site-packages/pip. How can I get rid of 3.7 completely?
python3.8 -m pip install SomePackage # specifically Python 3.8 should work.
More documentation here: https://docs.python.org/3/installing/index.html#work-with-multiple-versions-of-python-installed-in-parallel
Just to summarize the comments and suggestions from other answers:
The problem I have was caused by the fact that even I had set Python 3.8 as default and python -v was pointing to Python 3.8 the pip script was installing modules for Python 3.7.
The suggested solution was to use pip3.8 (or whatever version someone might have) to install packages for that equivalent Python version and that works good.
Ideally best option if someone wants to have multiple versions of python is to use pyenv. You can create multiple virtual environments with multiple python versions.
However Do not uninstall the default Python. I have also tried to uninstall the default Python 3.7 to avoid having two versions of python 3 and keeping track of which module is installed where. This was a bad idea. I did not know that many Linux distributions have applications which use the default Python. You might get a black screen and who knows what other problems see this discussion Removed Python 3 on 18.04, how can I fix my system?

Is there a solution to why I can't pip install any python modules past 2.7?

I can't install any python modules that require python 2.7 or later. I have uninstalled everything that Mac would let me that was related to python 2, and I run everything on python 3. I am completely lost. I am on Mac and whenever I try to install a module(like praw) this pops up.
I used the command
pip install praw
Collecting praw
Using cached https://files.pythonhosted.org/packages/41/89/94c1ec81a05536e2c2a1dc2e8f5402c8ad65963f28948bf41c64621e238b/praw-6.5.0-py2-none-any.whl
ERROR: Package 'praw' requires a different Python: 2.7.16 not in '>=3.5' ```
Welcome to Stack Overflow Malachi! I believe you're looking for the command pip3 (pip3 install praw) to install Python 3 packages through pip. If you open your terminal and type man pip to get the manual page, there is a line that says:
pip is the command to use when installing packages for Python 2, while pip3 is the command to use when installing packages for Python 3.
Looks like the most recent version of praw requires Python 3.5 or greater.
The last version that supported Python 2.7 was praw 5.4.0. If you're still using Python 2.7 and need this package, try running
pip install praw==5.4.0
If you're using Python 3.x, check to see if you're using the right version of pip. If you have both Python 2 and Python 3 installed on your system, you'll likely need to install system wide packages using pip3 instead if pip. In this case, try running
pip3 install praw
Alternatively, if you're targeting a specific Python interpreter (e.g. python3.7, python3.8, etc), and want to be certain that you are using the correct pip executable for your interpreter, you can run pip as a Python package via
python3 -m pip <args>
where python3 can be replaced by any interpreter path.

pip installs python library under wrong version of python

On the Linux Mint O.S. I used pip to install the CherryPy module. However it installed it under python2.7 in :
/home/jacslim/.local/lib/python2.7/site-packages/cherrypy
rather than under Python 3.6 which I found in /usr/bin/python3.6,
I tried again in the Pyton3.6 directory but the same thing happened.
Is there anyway I can direct pip to use the latest version of the Python interpreter?
Use pip3 install to install for python 3
pip is usually in the same bin directory as your python, so make sure you just specify the correct bin housing your pip installation.
I use anaconda and my Python 2.7 is installed in:
/anaconda2/bin/
To get pip installs specific to that Python, I therefore use:
/anaconda2/bin/pip [name_of_thing_to_install]

About different python versions installation

Is it possible to install 3 different python versions on windows 10 simultaneously? I'm using 2.7 for Udacity course, 3.6 for my college project and now I need to install Python 3.5 for "Tensorflow" package. Is it possible to have? Or is there any way to install tensorflow on python 3.6? Any suggestions will be appreciated.
Yes you can have multiple version of Python installed. They just have to be added in your system PATH.
Pay attention that if you let all executables called "python", the latest in your path will be used. You can check this with the command
python --version
For Tensorflow, you can install it on Python 3.6. In my case I used Anaconda 4.4 with Python 3.6 but you can do it by your own.
NB : For tensorflow, pay attention to install CUDA8.0 and add CUDNN version 5.1 (not the version 6.0)
In general you can use virtualenv. Specifically for working with Tensorflow I'd suggest building an env with Anaconda.
Yes just call it directly.
I use python 2, python 3 and pypy.
Call the binary directly - e.g. c:\python27\python.exe myfile.py
When installing packages you can use this as well e.g.
c:\python27\python.exe -m pip install pillow
Alternatively - or in parallel, you can use the Windows subsystem for Linux.
https://msdn.microsoft.com/en-us/commandline/wsl/install_guide
This is separate from your windows installs.
In the window's bash terminal:
python 2:
sudo apt-get install python
python 3:
sudo apt-get install python3
for python 2 pip:
sudo apt-get install python-pip
or for python 3
sudo apt-get install python3-pip
Everyone prefers different ways of using different Python versions. So what I prefer the most is to define different variables for your different Python versions and add/remove the variables in the System variable PATH to use a different Python Version.
So for example:
If you are using anaconda for Python 3, you may make a variable conda3 and add the following in it:
C:\Anaconda3;C:\Anaconda3\Library\mingw-w64\bin;C:\Anaconda3\Library\usr\bin;C:\Anaconda3\Library\bin;C:\Anaconda3\Scripts;
So of course, the values Change depending on where you have installed python.
In a similiar way you can add Python2 and depending on which Version you want to use, you May add(taking the above example as the basis) %Anaconda3% to your System variable PATH.
Note:
Even if you are adding different python variables in the System variable PATH, the System stops searching another python Version as soon as it finds the first one.
If you are using anaconda for python 3.6, I see no Problem in installing tensorflow for python 3.6 - so you can simply do:
conda install tensorflow
and that should work

Which version of Pip to use with my Python installs?

Being new to Python, I'd love to clear up a few points that I couldn't get from reading various articles and tutorials.
After using Homebrew to install Python3, I noticed that it had installed both Python3 and Python3.4. I was also a little surprised that there are now three versions of pip on my machine too; pip, pip3 and pip3.4.
I created a new virtualenv and told it to use Python3, using the following command:
virtualenv -p /usr/local/bin/python3 mysite
I was also surprised that the version of Python that it installed in my VM was 3.4.
Is it safe to have these multiple version of Python and Pip hanging around on my machine?
Am I right to assume that I should take extra care to use the matching version of pip with Python, for example, pip3.4 with Python3.4?
Yes, it is safe. Python uses this naming like python3.4, python3.5 etc to differentiate between releases. python3 is a symbolic link to the current python3.x version. Pip follows the same convention.
If you're using python3.4 explicitly, you should be using pip3.4 specifically as well. Otherwise, just use python3 and pip3. For Python 2, you can simply use python (which, unless you installed the Homebrew version as well), will be the system Python), and ditto for pip. python2.7 and pip2.7 may also work.
In general, to find out which Python version goes with which pip you're using, try:
pip --version
and you'll see the Python included in the result.
No need to worried about if you have multiple version of Python and Pip installed. just check your version by writing in terminal :
$ brew info python
or to check the version of pip write in terminal :
$ brew info pip
and make sure you have updated your both pip and python version (write in terminal $ brew upgrade pip/python)
and other way to install python is go to https://www.python.org/downloads/ and choose as your requirement, there is two version available 2.7.9 & 3.4.3 ,
after installing python write in terminal $ python -V to check its version :) Hope it will help :)

Categories

Resources