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

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!

Related

How to make python 3.9 my default python interpreter on centos

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

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".

change python version in terminal and intelliJ

short Q,
In a mac OS sierra terminal,
If I do:
whereis python
/usr/bin/python
Then if I do:
/usr/bin/python it opens python 2.10
but if I execute
python it opens python 2.7.8.
and this one comes from
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
How do I change the default python to point to python 2.7.8? In the terminal and in intelliJ?
Check the PATH environment variable with
echo $PATH
The python version you get when typing bare 'python' will be the first one found in that list of directories.
It is possible to control which python version is launched by, for example, rearranging the entries in PATH or by adding a symbolic link to the desired version in a position before the current version.
However, a more popular way to manage multiple python versions on the same machine is to use virtualenv. This will give you much less headaches when using pip to install/uninstall packages for particular python versions.
As wim mentioned you will get the first python that is found in $PATH.
A nice way may be to ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/local/bin/python. This will create a symbolic link in /usr/local/bin
That way you don't change the order in your $PATH variable. Also note that if you echo $PATH, /usr/local/bin should be before /usr/bin - in case you have other versions in /usr/bin (which you do given your example)
I would strongly recommend you do what wim mentioned and use virtualenv to manage you packages.

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.

Start the correct python program via symbolic link

I recently installed ActivePython 2.7. So I've got a symbolic link in /usr/local/bin/python which points to the 2.7 directory. Trouble is, when I type "python" at the command line, it starts up MacPython 2.6 in /usr/bin/python. I don't understand why the symbolic link is not taking precedence. /usr/local/bin is on my PATH. Any help would be appreciated.
~ $ which python
/usr/bin/python
~ $ find /usr -iname python
/usr/bin/python
/usr/local/bin/python
Path looks like this:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/opt/PalmPDK/bin:/opt/PalmSDK/Current/bin:/usr/X11/bin
When you type “python” on the command line, the shell checks the values in your $PATH from left to right, and invokes the program it finds first. Check your current $PATH setting like this:
$ echo $PATH
If it contains both /usr/bin and /usr/local/bin then whichever comes first will take precedence.
in your PATH variable, /usr/bin probabbly is before the /usr/local/bin/ directory. you should use the full path in order to work in this case.

Categories

Resources