How to switch python version permanently, without "activating" environments? - python

I would like to be able to switch Python version permanently, in OS-wide manner and reboot tolerant, is it possible?
I don't want to use usual activate approach, which shows environment in command line prompt.

For windows, you can simply install a new version of anaconda and add it to the PATH before the old version (and any other python versions). Windows will then find this version of python first, and it will thus be your "OS-wise" python installation.

On windows, I think you just have to change your PATH environment variable and add the path to your favorite python.
I think you don't need to reboot your machine but you may have to restart your command line console (cmd.exe) to take it into account

Related

With conda/anaconda should I work in (base) all the time?

This is a total newbie question, but I installed conda along with a bunch of other stuff like Tensorflow with a virtual environment, and now I'm a little confused. If I open a terminal my python is pointing to /usr/bin/python. If I go to the base environment ("conda activate base"), my path points to where I installed anaconda, /usr/local/anaconda2. Does conda expect that I always go into the base environment to use all the anaconda packages, or did I do something weird on my install that my non-environment still points to the original system python? Should I just change my path to point to anaconda? I'm on Ubuntu 18.10.
I am a fan of being explicit -- instead of running python at the command line, I use the full path to the python interpreter when running scripts (e.g. /usr/local/anaconda2/bin/python) -- this way I don't rely on the shell behavior or having to worry about which environment is currently active. Running the interpreter using its full path is no different than just running python while the environment is active.
But that's not everbody's cup of tea... So...
Even if anaconda did not prompt you to update your PATH variable, you can do it explicitly by updating your .bashrc or .bash_profile (or .zshrc or whatever else) by adding this line: PATH=/usr/local/anaconda2/bin${PATH}. Dont forget to re-source the .bashrc or close/reopen your shell for it to take effect.
Conda is capable of creating many virtual environments beyond the base, so if you have more than one and you want to set one as the default, then just update the line above for the appropriate path.

How to isolate Anaconda from system python without set the shell path

I want to install Anaconda locally on my home directory ~/.Anaconda3 (Archlinux) and without setting the path in the shell because I like to keep my system python as the default.
So I like to launch the Spyder (or other Anaconda's app) as isolated app from system binaries. I mean when I launch for example .Anaconda3/bin/spyder it launches spyder and this app uses Anaconda's binaries but when I use python ThisScript.py in my shell it uses system python installed from packages (e.g. /bin/python).
I managed to update the anaconda using .Anaconda3/bin/conda update --all in my shell without setting the Anaconda's binaries path (.Anaconda/bin/) but thsi way run some apps like spyder doesn't work obviously.
You could use virtualenv
1) create a virtual env using the python version you need for anaconda virtualenv -p /usr/bin/pythonX.X ~/my_virtual_env
2) virtualenv ~/my_virtual_env/bin/activate
3) Run anaconda, then deactivate
Currently this zsh function solves the problem using temporarily change the shell path variable. I just need to:
1) anaconda_on
2) which python or python --version or spyder ....
3) anaconda_off
This is neat and solves my problem. But there might be more universal way for this. Any suggestion? There are many cases which is better to isolate anaconda from the system python.

How to set the default python path for anaconda on Linux?

I have installed anaconda on a Linux machine. I noticed that after deactivating the anaconda environment with:
source deactivate
When running:
which python
I get:
/home/user/anaconda/bin/python
Instead of
/usr/bin/python
How can I restore this setting?
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\](A)\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! :)
To get back to the former default, I comment out the entry setting the pathbin the .bashrc file and get these results:
$ which python
/usr/bin/python
$ python -v
python 2.7.*
If you have installed something, you want to use it before older installs, in general.
Your path is well set: your install is searched before system install.
If you want to use system python, run % /usr/bin/python.
If you don't want anaconda, just trash it (or move it somewhere ouside of path).
This may help:
% which -a python.

Using python with Anaconda in Windows

I have just installed Anaconda on my computer because I need to use Numpy.
Well, when I use python I for some reason have to be in the same folder as python.exe and, of course, now that I want to use Anaconda I have to be in the Anaconda3\Scripts folder where python.exe isn't. This is a nightmare, how can I use anaconda with python on a windows computer? Why does it have to be so complicated?
I think you are referring to the command-line use of python?
If you have admin priviliges on your machine you can add python to your environment variables, making it available in the console anywhere. (Sorry for different spellings, I am not on an english machine)
Press Shift+Pause ("System")
Click "Advanced System Options"
Click "Environment variables"
In the lower field with "System variables" there is a variable called PATH. Append the complete path to your python.exe without the file to that by adding a ; behind the last path in the variable and then adding your path. Do not add any spaces!
Example: C:\examplepath\;C:\Python27\
When you install anaconda on windows now, it doesn't automatically add Python or Conda to your path.
If you don’t know where your conda and/or python is, you type the following commands into your anaconda prompt (it comes when you install anaconda)
Next, you can add Python and Conda to your path by using the setx command in your command prompt.
Next close that command prompt and open a new one. You should now be able to use the python command. To do this you open a command prompt and type
python nameofPythonfile.py
Source: https://medium.com/#GalarnykMichael/install-python-on-windows-anaconda-c63c7c3d1444
To be able to do that in the command line you just have to add Python and also the Anaconda3\Scripts directory to your system path.
Here is a good tutorial on setting your path in Windows:
http://www.computerhope.com/issues/ch000549.htm

Revert Ubuntu 14.04 to default python after uninstalling anaconda

I am new to ubuntu and the whole linux environment. I installed Anaconda on my system, but I would like to use the default python now for some reason. I removed the anaconda directory , but now the system can't find the python installation (obviously, but I dont know how to get to the right one).
Can someone write out a step-by-step as to how I revert back to the original python (the default one ubuntu 14.04 comes with) ?
Thank you
In your .bashrc file you will have a line something like, Anaconda adds it during the installation process:
export PATH=$HOME/anaconda/bin:$PATH
You need to remove that line, do a source .bashrc and type python, that should open a shell using your system default python.

Categories

Resources