在python脚本中使用conda安装 [英] Using conda install within a python script
问题描述
根据此 answer 您可以导入从Python脚本中点入并使用它来安装模块.可以用conda install
来做到这一点吗?
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
?
conda文档仅显示了命令行示例,但我正在寻找可以在Python脚本中执行的代码.
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.
是的,我可以在脚本中执行shell命令,但是我试图避免这种情况,因为它基本上是假设无法导入conda并调用其函数.
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.
推荐答案
我正在查看最新的 Conda Python API ,并注意到实际上只有两个具有非常长期稳定性"的公共模块:
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
conda.cli.python_api
conda.api
对于您的问题,我会处理第一个问题:
For your question, I would work with the first:
注意:下面的run_command()
将始终 添加-y
/--yes
选项(即它不会要求确认 )
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()
时出现的问题(在上面的注释中提到):
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试图解释comand行参数而不是conda.cli.main()的参数,因此像这样使用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
)
###################################################################################################
这篇关于在python脚本中使用conda安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!