Allow use of system python in conda env?
Asked Answered
A

3

14

Is there a way to force conda to use the system version of python (along with all of the system libraries) in a given env?

I have conda enabled by default in my shell, which can get a bit annoying, because if I try to run a system python app, it gets a different version of python to what it is expecting (python still defaults to 2.7 on *buntu), and often won't run. I would like the root env of conda to just be a redirect to the system python install.

Aubine answered 26/4, 2015 at 0:30 Comment(4)
Could you give an example app/experience, I've not seen this.Across
@AndyHayden: I'm using python 3.4 in my conda root env, and so basically any ubuntu package that has !#/bin/env python at the start of the main script will fail if run from the command line (because it's expecting python 2, which is still the default). My .profile and .zshrc both have the lines # added by Miniconda3 3.9.1 installer ; export PATH="/home/naught101/miniconda3/bin:$PATH" in them, so conda is enabled in all shells by default.Aubine
Do you think this a bug in these applications? What applications? Can you install that application with conda (rather than apt-get)?Across
@AndyHayden: Every python-based package installed via apt that doesn't specify a python version. Try grep -Hn 'env python' /usr/bin/*. For example fslint-gui. Yes, it's probably better that those packages specify the python version, but I suspect that there are a lot of packages that don't in the ubuntu repos, on the basis that python2 is default and can be expected. I feel like it should be possible to completely disable conda, and use only the system python install. I guess I could probably just /home/naught101/miniconda3/bin/python, but I'm not sure if that has other consequences.Aubine
D
1

You need to edit all user shell run commands such as your .bashrc file to prepend the bin directory of anaconda to path export PATH=~/anaconda/bin:$PATH, while in your root run commands append export PATH=$PATH:~/anaconda/bin. In both cases you will have access to the conda command. You can check which python will be run by typing $env python --version. You can also check which other versions would be available and their order of priority (if the other is removed) by using $type -a python. Of course ensure your executable python files have #!/usr/bin/env python and not some other direct route to a python executable. For further info Google BASH Shell look up queries like http://www.cyberciti.biz/tips/an-example-how-shell-understand-which-program-to-run-part-ii.html.

Dottie answered 29/4, 2015 at 17:52 Comment(3)
@naught101, I do believe what is explained above would work: if you edit the run commands that are executed under the root user so that the exported PATH reads export PATH="$PATH:/home/naught101/miniconda3/bin" (they would reside under both root and user's home via $cd ~ ), any file/script/etc that is executed under root would look up the default python before your conda version as explained and you should be able to verify using the methods above.Dottie
Yeah, it does make the system python default when a shell starts, which is good, but if you source activate blah and then source deactivate, then the conda root env python becomes the default again, until you restart the shell.Aubine
@Aubine you may try this: export PATH=$(conda ..deactivate)Grubby
A
1

Simply removing the python symlink from ~/miniconda3/bin/ appears to do the job.

$ which python           
/home/naught101/miniconda3/bin/python
$ rm /home/naught101/miniconda3/bin/python
$ which python                            
/usr/bin/python
$ source activate science                 
discarding /home/naught101/miniconda3/bin from PATH
prepending /home/naught101/miniconda3/envs/science/bin to PATH
(science)$ which python           
/home/naught101/miniconda3/envs/science/bin/python
(science)$ source deactivate                       
discarding /home/naught101/miniconda3/envs/science/bin from PATH
$ which python     
/usr/bin/python

So far, this doesn't seem to have caused me any problems. Unfortunately the same doesn't work for ~/miniconda/bin/python3, because conda requires it when switching to other envs that use the same python version. However, that one hasn't caused as many problems in the first place.

If this does cause problems, it's easy enough to undo, just cd ~/miniconda/bin/; ln -s python3 python (or what ever version of python you're using in your conda root env). You may need to activate/deactivate an env to get that version of python back on your PATH.

Aubine answered 5/5, 2015 at 3:25 Comment(0)
F
1

If you are in (base) or another environment, use conda deactivate, this will exit conda's environment and place you back into the OS environment:

on MacOS

(base) user$ python --version
Python 3.9.12
(base) user$ conda deactivate
user$ python --version
Python 2.7.16

on Linux (with no base python installed)

(base) user$ python --version
Python 3.9.15
(base) user$ conda deactivate
user$ python --version
-bash: python: command not found
Freemasonry answered 30/1, 2023 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.