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 OverflowSince 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.
You really don't want to change what python points to, because some programs might expect Python 2, and break.
The solution is to use virtualenv: create an isolated Python 3 environment (with the -p python3 option), activate it, and you're good to go.
The site module documentation and Modifying Python's Search Path seem to be what you're looking for.
As far as I understand it, those entries are being added to sys.path by:
/usr/lib/python2.6/site.py/usr/lib/python2.6/dist-packages/site.py
(Change 2.6 to your version of Python.)
The easiest way to change it is to add a file /usr/local/lib/python2.6/dist-packages/site-packages.pth containing ../site-packages.
Alternatively, maybe you can teach the package to use site.getsitepackages()?
I'd like to summarize my findings about python's path modification. There are two ways to do it.
- .pth file
PYTHONPATH
Any .pth file which is found on the default path (see bellow) will get its content included into sys.path.
Format of said .pth file is simple: one (folder) path per line. Surprisingly, the paths can be absolute or relative to the .pth file.
Default path is where the interpreter resides and <some-prefix>/lib/python<version>/site-packages where <some-prefix> is usually /usr/.
PYTHONPATH is environmental variable of your operating system. On unix systems you list them by env. Global modification of such variables is done through .sh scripts inside /etc/profile.d/ folder as mentioned by @TestUser16418.
ubuntu - How to set up Python path? - Unix & Linux Stack Exchange
How do I change where Bash looks for Python in Linux? - Stack Overflow
python - PYTHONPATH on Linux - Stack Overflow
Does changing default python path to anaconda python affect software centre
It's not good to change the default python. Many system programs depends on python2 not python3. if you want to use python3, you just type the command python3.
The PYTHONPATH doesn't have much to do with it. It just tells the Python interpreter, whichever interpreter runs, where to find extra modules you want to load.
But either way, changing the default version on your system is dangerous, as other commenters have observed. You stand the risk of breaking existing package that are built against Python2 and use /usr/bin/env python to find their interpreters.
In fact, that's the wrong way to phrase it. Changing the default version of Python will break your entire Ubuntu system and cause lots of programs to just not work. It's also completely unnecessary.
But since you asked, you just do this:
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3.2mu /usr/bin/python
Happy crashing!
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
execute the command:
echo $PATHroot1@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/binRemove your anaconda3 from your path variable that is
/home/root1/anaconda3/bin: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/binExecute 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. >>>
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!
PYTHONPATHis 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}/fooHere I use the
shsyntax. 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).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.
The folder where your modules live is dependent on
PYTHONPATHand 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 commandls-- where doeslslive?/usr/bin?/bin? 99% of the time, you don't need to care -- Just uselsand be happy that it lives somewhere on yourPATHso the shell can find it.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.
Configure
PYTHONPATHto include the directory where your module resides and python will be able to find your module.
PYTHONPATHis an environment variable- Yes (see https://unix.stackexchange.com/questions/24802/on-which-unix-distributions-is-python-installed-as-part-of-the-default-install)
/usr/lib/python2.7on Ubuntu- 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).
- if you use pip or setuptools, then you don't need to set
PYTHONPATHexplicitly
If you look at the instructions for pyopengl, you'll see that they are consistent with points 4 and 5.
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/binin their$PATH(some Linux distributions do this by default). - Make an alias (e.g. in your
.bashrcor 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
pythonfrom 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.bashrcshould not affect other scripts, unlesssourced), but you should be careful about it. - A common pitfall here is that changing the
pythoncommand does not change every Python-based command. For example, if you use IPython and just typeipythonyou 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.
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.
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.
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! :)