Installing Python 2.x and python 3.x on the same computer - python

There are python 2.7 and python 3.2 on my computer. The default version is 2.7 because using python -V gives 2.7 as the version.
But when I use apt-get install numpy, scipy,pip why it install them into the python3.2 folder. After that I used pip to install the module into the 3.2 folder.
I also installed Theano this way but in the end it showed a message saying that there is no module named Theano installed although it is in the python 3.2 folder.

You haven't told on which OS you're running this, but it look likes a debian base linux, maybe ubuntu?
If so, I'd try with:
sudo apt-get install python3-numpy or
sudo apt-get install python2-numpy.
This would also work with python-pip2 and python-pip3.
After this, you could effectively use "pip2" or "pip3" to install your packages without having to go through the OS "prebuild" modules (but the os version of the packages are usually my prefered way to install them, if the exists in the repo)

Depending on what you're doing with python, it's often a good idea to run in a virtual environment, this lets you have several different versions of python with several different sets of installed packages on the same system. . .
See http://docs.python-guide.org/en/latest/dev/virtualenvs/ for the details.

You can also use Anaconda for maintaining two versions of Python. Anaconda consists of various libraries so you don't have to install them and after switching it to the different version of Python you can easily install them :
Download Anaconda for both Python versions
Open .bashrc
Add the path to new Anaconda you have installed for, e.g.:
export PATH="/home/paras/anaconda3/bin:$PATH"
Now there will be 2 export paths: one for Python 2 and one for Python 3. Comment the one which you don't want.

First install different versions of python or whichever python version you would like to use
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.6 10
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 20
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.3 30
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.4 40
Now create virtualenv like this and give the path of python version you want to use inside the virtualenv.
virtualenv -p /usr/bin/python3.6 <foldername>
virtualenv -p /usr/bin/python3.4 <foldername>

Related

Python Multiple version installation in Linux rocky

I am new to Python and Linux env, so a little confused.
I want to find where my Python3.9 is installed, so that I can update the PATH..
Running python --version or python3 --version give me Python 3.6.8
I want to install Python 3.9, so when I run sudo dnf install -y python39 it gives me
Package python39-3.9.12-1ep1.el8.x86_64 is already installed.
ls /usr/bin/python* doesn't show 3.9
Output of above command - /usr/bin/python /usr/bin/python3 /usr/bin/python3.6
/usr/bin/python3.6m
Also tried alternatives --config python but it shows only 3.6
-----------------------------------------------
* 1 /usr/libexec/no-python
+ 2 /usr/bin/python3
which python3.6 gives /usr/bin/python3.6
which python3.9 gives /usr/bin/which: no python3.9 in....
rpm -ql python39-3.9.12-1ep1.el8.x86_64 gives me
/etc/gdbinit.d/python39.gdb. /opt/python3.9.
/opt/python3.9/bin
I could be missing something obvious, if someone can point me in the right direction it will be awesome.
what happens when you enter the following command: python3 --version and is it any different compared to running python --version?
I am not sure what is your actual requirement is. But if you have already installed python 3.9 in your machine, then you can have this and all previous versions applied to different projects. This is selected when you create virtual environments. For example if you want to run a project that built on 3.6, then you can select python interpreter 3.6 at the time of creation.
So the virtual machine I was working was built using vagrant, it had python 3.6 in usr/bin and python 3.9 in opt/python3.9. Newbie like me who wanna understand what /opt is - https://www.baeldung.com/linux/opt-directory
To search any package used command - rpm -ql python39-3.9.12-1ep1.el8.x86_64
Once package is found add it to path as symlinking binaries can be confusing
vim ~/.bashrc
PATH=/opt/python3.9/bin

How to install brownie on mac os running both python 2.7 and 3.9 [duplicate]

