Use updated version of Python on Linux - python

I have Python 3.4 installed on my Linux computer.
sudo apt-get install python3.4
However, when I run python -V, it shows that Python 2.7.6 is being used.
How do I tell the system to use the updated version of Python?
The answer to this question for a windows computer is at How to update version of Python?, but I couldn't find an answer for Linux.

On Linux, installations of python3 installed by the package manager (e.g. apt) can be called as python3. You might need to specify the version - e.g. python3.5 if the package manager has installed more than one, or you've compiled your own installations from source.

you can specify the version in the shebang.
write #!/usr/bin/env python3.
when run via ./my_script.py it will run in python3.
otherwise run it via python3 my_script.py.
if you just want to start an interactive python shell start it with python3
you also can be more specific with the version. just replace python3 with python3.4 (if installed)

In ubuntu various python executable are places under /usr/bin/ and might look like
/usr/bin/python
/usr/bin/python3.2
/usr/bin/python3.4
etc. so when you execute a command python -v it looks for a file with that name in that location. so to choose your version specify it like python3.4 -v
you can replace the simlink /usr/lib/python with /usr/bin/python3.4 to make that "default"

Related

I don't have python but I have python3

I recently formatted my laptop and installed the Ubuntu operating system. I did not explicitly install Python. To check if it is pre-installed, I ran python --version in the terminal and got this:
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
Then I checked python3 --version and got this: Python 3.6.9.
How is this possible? Should I install python as well? Or can I continue to use python3? Would this cause any problems?
Recent Ubuntu versions do not install python 2 by default, as python 2 is now "dead". However, the command python is not (yet) linked to python 3, to avoid some confusion; traditionally, python was for python 2 while python3 was for python 3.
In case you want to change this behavior and use python to run python 3, try installing a package named 'python-is-python3'. Then you can run python --version and see that it points to python 3.
In theory it might cause confusion for some legacy programs, but I haven't seen any issue so far for about a year.
In 20.04 LTS, the python included in the base system is Python 3.8. Python 2.7 has been moved to universe and is not included by default in any new installs.
Remaining packages in Ubuntu which require Python 2.7 have been updated to use /usr/bin/python2 as their interpreter, and /usr/bin/python is not present by default on any new installs. On systems upgraded from previous releases, /usr/bin/python will continue to point to python2 for compatibility. Users who require /usr/bin/python for compatibility on newly-installed systems are encouraged to install the python-is-python3 package, for a /usr/bin/python pointing to python3 instead.
— https://wiki.ubuntu.com/FocalFossa/ReleaseNotes
Just python on Linux traditionally refers to Python 2. python3 is the correct command if you want to use Python 3 (which you should, as Python 2 is EOL).
TL;DR: Just use python3
Python on Ubuntu refers to Python v2. If you were to run
python --version
you would see an output in the format of
Python 2.x.y
Python 2 has been sunset, which means it no longer will be updated and the recommendation is to move to Python3.
To run a program with Python 3, you just need to use python3 command instead.
python3 ./app.py

Use python 3.8 instead of 3.6 on elementary OS (or Ubuntu)

I recently started using elementary OS. It's based on Ubuntu.
During installation it installs python 3.6.
I installed python 3.8 manually by installing the following packages: python3.8, python3.8-dev, python3.8-minimal, python3.8-venv.
I also updated the link to the python binary with:
sudo ln -sf /usr/bin/python3.8 /usr/bin/python3
After this a couple of things stopped working.
For example when I tried to execute a non-existing command it didn't print the error message that it cannot found the command, but it displayed a python stack trace. That one I solved with:
cd /usr/lib/python3/dist-packages
sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so
sudo ln -s apt_inst.cpython-36m-x86_64-linux-gnu.so apt_inst.so
As you can see the python error was because it couldn't find the compiled binaries for the apt module.
So this issue was solved, but there are a couple of similar ones, and none of them can be solved this way, as the module binary is not compatible with python 3.8.
Is it possible to remove python 3.6 completely and override it with 3.8 so that the module binaries also get updated? Or can python 3.8 and 3.6 coexist? I would be fine with the /usr/bin/python3 link pointing to python 3.6 and I would manually execute /usr/bin/python3.8 or create a different alias or link for it. However when I print out the sys.path with /usr/bin/python3.8 I get this:
/usr/lib/python38.zip
/usr/lib/python3.8
/usr/lib/python3.8/lib-dynload
/home/{username}/.local/lib/python3.8/site-packages
/usr/local/lib/python3.8/dist-packages
/usr/lib/python3/dist-packages
The trouble is with the last one. That's where the modules are with the 3.6 compatible so files. Can I somehow force python 3.8 to completely ignore the last module search path (without always stating sys.path.remove in my scripts) ?
I recommend you undo your symlink overwriting your systemwide version of Python 3. As you have found, replacing the python3 executable might create some problems, as it is used under the hood.
In general, you should leave your system python[3] installation alone for this reason and it is common to use virtual environments. This can be done as follows (assuming you are in some project directory):
python3.8 -m venv venv
source venv/bin/activate
The first command will create a virtual environment ('venv') in the directory venv. The second command will 'activate': now python (and in this case, python3 and python3.8) all refer to your original python3.8 in the context of this shell. You will have to repeat this if you launch a new shell.
This will also allow you to install packages using pip without cluttering your system installation. The use of virtual environments and pip is an incredibly common workflow in the Python development world.
In terms of shell and 'global' Python management, you can also use pyenv to manage your Python versions and what is available in the shell. pyenv is quite nice if you want to run a particular version of python, say 3.8.0 but not 3.8.1.

