How to make python 3.9 my default python interpreter on centos - python

I recently installed python3 on my vps, I want to enable it as default, so that when I type
python I get python 3. I think the problem is its installed in /usr/local/bin instead of /usr/bin/ typing python on the terminal access python2 typing python3 returns bash: python3: command not found
Most answers I have seen is a bit confusing as I am not a centos expert.

for a simple fix, you can use alias
add the alias to your .bashrc file
sudo vi ~/.bashrc
then add your alias at the bottom
alias python="python3.9"
So that when you type python you'll get python 3

There're several ways.
First, check if /usr/local/bin isn't in your $PATH variable:
echo $PATH
If it is indeed not there, you may want to add this line to your .bashrc file (assuming you're using bash):
export PATH="/usr/local/bin:$PATH"
This will add /usr/local/bin to your $PATH variable and will make python3 accessible after you relaunch a terminal session.
Now, changing the default python globally might not be a good a idea as there could be system software depending on python command pointing to python2.
What you could do is use PyEnv which will allow you using different python versions on your computer: https://github.com/pyenv/pyenv

Related

Arch mingw: Using a python version installed with pacman

I am currently using the MSYS2 Building Platform which uses an arch shell to allow users to install their packages via the pacman package manager. On this shell, neither $PYTHONHOME nor $PYTHONPATH are set, and issuing which python3 returns /usr/bin/python3. I have used pacman to install a python version I would actually like to be using, which is located in /mingw64/lib/python3.9/. My first approach was to issue:
export PYTHONPATH=/mingw64/lib/python3.9/
exec bash
But this did not change the output of which python3 or which python. I have also tried modifying the PYTHONPATH variable in a virtual environment but this has not worked either. How can I made the adjustments necessary so that the aliases python and python3 automatically point to the desired python installation?
The solution was to find the location of the mingw64 python installation by issuing
pacman -Q1 | grep /mingw64/bin/
and then appending it to $PATH by issuing
export PATH=mingw-w64-x86_64-python:$PATH
and then reloading the python alias paths by issuing
exec bash

Editors not honoring Python shell aliases

I run macOS Catalina with zshell.
Out of the box the os has one python2 and one python3 version in /usr/bin/python and /usr/bin/python3. I have installed a newer python3 via Homebrew. That version is in /usr/local/opt/python#3.8/bin/python3.
I have added aliases to my ~/.zshrc-file so that both python and python3 will launch into the 3.8 Homebrew version.
When using editors (e.g. Atom) that run python scripts by calling python3 this aliasing does not seem to work. I guess this is because it is specific to the terminal shell.
What is a better way of getting my homebrew python3.8 to become the default python on my system?
Don't uses aliases for selecting alternate programs. Use your PATH variable to manage your preferences.
Start by creating a local bin directory if you don't already have one.
mkdir -p ~/bin
Assuming your PATH is already set up to prefer Homebrew versions over system-installed versions, add ~/bin to the front of the path.
# In .bash_profile
PATH=~/bin:$PATH
Now, create a symbolic link ~/bin/python to the desired Python 3 interpreter.
ln -s /usr/local/opt/python#3.8/bin/python3 ~/bin/python
Now when you run python, you'll get your Homebrew python3.8 interpreter. You can still access the system Python 2 with /usr/bin/python when needed. Your editors should also inherit and respect your PATH variable, unless it is configured to use a specific hard-coded path.
Note that Homebrew still(?) links /usr/local/bin/python to its own Python 2 interpreter; I don't recommend changing that to python3, lest other Homebrew-managed programs get Python 3 when they require Python 2, hence the use of ~/bin. (There's still a chance that programs using python via path lookup will assume it is Python 2, but this should minimize the problems.)

Make python3 as my default python on Mac