I am trying to get python 3 working on my OSX laptop.
I need to install requests for python 3, and it isn't working.
I think I've managed to get pip installed for both python 2.7 & python 3 however...
Whenever I use 'pip' it points to python2... I can't seem to access the pip for python 3?
In all likelihood, pip3 will be installed pointing to your Python 3 installation, so your use case is probably solvable by just switching from:
$ pip install foo
to:
$ pip3 install foo # Or pip3.7 install foo if you need to disambiguate further
That said, it can get kind of complicated when you have many different Python installs, where pip/pip3 might have been installed pointing to a Python version that doesn't correspond to the python/python3 you're using, which can be quite confusing.
If you know python & python3 are the correct executable, just use it to invoke pip on your behalf. It's fairly easy too, just check your version to be sure it's the one you expect (e.g. on my system):
$ python --version
Python 2.7.15rc1
$ python3 --version
Python 3.6.6
then use the appropriate one with -mpip, a flag to run an installed module/package via the chosen Python as the "main" executable, bypassing the need for specifically versioned pip executable entirely. So if you wanted to install foo for Python 3.6 on my machine, you'd run:
$ python3 -mpip install foo
This is especially useful on Windows, where the pip executables often either don't exist, or are not installed in the PATH, so it's irritating to use them. Instead, use the Windows launcher that comes with any modern Python 3 version (but manages all Python versions on the machine), and is used to disambiguate among various versions. For example:
C:\>; Installs foo for latest installed version of Python 3
C:\>py -3 -mpip install foo
C:\>; Installs foo for latest installed version of Python 2
C:\>py -2 -mpip install foo
C:\>; Installs foo for latest installed version of Python 3.6
C:\>py -3.6 -mpip install foo
Essentially, any use of pip can be replaced by executing the Python interpreter directly with the -mpip option to run the pip package as the "main" executable.
This trick applies to many other tools with dedicated launchers that are often not installed in the PATH, particularly on Windows, and it makes updates easier too; my Windows shortcut for launching ipython3 never used a hardcoded path to the launcher (e.g. C:\Program Files\Python36\Scripts\ipython3.exe), instead using %WINDIR%\py.exe -3 -mIPython. In addition to being more portable (the shortcut "just works" on any Windows system with a semi-recent Python 3 install), it's self-updating; when I upgraded from 3.6 to 3.7, the shortcut didn't have to change at all (I had to run py -3 -mpip install ipython again to get IPython reinstalled, but once I'd done that, the shortcut seamlessly began referring to the 3.7 install with no changes needed).
Run this command to find the python that is used before running pip: which python. You can do the same idea to find which pip version is being run: which pip
You’ll need to create separate virtual environments in order to use different python versions and/or python dependencies. Use something like conda or venv to do this. Then, ensure that the desired python version virtual environment is activated prior to installing a new package with pip.
To install requests for python3, use pip3 install requests which is the pip installer for Python 3 modules.
This guide has some further info on getting Python 3 working on a mac.
https://docs.python-guide.org/starting/install3/osx/
try to sudo apt-get update first then sudo apt-get install python3-pip --fix-missing

How to downgrade ubuntu to python 3.6?

I'm trying to install an older version of tensorflow and it needs python3.6 to support the whl file of installation.
I'm now running Ubuntu 20.04 with python 3.8.5, I've already done this :
sudo add-apt-repository ppa:deadsnakes/ppa
followed by :
sudo apt-get update
sudo apt-get install python3.6
By doing that it installs python 3.6 but when I see the version of python installed it's still 3.8.5. Should I do something to remove python3.8.5 ? maybe apt-get purge ?
P.S: I'm installing the wheel file through pip3 should I downgrade it too ?
You shouldn't hack the system installation of Python I'd say.
Instead you should use something that let's you manage multiple versions of it, something like pyenv.
It is a well-known and widely accepted utility and according to its readme:
pyenv lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.
You can find the installation instructions here: https://github.com/pyenv/pyenv#basic-github-checkout.
Once you installed it you can install your preferred Python distribution which will live besides your system one and all the others you want later.
E.g. if you want to install 3.6.7 and then use it globally you can do:
pyenv install 3.6.7
pyenv global 3.6.7
Then test it like:
python -V
And that's only to scratch the surface, you can do many more things with it, check out the documentation for more.
after the commands you've run, you should have a python3.6 binary installed on the path
that said, I'd recommend using virtualenvs instead of system installations
for deadsnakes, you can install the venv module by (sudo) apt install python3.6-venv (debian decided to split venv into a separate module, so the deadsnakes packaging follows that)
from there you can create and activate a virtualenv:
python3.6 -m venv venv # create the environment
. venv/bin/activate # activate the environment
pip install ... # install things to your isolated environment
inside this virtualenv the python command will refer to your own isolated python installation
disclaimer: I'm the maintainer of deadsnakes

Install python 2.6 in a sandbox/virt-env

I have python 2.7 running on ubuntu 14.04. and, I need to set up py2.6 in a sandbox environment. I tried using the command, virtualenv as
virtualenv /path/to/sandbox --no-site-packages
But, it copies /usr/bin/python2.7 binary file into the sandbox's bin folder.
Using pythonbrew also didn't work, as it throws compilation errors almost always.
How to create a sandbox environment and install python2.6 binary in it?
Virtualenv won't really install a new python version from scratch, but rather copy one of the versions installed on your system. That's why you first need to get a python2.6 binary for Ubuntu 14.04. It seems they don't officially support python2.6 anymore, so either you manually download and install it from http://python.org or use a ppa like this:
sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python2.6
afterwards you can tell virtualenv to use python2.6 like this:
virtualenv -p python2.6 --no-site-packages /path/to/sandbox

How to use pip with Python 3.x alongside Python 2.x

I installed Python 3.x (besides Python 2.x on Ubuntu) and slowly started to pair modules I use in Python 2.x.
So I wonder, what approach should I take to make my life easy by using pip for both Python 2.x and Python 3.x?
The approach you should take is to install pip for Python 3.2.
You do this in the following way:
$ curl -O https://bootstrap.pypa.io/get-pip.py
$ sudo python3.2 get-pip.py
Then, you can install things for Python 3.2 with pip-3.2, and install things for Python 2-7 with pip-2.7. The pip command will end up pointing to one of these, but I'm not sure which, so you will have to check.
What you can also do is to use apt-get:
apt-get install python3-pip
In my experience this works pretty fluent too, plus you get all the benefits from apt-get.
First, install Python 3 pip using:
sudo apt-get install python3-pip
Then, to use Python 3 pip use:
pip3 install <module-name>
For Python 2 pip use:
pip install <module-name>
The shortest way:
python3 -m pip install package
python -m pip install package
If you don't want to have to specify the version every time you use pip:
Install pip:
$ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python3
and export the path:
$ export PATH=/Library/Frameworks/Python.framework/Versions/<version number>/bin:$PATH
In Windows, first installed Python 3.7 and then Python 2.7. Then, use command prompt:
pip install python2-module-name
pip3 install python3-module-name
That's all
This worked for me on OS X: (I say this because sometimes is a pain that mac has "its own" version of every open source tool, and you cannot remove it because "its improvements" make it unique for other apple stuff to work, and if you remove it things start falling appart)
I followed the steps provided by #Lennart Regebro to get pip for python 3, nevertheless pip for python 2 was still first on the path, so... what I did is to create a symbolic link to python 3 inside /usr/bin (in deed I did the same to have my 2 pythons running in peace):
ln -s /Library/Frameworks/Python.framework/Versions/3.4/bin/pip /usr/bin/pip3
Notice that I added a 3 at the end, so basically what you have to do is to use pip3 instead of just pip.
The post is old but I hope this helps someone someday. this should theoretically work for any LINUX system.
On Suse Linux 13.2, pip calls python3, but pip2 is available to use the older python version.
To use pip for a Python 2.x environment, use this command:
py -2 -m pip install -r requirements.txt
To use pip for Python 3.x environment, use this command:
py -3 -m pip install -r requirements.txt
Please note that on msys2 I've found these commands to be helpful:
$ pacman -S python3-pip
$ pip3 install --upgrade pip
$ pip3 install --user package_name
In the general case, you will need to find out the precise location of your Python 2 and Python 3 interpreters. On Windows, the whereis command will reveal where a program is installed; on Unix and related platforms, try type or command -v (don't use the popular but nonstandard tools whereis or which unless you know you can trust them).
Windows example:
C:\loser> whereis python
C:\Python37\Python.exe
Linux example:
bash$ type python
python is /usr/local/bin/python
For instance, you might have Python 3 in /usr/local/bin/python3 and Python 2 in /usr/bin/python.
(This is by no means universal; where and how you installed the different Python versions can vary enormously. You could have Python in /opt/anaconda/forest/trees/shirley for all I know. Probably also look in /opt and in your home directory, or just use the search facilities of your OS to see what's where.)
Generally, if you find a Python binary, /path/to/this/python --version will print its version number.
Once you know this, using the full path to the version you want to use will always get you precisely that version, with its specific configuration for where to install packages.
So, in this example,
/usr/local/bin/python3 -m pip install requests
will install requests for Python 3, and
/usr/bin/python -m pip install requests
for Python 2. But again, these are just examples; you need to know the correct paths for your specific system.
On Windows (and now, going forward, other platforms) there is a wrapper called py which lets you conveniently specify which version of Python you want to run with an option. py -2 runs Python 2 (provided you have installed it, of course), and you can probably guess how to get Python 3.
By the way; on Debian and derived Linux distros, including Ubuntu, Mint, etc, pip is not necessarily installed when you apt install python3 (or python etc); you will need to separately install python3-pip (or python-pip etc).
For convenience, you might want to explore using virtual environments to control your Python environment. With Python 3, you want /path/to/your/python -m venv envname to create envname which, when activated, will always run that particular Python version when you simply run python, and the correct corresponding pip when you type that command. (Again, on Debian-based distros, you will separately need to install python3-venv.) With Python 2, you need to separately install the third-party module virtualenv. (There are other tools with similar facilities but slightly different features; but these are de facto standard, pretty much.)
If there are only a couple of versions which you want to keep on using in parallel, you can create a wrapper or alias so you don't have to type the full path every time. For example, if $HOME/bin is on your PATH, you could create a file $HOME/bin/py3 with the contents
#!/bin/sh
exec /path/to/python3 "$#"
and chmod +x this file to be able to type just py3 going forward. (The syntax for a similar wrapper on Windows will be different; in cmd you want %* instead of "$#" and no exec or #!/bin/sh shebang, and probably a # in front. There is no chmod on Windows, but the file needs to have the .cmd extension.)
Of course, if the versions you want to use already have different names, you just need to make sure their directories are on your PATH, and make sure no other programs with the same name are in a directory which is earlier on your PATH.
If you want to be able to use multiple Python versions in parallel (for example, a specific version for each project you are working on) there are third-party tools like pyenv which let you maintain a number of parallel Python installations and choose between them easily. But this is just a convenience; if you know what you are doing, you can always run a specific Python version with the instructions in this answer.

Categories