Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using conda install within a python script

Tags:

According to this answer you can import pip from within a Python script and use it to install a module. Is it possible to do this with conda install?

The conda documentation only shows examples from the command line but I'm looking for code that can be executed from within a Python script.

Yes, I could execute shell commands from within the script but I am trying to avoid this as it is basically assuming that conda cannot be imported and its functions called.

like image 364
slaw Avatar asked Jan 20 '17 15:01

slaw


People also ask

Can I install Python using conda?

The Earth Engine Python API can be installed to a local machine via conda, a Python package and environment manager.

Can I use both pip and conda?

Built into Anaconda, conda is a powerful package manager and environment manager that you use with command-line in the Anaconda Prompt for Windows, or in a terminal window for macOS or Linux. pip is the standard package manager for python, meaning you can use it both inside and outside of Anaconda.

How do you use conda in Python?

If you want to use a different version of Python, for example Python 3.5, simply create a new environment and specify the version of Python that you want. When conda asks if you want to proceed, type "y" and press Enter. Activate the new environment: Windows: conda activate snakes.

Can you install conda through pip?

If you ONLY want to have a conda installation. Just remove all of the other python paths from your PATH variable. This allows you to just use pip install * and it will install straight into your conda installation.


2 Answers

You can use conda.cli.main. For example, this installs numpy:

import conda.cli  conda.cli.main('conda', 'install',  '-y', 'numpy') 

Use the -y argument to avoid interactive questions:

-y, --yes Do not ask for confirmation.

like image 146
Mike Müller Avatar answered Sep 30 '22 17:09

Mike Müller


I was looking at the latest Conda Python API and noticed that there are actually only 2 public modules with “very long-term stability”:

  1. conda.cli.python_api
  2. conda.api

For your question, I would work with the first:

NOTE: run_command() below will always add a -y/--yes option (i.e. it will not ask for confirmation)

import conda.cli.python_api as Conda import sys  ################################################################################################### # The below is roughly equivalent to: #   conda install -y 'args-go-here' 'no-whitespace-splitting-occurs' 'square-brackets-optional'  (stdout_str, stderr_str, return_code_int) = Conda.run_command(     Conda.Commands.INSTALL, # alternatively, you can just say "install"                             # ...it's probably safer long-term to use the Commands class though                             # Commands include:                             #  CLEAN,CONFIG,CREATE,INFO,INSTALL,HELP,LIST,REMOVE,SEARCH,UPDATE,RUN     [ 'args-go-here', 'no-whitespace-splitting-occurs', 'square-brackets-optional' ],     use_exception_handler=True,  # Defaults to False, use that if you want to handle your own exceptions     stdout=sys.stdout, # Defaults to being returned as a str (stdout_str)     stderr=sys.stderr, # Also defaults to being returned as str (stderr_str)     search_path=Conda.SEARCH_PATH  # this is the default; adding only for illustrative purposes ) ###################################################################################################  


The nice thing about using the above is that it solves a problem that occurs (mentioned in the comments above) when using conda.cli.main():

...conda tried to interpret the comand line arguments instead of the arguments of conda.cli.main(), so using conda.cli.main() like this might not work for some things.


The other question in the comments above was:

How [to install a package] when the channel is not the default?

import conda.cli.python_api as Conda import sys  ################################################################################################### # Either: #   conda install -y -c <CHANNEL> <PACKAGE> # Or (>= conda 4.6) #   conda install -y <CHANNEL>::<PACKAGE>  (stdout_str, stderr_str, return_code_int) = Conda.run_command(     Conda.Commands.INSTALL,     '-c', '<CHANNEL>',     '<PACKAGE>'     use_exception_handler=True, stdout=sys.stdout, stderr=sys.stderr ) ###################################################################################################  
like image 43
YenForYang Avatar answered Sep 30 '22 18:09

YenForYang