What I'm trying to do here is to make python3 as my default python. Except the python 2.7 which automatically installed on mac, I installed python3 with homebrew. This is the website that I'm following. http://docs.python-guide.org/en/latest/starting/install3/osx/#install3-osx
I guess I followed every instruction well, got xcode freshly installed, Command line tools, and homebrew. But here's my little confusion occurs.
The script will explain what changes it will make and prompt you before the installation begins. Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
I was really confused what this was, but I concluded that I should just add this following line at the bottom of ~/.profile file. So I opened the ~/.profile file by open .profile in the terminal, and added following line at the bottom. And now it looks like this.
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
# Setting PATH for Python 3.6
# The original version is saved in .profile.pysave
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
And then I did brew install python, and was hoping to see python3 when I do python --version.
But it just shows me python 2.7.10. I want my default python to be python3 not 2.7
And I found a little clue from the website.
Do I have a Python 3 installed?
$ python --version
Python 3.6.4
If you still see 2.7 ensure in PATH /usr/local/bin/ takes pecedence over /usr/bin/
Maybe it has to do something with PATH? Could someone explain in simple English what PATH exactly is and how I could make my default python to be python3 when I run python --version in the terminal?
Probably the safest and easy way is to use brew and then just modify your PATH:
First update brew:
brew update
Next install python:
brew install python
That will install and symlink python3 to python, for more details do:
brew info python
Look for the Caveats:
==> Caveats
Python has been installed as
/usr/local/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/bin
Then add to your path /usr/local/opt/python/libexec/bin:
export PATH=/usr/local/opt/python/libexec/bin:$PATH
The order of the PATH is important, by putting first the /usr/local/opt/python/libexec/bin will help to give preference to the brew install (python3) than the one is in your system located in /usr/bin/python
Before we make the changes, the default version of python in my system was python 2.7.17.
python --version
Python 2.7.17
To make python3 as default python by replacing python2 in Ubuntu.
Open Terminal
cd
nano ~/.bashrc
alias python=python3 (Add this line on top of .bashrc file)
Press ctr+o (To save the file)
Press Enter
Press ctr+x (To exit the file)
source ~/.bashrc OR . ~/.bashrc (To refresh the bashrc file)
python --version
Python 3.7.5
Changing the default python version system wide can break some applications that depend on python2. The alternative solution would be to create an alias.
If you are using zsh (the default on Mac OS) run the following from terminal:
echo 'alias python="python3"' >> ~/.zshrc
echo 'alias pip="pip3"' >> ~/.zshrc
According to this S.O. post, changing the default Python interpreter could possibly break some applications that depend on Python 2.
The post also refers to using aliasing as a solution, and this link might also be a good reference on how to do that.
Personally, I just type "Python3" before I run scripts or go into a shell environment instead of "python".

Always run python 2.6 instead of 3.4 in centos

I have installed python 3.4 in centos.
So I opened terminal and typed python or idle then always ran python 2.6 not python 3.4.
How can i run python 3.4 instead of 2.6?
thanks you.
Do not replace the default Python! CentOS's system tools such as yum, system-config-* tools and several other things rely on the default Python 2.6 installation. Set up a virtual environment instead, where you can define which is the default version.
virtualenv --python=/usr/bin/python3.4 myenviron
source myenviron/bin/activate
First, look for where your Python 3.4 is located:
$ which python3.4
/usr/bin/python3.4
See if ~/bin (e.g., /home/username/bin) directory is in the PATH environment variable:
$ echo $PATH
/home/username/bin:/usr/local/bin:/usr/bin:/bin
If not, add the bin directory to PATH, ideally within your ~/.bashrc file. Then create a symlink pointing to the Python interpreter under the bin directory:
$ ln -s /usr/bin/python3.4 /home/username/bin/python
This way, when you type python on the command line, the interpreter specified will be launched.
I am on Debian/Wheezy, so the detail might be slightly different, but a similar approach should work.

How do I change where Bash looks for Python in Linux?

I just updated my ReadyNas from python 2.3.5 to python 2.6.6. The upgrade placed the new version in the /usr/local/bin directory. So
/usr/local/bin/python is Python 2.6.6
/usr/bin/python is Python 2.3.5
When I type python at a bash prompt tries to run /usr/bin/python or my old version. I relocated my old version, and now I get:
bash: /usr/bin/python: No such file or directory
How can I change where bash looks for python? How is bash currently deciding that when I type python that it only looks in /usr/bin for python?
Your PATH environment variable. It has a list of directories which bash searches (in the same order) when it's looking for an program to execute. Basically you want to put /usr/local/bin at the start of your PATH environment variable. Add the following to your ~/.bashrc file:
export PATH=/usr/local/bin:$PATH
You can have a look at the current setting by running the set command in bash.
Alternatively, you can simply rename /usr/bin/python to /usr/bin/python2.3 and create a symlink pointing to the new version, e.g.
ln -s /usr/local/bin/python /usr/bin/python
I don't think it's BASH responsibility to choose the default version for the Python interpreter.
If you're the administrator, the cleanest way to do this is to use a symbolic link in /usr/bin/python pointing to the appropiate version. Avoid replacing the actual binaries if possible.
If you're not, then add a bin folder somewhere you have access to, and prepend it to the $PATH environment variable. Then create a symlink to the desired version of the python interpreter.
Cheers!

Categories

Resources