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.
The Earth Engine Python API can be installed to a local machine via conda, a Python package and environment manager.
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.
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.
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.
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.
I was looking at the latest Conda Python API and noticed that there are actually only 2 public modules with “very long-term stability”:
conda.cli.python_api
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 ) ###################################################################################################
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 ) ###################################################################################################
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With