pytest running with another version of python

I've installed pyenv and have different versions of python installed with it:
$ pyenv versions
system
2.7.1
3.2.5
3.5.0
3.5.1
* 3.5.2
I use the following command to switch to python 3.5.2:
pyenv shell 3.5.2
And when I check the python version this is what I get:
$ python --version
Python 3.5.2
But when I run pytest, it still runs under python 2.7.6:
pytest -v
==================================================================== test session starts ====================================================================
platform linux2 -- Python 2.7.6, pytest-3.0.3, py-1.4.31, pluggy-0.4.0 -- /usr/bin/python
Why is pytest running under the older version?
Bottom line: run
python -m pytest, or
py.test-<version> if your alternative Python and pytest are installed with system package manager, or
if your alternative Python has been installed with pyenv, switch with pyenv to that version and make sure you have pytest installed for it. Then you can just run pytest.
since the pip executable is also among the ones being switched, you need to switch to the alternative Python before installing pytest for it, too.
As I can see, /usr/bin/pytest (that belongs to the system package manager's python-pytest package) has a shebang !#/usr/bin/python since it corresponds to the system python's installation.
pyenv, as its README.md says, does not replace /usr/bin/python - because it indeed should not be replaced to avoid breaking system packages.
Instead, it adds its directory to PATH and inserts a launcher there (called "shim") which is what gets invoked when you type "python". As you probably guessed, this hack is ignored by a shebang like the above - as it should.
Running python -m pytest will make whichever python that launches itself use the package from its installation.
Alternatively, pytest for your other Python version may include versioned executables on the PATH named py.test-<version> (e.g. py.test-3 or py.test-3.6) depending on the way you installed it.
If it's from a system package manager's package for nonstandard python - like python36-pytest - this is virtually guaranteed.
I checked that if you install a version with pip, it only creates an unversioned executable (though you can create a versioned one yourself). Moreover, if you install the same package for a different Python version but with the same --prefix, it will overwrite the existing one's executable!
pyenv's suggested way seems to be to install all python versions of interest and packages for them under ~/.pyenv/versions.
This is not applicable for the system's Python but the default /usr/local can be used for it.
Once you switch to an alternative Python version, it claims to create shims for all scripts (including pip!) that are currently installed for that version, so invoking those scripts without a path would run those shims.
So, if a package (and thus its script) is not installed for the alternative version but installed for system version, trying to run its executable would "fall through" to /usr/local with just the result you're seeing now.
I found this related question. For them it worked with this:
python -m pytest tests/my_test.py
I hope it works
I just wanted to run "black" for Python 2.7, but I have installed it in pyenv's Python 3.7.9. It worked correctly with the script /usr/local/bin/black27:
PYENV_VERSION=3.7.9 black -t py27 "$#"
It didn't work with:
pyenv shell 3.7.9
black -t py27 "$#"
nor
pyenv shell 3.7.9
pyenv exec black -t py27 "$#"

How do I make Python 3.5 my default version on MacOS?

I have just installed Python 3.5.1 on my Mac (running the latest version of OSX). My system came with Python 2.7 installed. When I type IDLE at the Terminal prompt my system pulls up the original Python 2.7 rather than the newly installed Python 3.5. How do I get my system to default to Python 3.5.1 when I open the IDLE window from Terminal?
Since Python 2 and 3 can happily coexist on the same system, you can easily switch between them by specifying in your commands when you want to use Python 3.
So for Idle, you need to type idle3 in the terminal in order to use it with Python 3 and idle for using it with Python 2.
Similarly, if you need to run a script or reach a python prompt from the terminal you should type python3 when you want to use Python 3 and python when you want to use Python 2.
It's good practice to have your MacOS Python environment set up properly from the beginning making sure that Homebrew installations take precedence over stock MacOS binaries. You want it in usr/local/bin not MacOS default usr/bin.
.bash_profile
# Ensure user-installed binaries take precedence
export PATH=/usr/local/bin:$PATH
# Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc
Can also create aliases for both.
alias py2='python2.7'
alias py3='python3.6'
Source the file to ensure it takes effect for the current session
source ~/.bash_profile
Homebrew install and setup etc...
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew doctor
brew update
brew upgrade --all
brew cleanup
Python3 install
brew install python3
Next
pip3 install virtualenv
Next
pip3 install virtualenvwrapper
When all is finished python3, pip3, virtualenv, and virtualenvwrapper.sh will all be in usr/local/bin.
Result
Every time I install anything or use commands like mkvirtualenv Python 3 is used by default.
You can use the python3 command (instead of using python), or you can simply uninstall the 2.7 version if you don't use it
If you dont have any python 2 scripts that you use, you can delete python2. But its not a problem to have them both installed. You just have to use another path python3 to launch IDLE.
I would prefer to let them both installled so if you have any scripts that are in python 2 you can still run them or you have to port them to python3.
You can switch to any python version in your project by creating a virtual environment.
virtualenv -p /usr/bin/python2.x (or python 3.x)
In case you just want to run a program in a specific version just open shell and enter python2.x or python3.x
Do right thing, do thing right!
Open your terminal,
input python -V, It likely shows:Python 2.7.10
input python3 -V, It likely shows:Python 3.7.2
input where python or which python, It likely shows:/usr/bin/python
input where python3 or which python3, It likely shows:
/usr/local/bin/python3
add the following line at the bottom of your PATH environment variable file in ~/.profile file or ~/.bash_profile under Bash or ~/.zshrc under zsh.
alias python='/usr/local/bin/python3'
OR
alias python=python3
input source ~/.bash_profile under Bash or source ~/.zshrc under zsh.
Quit the terminal.
Open your terminal, and input python -V, It likely shows:
Python 3.7.2
Note, the ~/.bash_profile under zsh is not that ~/.bash_profile.
The PATH environment variable under zsh instead ~/.profile (or ~/.bash_file) via ~/.zshrc.
Hope this helped you all!
By typing python, you are actually referring to a link.
You will find its location with $ which python. In my case it was /usr/local/bin/python. go there $open /usr/local/bin/ and just delete the original python, python-config and idle as they are
identical to the 2.7 files in the same folder.
Then duplicate the 3.5 files and rename them to what you just deleted.
This also changes the default link other editors like Sublime_ReplPython use and updates it therefore to the 3.5 Version. This was my major concern with the standard installation.

switch versions of python

Story:
One of the app that i have works on python 2.4 and other on 2.6. I tried to do a sym link of python2.4 to python and things started to break loose on ubuntu jaunty.
Now i am downloading every dependency of 2.4 and installing it using python2.4 setup.py install. The dependencies seem to be endless.
Question1: How will i tell any framework that go and use version so and so pf python like day django to use 2.6 and say mjango to use 2.4? Something like we say use database databasename kinda syntax.
Question2: Is there more elegant way to switch between version as my hack of symlinking was a virtual disaster?
Question3: Can I download a deb for say hardy and make jaunty believe its for her?
Use Virtualenv.
There is more information here: Working with virtualenv.
Using virtualenv you can create a new virtual python environment with whatever version of Python you want for each project or application. You can then activate the appropriate environment when you need it.
To expand on my answer:
You can install multiple versions of Python on your computer (I have 2.4, 2.5, 2.6 and 3.1 on my machine - I install each from source). I use a Mac, and keep my system Python as whatever OS X sets as the default.
I use easy_install to install packages. On ubuntu you can get easy_install like this:
sudo apt-get install python-setuptools
To install virtualenv then do:
easy_install virtualenv
I tend to create a new virtualenv for each project I'm working on and don't give it access to the global site-packages. This keeps all the packages tight together and allows me to have the specific versions of everything I need.
virtualenv -p python2.6 --no-site-packages ~/env/NEW_DJANGO_PROJECT
And then whenever I am doing anything related to this project I activate it:
source ~/env/NEW_DJANGO_PROJECT/bin/activate
If I run python now it uses this new python. If I use easy_install it installs things into my new virtual environment.
So, virtualenv should be able to solve all of your problems.
Pythonbrew is a magical tool. Which can also be called as Python version manager similar to that of RVM-Ruby version manager but Pythonbrew is inspired by Perlbrew.
Pythonbrew is a program to automate the building and installation of Python in the users $HOME.
Dependencies – curl
Before Installing the Pythonbrew, Install “curl” in the machine, to install curl use the below command in the terminal, give the the password for the user when prompted.
$sudo apt-get install curl
After Installing the curl, Now Install Pythonbrew, copy and paste the following commands in the terminal and type the password for the user when prompted.
Recomended method of installation - Easy Install
$ sudo easy_install pythonbrew
To complete the installation, type the following command
$pythonbrew_install
Alternate method of installation:
Use curl command to download the latest version of pythonbrew from github.
curl -kLO http://github.com/utahta/pythonbrew/raw/master/pythonbrew-install
After downloading, change “pythonbrew-install” to “executable”
chmod +x pythonbrew-install
Then, run the pythonbrew-install in the terminal
./pythonbrew-install
Now the Pythonbrew has been installed in the “Home Directory” i.e., /home/user/.pythonbrew
Next, copy and paste the following line to the end of ~/.bashrc
*NOTE: change “user” to your user name in the system
source /home/user/.pythonbrew/etc/bashrc
Thats it! Close the terminal.
Steps to Install different versions of Python:
Open a new terminal, type the following command or copy and paste it.
$pythonbrew install 2.6.6
This will install Python 2.6.6 and to install Python 2.7 or Python 3.2, change the version number in the previous command.
$pythonbrew install 2.7
or
$pythonbrew install 3.2
Update: If you get error while Installing then Install using the below command.
$pythonbrew install --force 2.7
or
$pythonbrew install --force 3.2
How to manage different versions of Python installed in system
For instance, if Python 2.6.6, Python 2.7 and Python 3.2 is installed in your system, switching between the versions can be done as follows:
By default, Python 2.6.6 will be active and in order to switch to Python 2.7 use the below command
$pythonbrew switch 2.7
The default Python is changed to Python 2.7.
Now, to switch to Python 3.2 change the version number in the previous command.
$pythonbrew switch 3.2
Use the below command to check or list the installed Python versions
$pythonbrew list
Use the below command to check or list the available Python Versions to install
$pythonbrew list -k
To uninstall any of the installed Python version (for example to uninstall Python 2.7), use the below command.
$pythonbrew uninstall 2.7
Use the below command to update the Pythonbrew
$pythonbrew update
Use the below command to disable the Pythonbrew and to activate the default version
$pythonbrew off
Enjoy the experience of installing multiple versions of Python in single Linux / ubuntu machine!
I find http://github.com/utahta/pythonbrew much easier to install and use than any other solution.
Just install it and you'll have these options:
pythonbrew install 2.7.2
pythonbrew use 2.7.2 # use 2.7.2 for a current terminal session
pythonbrew switch 2.7.2 # use 2.7.2 by default system wide
pythonbrew uninstall 2.7.2
Note: if you're using a Linux-based operating system with preinstalled Python, switching (system wide) to another version can make things go wrong, so be careful.
A more grassroot approach than Virtualenv is the side-by-side installation of two Python versions.
If there is an existing installation, and you want a second installation into the same root path (e.g. /usr/local), use this target when making install:
make altinstall
When your second installation is Python 2.6, this will leave you with a /usr/local/bin/python2.6 alongside the old /usr/local/bin/python.
A simple way to switch between these two versions is using a shell alias (alias python=/usr/local/bin/python2.6) on the shell where you invoke the interpreter. But this won't work across sub-shells and she-bang invocations.
pyenv is yet another Python manager. The README.md at that link has a good set of instructions, but they basically are:
$ cd
$ git clone git://github.com/yyuu/pyenv.git .pyenv
Then set up your $PATH.
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
Install the desired versions of Python:
$ pyenv install 2.7.8
After installing you need to run this:
$ pyenv rehash
Then switch to the version of Python you want to run, for the shell:
$ pyenv shell 2.7.8
"Question1: How will i tell any framework that go and use version so and so pf python like day django to use 2.6 and say mjango to use 2.4?"
You simply run them with the specific python version they need. Run mjango with /usr/bin/python2.4 and django with /usr/bin/python2.6. As easy as that.
"Question2: Is there more elegant way to switch between version as my hack of symlinking was a virtual disaster?"
Yes, see above. Have two separate installs of Python, and run explicitly with the different versions.
"Question3: Can I download a deb for say hardy and make jaunty believe its for her?"
That generally works. If it doesn't, it's because it has dependencies that exist in Hardy, and does not exist in Jaunty, and then you can't.
And here is a Question 4 you didn't ask, but should have. ;)
"Is there an easier way to download all those Python modules?"
Yes, there is. Install setuptools, and use easy_install. It will not help you with library dependecies for those Python modules that have C code and need to be compiled. But it will help with all others. easy_install will download and install all the Python dependencies of the module in question in one go. That makes it a lot quicker to install Python modules.
Move to the project directory :
Create an environment :
virtualenv -p python2.7 --no-site-packages ~/env/twoseven
Then activate your source :
source ~/env/twoseven/bin/activate

Categories