Python Conda 环境和 .BAT 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24678144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:59:55  来源:igfitidea点击:

Conda environments and .BAT files

pythonanacondaconda

提问by Hans

I am setting up calls to python (Anaconda distribution) via BAT files and the windows task scheduler.

我正在通过 BAT 文件和 Windows 任务调度程序设置对 python(Anaconda 发行版)的调用。

I've now used environments for the first time and was trying to set a .batfile up as below:

我现在第一次使用环境并尝试设置.bat如下文件:

activate [my_env]
python my_script.py
deactivate

Unfortunately it appears that the second command does not get executed.

不幸的是,第二个命令似乎没有被执行。

采纳答案by Chris Burgoyne

Use the 'call' command when activating/deactivating the environment.

激活/停用环境时使用“call”命令。

call activate [my_env]
python my_script.py
call conda deactivate

See https://github.com/conda/conda/issues/794

https://github.com/conda/conda/issues/794

回答by BKay

Are you sure you need a batch file? I think this should work.

您确定需要批处理文件吗?我认为这应该有效。

cmd "/c activate [my_env] && python my_script.py && deactivate"

When I made a simple file containing

当我制作了一个包含

print("Hello")

Which I called myprint.py and ran

我称之为 myprint.py 并运行

cmd "/c activate anaconda33 && python myprint.py && deactivate"

This worked for me. You could also put this in a one line batch file.

这对我有用。您也可以将其放在一行批处理文件中。

回答by asmeurer

All activate does is put the environment in the front of the PATH. You can just call the absolute path to the pythonin the environment you want, like C:\Anaconda\python my-script.py.

activate 所做的就是将环境放在 PATH 的前面。你可以python在你想要的环境中调用绝对路径,比如C:\Anaconda\python my-script.py.