Set python 3 as default python version in CentOS - python

I have installed python 3.6 on my CentOS machine by following the guide at https://linuxize.com/post/how-to-install-python-3-on-centos-7/, which installs python 3 with the following:
sudo yum install rh-python36
The default python version, however, is still python 2. The guide mentions I can run python 3 by first running scl enable rh-python36 bash, and then python 3 will be used. However, this only works for that session - logging out and back in will revert back to python 2. According to How to set Python3.5.2 as default Python version on CentOS?, python 3 can be set as the default with the following:
sudo ln -fs /usr/bin/python3 /usr/bin/python
However, this doesn't work for me as for some reason I don't have any python3 file in /usr/bin/ - I only have python, python2, python2.7, python2.7-config, python2-config and python-config (despite installing python 3 with yum as above).

First, run
$ scl enable rh-python36 bash
to switch python version.
Second, run
$ which python
to find out the exact path of python3.6 binary.
Third, edit ~/.bashrc.
$ vim ~/.bashrc
and add below line.
alias python='/opt....(the path you found by "which python")'
and save the file.

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

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

How to fix "python version are always returned 2.7 instead of 3.7"

I downloaded python 3.7.3 but python 2.7.10 already existed.
Now python --version returns 2.7.10
How to fix this?
If you are working on Linux, you can always type python3 --version to check if it is installed and be sure it is the version you want to use.
There are several ways to make python call Python 3 by default. For instance you can create an alias. Type whereis python3 so you get the installation path to python3 (normally it is located in /usr/bin/python3). If that's the case you can simply add to ~/.bashrc the following line:
alias python='/usr/bin/python3'
Then, source that file or reload the session. This assumes that /usr/bin/python3 is the location of Python 3. Please, note that other commands that depend on your Python installation (such as pip or coverage) are still pointing to the ones installed by Python 2, so you may want to do the same for them, or make sure that you call pip3 instead of pip if you want to install any extra package.
If you are using linux or macOS, the python command refers to the built-in Python2. You need to use the python3 command to use Python3.
So running:
python3 --version
should give you the expected output.
Also, when you want to run your scripts in Python3, you need to use:
python3 myscript.py
Both the python2x and python3x can exist on a system. On linux machines the default python version is python2x.
so if you want to work with python2x type python2.7 or python3 otherwise.
Installation
sudo apt update
sudo apt install python3.9
Checking the installation with the python3 --version command still returns the old version. To fix this :
Create a list of update alternatives. First, add the old version to the list with the command:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.[old-version] 1
Now add the new version:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
Type the following command to configure the priority status of the versions:
sudo update-alternatives --config python3
check the default version:
python3 --version
DONE

system python versus user python in 2.7

I've installed Python 2.7 using macports and because of my $PATH variable, that's the one I get when I type $ python. However, virtualenv defaults to using Python 2.6 unless I remember to force it to do otherwise with the -p flag.
On a related note, globally running yolk -l shows the following:
Python - 2.6.1 - active development (/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload)
So my question is, should I do something to ensure the system is always using MacPorts' Python 2.7 or is it not worth worrying that Apple's Python 2.6.1 is apparently in the mix?
Use:
$ sudo port select --set python2 python27
In order to use the MacPorts version as your primary Python try:
$ sudo port select --set python python27
or
$ sudo port select --set python2 python27
To make this the default Python or Python 2 (i.e., the version run by
the 'python' or 'python2' commands), run one or both.
* Restart Terminal.app, or whichever console you're using then try:
$ which python
$ python -V

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.

Categories