Since you have Linux, and if you want to simply type "python" instead of "python3" in order to run Python programs, a solution is simply to define an alias in you shell configuration file (.bashrc, etc.). For Bourne shells, it should be something like

alias python=python3

(or whatever your Python 3 name is).

This way, you do not have to change anything on your system, so this solution should quite innocuous and it should not break your system.

Answer from Eric O. Lebigot on Stack Overflow
Discussions

ubuntu - How to set up Python path? - Unix & Linux Stack Exchange
I have uninstall anaconda2. but now when I run Python command in terminal it says "bash: /home/user/anaconda2/python: No such file or directory" now how can I set to Python when I have python 2.7 i... More on unix.stackexchange.com
🌐 unix.stackexchange.com
December 31, 2015
How do I change where Bash looks for Python in Linux? - Stack Overflow
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 P... More on stackoverflow.com
🌐 stackoverflow.com
python - PYTHONPATH on Linux - Stack Overflow
I'm novice in this, and I have started learning Python, but I have some questions that I'm not be able to understand, What exactly is the PYTHONPATH (on Ubuntu)? Is it a folder? Is Python provided by More on stackoverflow.com
🌐 stackoverflow.com
Does changing default python path to anaconda python affect software centre
Hi, Recently i installed anaconda on my system and changed the default python to anaconda python export PATH="/usr/local/anaconda/bin:$PATH" default python path was - export PATH="/usr/bin/python/bin:$PATH" By doing this i can launch my environment directly from terminal. More on forum.manjaro.org
🌐 forum.manjaro.org
0
0
January 31, 2022
🌐
Medium
medium.com › @furkangozukara › how-to-change-default-python-on-a-linux-machine-a200d9655543
How To Change Default Python On A Linux Machine - Furkan Gözükara - PhD Computer Engineer, SECourses - Medium
January 3, 2024 - echo 'export PATH=/home/ubuntu/Python-3.10.13:$PATH' >> ~/.bash_profile echo 'export PATH=/home/ubuntu/Python-3.10.13:$PATH' >> ~/.profile echo 'export PATH=/home/ubuntu/Python-3.10.13:$PATH' | sudo tee -a /etc/environment echo 'export PATH=/home/ubuntu/Python-3.10.13:$PATH' | sudo tee -a /etc/profile.d/custom.sh · Press enter or click to view image in full size · Press enter or click to view image in full size · Linux ·
Top answer
1 of 4
5

I'm assuming that when you installed anaconda 2, you manually set the PYTHONPATH environment variable, by putting something like

PYTHONPATH=/home/user/anaconda2/python
export PYTHONPATH

in your .bash_profile or .bash_rc.

But since you deleted the /home/user/anacanda2/ directory, that path no longer exists.

Thus you want to change PYTHONPATH to point to the executable in /usr/lib, by changing the above to

PYTHONPATH=/usr/lib/my_python_distribution
export PYTHON
2 of 4
3
  1. execute the command: echo $PATH

    root1@master:/usr/lib/python2.7$ echo $PATH
    /home/root1/anaconda3/bin:/home/root1/NAI/Execution/HDE/x86_64.linux/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/root1/java/jdk1.8.0_74/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin:/home/root1/NAI/hadoop-2.7.3/bin
    
  2. Remove your anaconda3 from your path variable that is

    /home/root1/anaconda3/bin:
    
  3. Again set PATH variable with remaining information like below

    export PATH=/home/root1/NAI/Execution/HDE/x86_64.linux/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/root1/java/jdk1.8.0_74/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin:/home/root1/NAI/hadoop-2.7.3/bin
    
  4. Execute python command and should redirect to your python interpreter

    root1@master:/usr/lib/python2.7$ python
    Python 2.7.14 (default, Sep 18 2017, 00:00:00) 
    [GCC 4.8.4] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    
