change python directory from conda - python

Recently, I installed miniconda, and because of which, my default python directory seems to have changed to,
/home/user/miniconda3/bin/python
Also, the default version seems to have changed to python 3.7 instead of python 2.7
And I cannot seem to install various libraries like tensorflow.
How do I get my python back to normal?

The miniconda installation has properly added itself to your PATH. You can check whether /home/user/miniconda3/bin is listed when you run
echo $PATH
in a terminal. If so, it most likely added some lines to the bottom of your ~/.bashrc file. Simply remove these lines yourself, and your PATH should be back to normal (save the file and open up a new terminal), meaning that typing e.g. python will give you the system Python.

Related

VSCode finding different python by default than the first one on the path

I'm using vscode on macOS and using nix to manage my environment. I do have python3.9 installed via homebrew as my system-default python, but I'm working on a project using a nix environment that is setting my path such that the only python executable available is python 3.8.
$ which python
/nix/store/mr2p1ycgj3wfppbw8ry2wwxg9r0xjz0z-python3-3.8.8-env/bin/python
$ whence python
/nix/store/mr2p1ycgj3wfppbw8ry2wwxg9r0xjz0z-python3-3.8.8-env/bin/python
$ whence python3
/nix/store/mr2p1ycgj3wfppbw8ry2wwxg9r0xjz0z-python3-3.8.8-env/bin/python
$ code .
The VSCode instance that comes up finds my 3.9 install as the default interpreter...
This causes a problem because the interpreter that's put on the path by nix is also the one with all my packages installed for this particular project, including developer utilities like black, flake8, pylint, etc... and my default 3.9 install does not necessarily have those.
It seems like there's a mechanism by which the Python extension discovers various python installs (I see in the extension output things where it tries to source virtualenvs and query pyenv), so what I think is happening is that it's finding a bunch of different python interpreters and blindly defaulting to the newest.
Is there any way to override the way the Python extension discovers python installs? I just want it to use the python interpreter that it finds on the path by default. I know I can override the interpreter per-project via the .vscode/settings.json, but that's a bit tedious to do for every project, and the way nix works that path may change occasionally. I already have "python.pythonPath": "python" in my vscode settings (which is the default anyway) - I was hoping that would work... it does not.
When we use "code." to open VS Code from the system terminal outside VS Code, it uses the latest version (highest version) of python installed in the system by default.
For directly opening an existing folder (containing python files) from VS Code, it defaults to using the python environment used last time, and for a new project that has not been selected, it also defaults to using the latest version (highest version) of python installed in the system .
You could click on the python environment in the lower left corner of VS Code, and then switch to the desired python environment in the options. In addition, it is recommended that you open the previously used project from VS Code (click File, Open Folder) and use the virtual environment in VS Code.

How to deploy python programs in Anaconda environments on Windows?

I am trying to use Anaconda and conda environments to allow Python programs for data acquisition* etc. to run from the (Anaconda) command line on Windows. The set up will be that the Python programs are installed to a particular location (cloned from Github), within %PATH% or whichever environment variable is more appropriate.
From an Anaconda command prompt in another directory and a particular conda environment, I want (both myself and other users) to be able run either python test.py <args> or test.py <args> (either solution is acceptable) and have a system wide conda environment run its Python to execute the program. test.py can/will have an appropriate shebang set.
Right now the python test.py calls the correct Python within the active conda environment, but cannot find the test.py program as Python won't search the %PATH% or similar looking for the program. test.py does something (Windows does not complain that the executable can't be found, and I've been playing with the file associations to get this far), but doesn't appear to start Python - a simple print function or raise statement as the only entry in the file does nothing.
I've tried setting file associations in Windows, but this hasn't changed anything. I've copied the py.exe/pyw.exe across to the Anaconda environments, with no change.
Is this something that can be done within Anaconda, or am I going to have to fall back on installing base Python directly and trying to use the launcher mechanism there?
Note that I'm also intending to deploy these programs on Raspbian, so any solutions, including non-Anaconda ones, that will work cross platform there would be worth extra effort on my part.
*these programs have significant usage of library packages for accessing external USB/GPIB/serial/ethernet connected lab equipment and use matplotlib, scipy, etc., hence the desire for a cloneable conda environment as the base environment.
It turns out the correct answer to this is fairly simple, but is fairly hard to find explained well. This might be a little clearer than the other answers I found:
Install the standalone launcher from pylauncher and add #!/usr/bin/env python shebangs to your scripts.
This should register .py files to Python.File and will find your Anaconda Pythons in appropriate environments. If you don't have a non-Anaconda python, it will use the Anaconda base environment (these two facts were the key element I was missing from various other answers around this problem that I had looked at, including the documents on python.org).
If you have a Python from python.org installed, then a standalone command line shell will use that, defaulting to Python 2.x, then Python 3.x. With #!/usr/bin/env python shebang, then a regular command shell will try to use python.org pythons first, then the Anaconda base environment. An Anaconda prompt will use the active environment. #! /usr/bin/env python2 or python3 will try to use python.org pythons only and fail if they are not found.
Installing Python 2.7 from python.org installers (and letting the installer set the file associations) will break pylauncher, and reinstalling will not fix it. Instead, set Computer\HKEY_CLASSES_ROOT\Python.File\Shell\open\command default value to "C:\WINDOWS\py.exe" "%L" %* to revert back to the pylauncher set up (assuming you used the launchwin.* packages to install it).