Top answer
1 of 3
73
  1. PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. e.g.:

    # make python look in the foo subdirectory of your home directory for   
    # modules and packages 
    export PYTHONPATH=${PYTHONPATH}:${HOME}/foo 
    

    Here I use the sh syntax. For other shells (e.g. csh,tcsh), the syntax would be slightly different. To make it permanent, set the variable in your shell's init file (usually ~/.bashrc).

  2. Ubuntu comes with python already installed. There may be reasons for installing other (independent) python versions, but I've found that to be rarely necessary.

  3. The folder where your modules live is dependent on PYTHONPATH and where the directories were set up when python was installed. For the most part, the installed stuff you shouldn't care about where it lives -- Python knows where it is and it can find the modules. Sort of like issuing the command ls -- where does ls live? /usr/bin? /bin? 99% of the time, you don't need to care -- Just use ls and be happy that it lives somewhere on your PATH so the shell can find it.

  4. I'm not sure I understand the question. 3rd party modules usually come with install instructions. If you follow the instructions, python should be able to find the module and you shouldn't have to care about where it got installed.

  5. Configure PYTHONPATH to include the directory where your module resides and python will be able to find your module.

2 of 3
46
  1. PYTHONPATH is an environment variable
  2. Yes (see https://unix.stackexchange.com/questions/24802/on-which-unix-distributions-is-python-installed-as-part-of-the-default-install)
  3. /usr/lib/python2.7 on Ubuntu
  4. you shouldn't install packages manually. Instead, use pip. When a package isn't in pip, it usually has a setuptools setup script which will install the package into the proper location (see point 3).
  5. if you use pip or setuptools, then you don't need to set PYTHONPATH explicitly

If you look at the instructions for pyopengl, you'll see that they are consistent with points 4 and 5.

Find elsewhere
🌐
Net Informations
net-informations.com › python › intro › path.htm
How to set python path
To add the Python directory to the path for a particular session in Unix/Linux : csh shell: type setenv PATH "$PATH:/usr/local/bin/python" and press Enter. bash shell (Linux): type export ATH="$PATH:/usr/local/bin/python" and press Enter. sh or ksh shell: type PATH="$PATH:/usr/local/bin/python" ...
🌐
Baeldung
baeldung.com › home › administration › setting the default python to python3
Setting the Default python to python3 | Baeldung on Linux
May 7, 2025 - Assuming we’ve installed Python3.8 in the /usr/bin directory and want it to be our default Python, we can use the alias command: ... Using an alias, we can set both python and python3 commands to the same path.
🌐
GitHub
bic-berkeley.github.io › psych-214-fall-2016 › using_pythonpath.html
Using PYTHONPATH — Functional MRI methods
Luckily, we can make the PYTHONPATH value be set for any terminal session, by setting the environment variable default.
🌐
Tutorialspoint
tutorialspoint.com › python › python_environment.htm
Python - Environment Setup
To add the Python directory to ...ocal/bin/python" and press Enter. In the bash shell (Linux) − type export PATH="$PATH:/usr/local/bin/python" and press Enter....
🌐
TutorialsPoint
tutorialspoint.com › how-to-set-python-environment-variable-pythonpath-on-linux
How to set Python environment variable PYTHONPATH on Linux?
May 2, 2023 - This command sets the PYTHONPATH environment variable to /home/user/myproject and also includes the previous value of PYTHONPATH in case it was already set. Note that the path should be separated by a colon (:) on Linux.
🌐
Manjaro Linux
forum.manjaro.org › support › third-party software
Does changing default python path to anaconda python affect software centre - Third-Party Software - Manjaro Linux Forum
January 31, 2022 - Hi, Recently i installed anaconda on my system and changed the default python to anaconda python export PATH="/usr/local/anaconda/bin:$PATH" default python path was - export PATH="/usr/bin/python/bin:$PATH" By doing this i can launch my environment ...
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 485410 › python-path-linux
python path Linux [SOLVED] | DaniWeb
September 26, 2014 - I'm trying to add /home/garrett/bin/libpy/ to my python path so I can import my own libraries, I would also prefer not to overwrite the default python path but rather append my own path to the default. When I try to echo $PYTHONPATH or $PYTHONHOME I get nothing and even if I were to set it on the command line it wouldn't remain after the next reboot. I don't know where the config file for this is. Here's what the man pages had to say. PYTHONHOME Change the location of the standard Python libraries. By default, the libraries are searched in ${prefix}/lib/python<version> and ${exec_prefix}/lib/python<version>, where ${prefix} and ${exec_prefix} are installa‐ tion-dependent directories, both defaulting to /usr/local.
Top answer
1 of 3
2

If what you want is to get a specific version of the Python interpreter when you type python in your shell, there is no environment variable that can help you in that sense. It is the shell that decides the binary of the interpreter to use, and by the time it is spawned it cannot be swapped to a different one. Some ways that you can work around this:

  • As you said, making a symlink somewhere. It does not need to be in a system directory, many users have something like $HOME/bin in their $PATH (some Linux distributions do this by default).
  • Make an alias (e.g. in your .bashrc or similar).
  • Make a function (same as previous). You could even program it to receive some argument that decides which version of the interpreter you want to use, or to use the value of some environment variable, as you suggested.
  • In distributions supporting it, set up an alternative.
  • Make a virtual environment using the interpreter that you want and work within that environment.

In any case, there are a few things you should take into account:

  • As mentioned in another answer, changing the version of the Python interpreter system-wide may break things. Particularly, switching the command python from Python 2 to Python 3 (or the other way around) is almost guaranteed to cause quite a lot of trouble. Some of the solutions above may be unaffected by this (e.g. setting and alias or function in .bashrc should not affect other scripts, unless sourced), but you should be careful about it.
  • A common pitfall here is that changing the python command does not change every Python-based command. For example, if you use IPython and just type ipython you need to make sure that the correct interpreter and script are launched. Depending on your context there may be a few tools that you may need to consider in that sense.

A virtual environment is probably the easiest and cleanest solution, since it was designed for that particular problem (even though issues still arise sometimes, e.g. if you run ipython but you forgot to install it in your environment first you will get the system-wide one, obviously), although if you want to use it for every shell session then it may not be as convenient.

2 of 3
2

Depending on what OS you're using, you can use alternatives to change the default.

https://wiki.debian.org/DebianAlternatives https://linuxconfig.org/how-to-switch-between-python-versions-on-fedora-linux

So, to change the default Python, you could do something like

alternatives --install /usr/bin/python python /usr/bin/python3.6 1

Be aware though, this can break a lot of stuff as the OS is likely dependent on the version of Python.

Top answer
1 of 4
5

The comments somewhat cover the answer to the question, but to clarify:

When you installed Anaconda you must have agreed to have it added to your PATH. You'll want to check in your ~/.bash* files and look for any export PATH= lines to check this. So Anaconda is always on your path. The source deactivate command will only deactivate "sub" Conda environments. It will never remove what is called the "root" Conda environment (the one you originally installed). If you don't want Anaconda on your PATH by default then remove it from your ~/.bash* startup files. Then when you want to use Anaconda you'll need to add it to your PATH. Or just add the specific Conda environment you are interested in to your PATH directly, and don't worry about the activate and deactivate scripts. At their core all they do is modify PATH.

I hope that helps clarify things.

2 of 4
1

Anaconda comes with its own everything, and they ask if you wish to use their software as a default when you install it by adding their bin first to your PATH variable. If you do that, you can only manually remove it later from .bashrc to undo this action.

I chose not to do it, but i made a shell script to start spyder and use the anaconda distribution when i wish to, without altering my PATH by calling spyder like this from the shell script:

PATH=/home/<... path to where i installed anaconda>/bin:$PATH spyder &

This means that i am adding their distribution's bin to the path only for the extent of running that command (spyder), otherwise my environment is unaffected by anaconda.

If i wish to add things to it, i pass an option to the shell when i source it and that triggers these actions:

PATH=/home/<... path to where i installed anaconda>/bin:$PATH
PS1='\\033[1;34m\\w:\[\033[0m\] '

so that i see (with colors!) that in this terminal i am using an altered PATH, the one with python3 and such from anaconda, etc... When done, i kill the terminal! :)

🌐
Edureka
edureka.co › blog › add-python-to-path
Add Python To Path | How To Add A Python Path | Edureka
December 5, 2024 - One of the most important things to note when you are adding Path to Python in Unix or Linux is that, /usr/local/bin/python is the default path of the Python directory.