Python global environment screwed up

So, I've been toying around with python environments and I think i screwed mine up.
So when I ran python in shell, it would tell me i'm running on 2.7
I'm on windows 10, and i need a python switcher for my next project so i download pywin and used pywin to switch it to 3.5
the command i used was
pywin setdefault 3.5
now when i type python it says
python is not recognized as an internal or external command.
but py produces
Python 2.7.12
Now i can't use pip, easy_install, virtualenv
all of these commands i used to use, i suddenly no longer have access to them.
I tried switching back
but it wont even recognize pywin anymore.
The best way to check which Python version is executed is to check your environment variables. Another way of checking this is using the which command. (open cmd and run which python).
But, first you need to start a new cmd prompt to ensure your environment variables are not altered.
On Windows, but also any OS, you need to check the PATH and PYTHONPATH variables.
For Windows, follow the recommandations available in the Python documentation.
If you're not very experienced with working with windows, installations, and other similar things, I would recommend that you uninstall python, delete all versions/folders containing python (compiled) files (those that were installed with python, not the ones you've written) and then reinstall python. The installer should re-set the path variable to the correct location.

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.

Discover which version of Python is in PATH and how to change it

I am trying to use the the Rodeo IDE. However, according to the following error message, Rodeo can't find the right version of Python (I've installed the Anaconda distribution with Python 3.5, but according to the following message, that might not be the version of python Rodeo is trying to use):
"Bad news! Rodeo can't start. If you do have one of them installed, then the issue is that Rodeo is using the "wrong python". Rodeo defaults to using whatever python is on your PATH (or on Windows, whatever is set in your Environment Variables)."
Note, I use Mac OS 10.11.1.
I've tried to remove Python 2.7 (successfully, I think) and to reinstall Python 3.5 through Anaconda, but the trouble with Rodeo persists, I think because I have not changed the version of Python in my PATH.
How can I discover which version of Python is in my PATH and how could I change it to the version associated with the Anaconda distribution? Python beginner and answers can't be too simple.
In Terminal type
which python
to get the current python program path.
You can edit your ~/.bashrc and add the following with the path to your anacondas distribution at the end and then remove all other python paths in the .bashrc file.
export PATH=$PATH:/usr/local/lib/ <path to Anacondas...>
#for me it is /usr/local/anaconda/bin
Optional:
If you want to add other libraries / execute your own programs as if they were in the library or save yourself reinstalling everything, you can use the following:
export PYTHONPATH=/Library/Python/2.7/site-packages'
where you add the path of any libraries you wish to include.
If on Mac:
You have a .profile file instead of a bashrc. You therefore need to add the Path (above) to it and refresh your terminal. The easiest way is to use nano.
nano ~/.profile
#add:
export PATH=$PATH:/usr/local/anaconda/bin
# ctrl+x, then y, then rtn : to save and exit
#run bash or reopen terminal to refresh
bash
which python #should now be updated to anacondas path
I suggest try to use pyenv - https://github.com/yyuu/pyenv . It's simple to install and use and it has anaconda support. Also this plugin can be helpful - https://github.com/yyuu/pyenv-virtualenv .

Categories

Resources