windows 如何使批处理文件在 anaconda 提示符下运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46305569/
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
How to make batch files run in anaconda prompt
提问by SuperNano
After installing anaconda3 in windows, I can run python commands from the anaconda prompt, but not from the windows command prompt.
I would like to make a desktop shortcut to activate my environment and run spyder from it. Previously, I would do this with a .bat
file, but now that I cannot run python commands from cmd.exe
this doesn't work.
在 Windows 中安装 anaconda3 后,我可以从 anaconda 提示符运行 python 命令,但不能从 Windows 命令提示符运行。我想制作一个桌面快捷方式来激活我的环境并从中运行 spyder。以前,我会用一个.bat
文件来做这个,但现在我不能从cmd.exe
这个文件中运行 python 命令。
Is there an alternative way of running batch files for the anaconda prompt?
I know that I could just modify my PATH
to get cmd.exe
to run python commands, but I'd like to avoid this if possible.
是否有另一种为 anaconda 提示符运行批处理文件的方法?我知道我可以修改 myPATH
来cmd.exe
运行 python 命令,但如果可能的话,我想避免这种情况。
采纳答案by Jeremy McGibbon
I believe all the Anaconda prompt does is open CMD and run a batch file. Make the first command of your script:
我相信 Anaconda 提示符所做的就是打开 CMD 并运行批处理文件。执行脚本的第一个命令:
call <anaconda_dir>/Scripts/activate.bat <anaconda_dir>
回答by N4v
Extending Jeremy's answer:
扩展杰里米的回答:
You do need to use call
for the "activate.bat" script as well as any subsequent Anaconda/Python-related commands. Otherwise the prompt will immediately quit after running the commands, even if you use a pause
statement. Please see below example:
您确实需要使用call
“activate.bat”脚本以及任何后续的 Anaconda/Python 相关命令。否则,即使您使用pause
语句,提示也会在运行命令后立即退出。请看下面的例子:
set root=C:\Users\john.doe\AppData\Local\Continuum\anaconda3
call %root%\Scripts\activate.bat %root%
call conda list pandas
pause
回答by ivan_pozdeev
Add
添加
call "<anaconda_dir>\Scripts\activate.bat"
to the start of your script (it doesn't actually require an argument, and it activates the base
environment by default).
到脚本的开头(它实际上不需要参数,并且base
默认情况下会激活环境)。
Note that after this line, you can make use of the CONDA_
envvars!
请注意,在此行之后,您可以使用CONDA_
envvars!
回答by JedB
Thanks to this thread I solved my challenge to get a windows batch file to open the Ananconda Prompt and then run some python code.
感谢这个线程,我解决了我的挑战,即获取 Windows 批处理文件以打开 Ananconda Prompt,然后运行一些 python 代码。
Here is the batch file:
这是批处理文件:
@echo on
call C:\ProgramData\Anaconda3\Scripts\activate.bat
C:\ProgramData\Anaconda3\python.exe "D:\Documents\PythonCode\TFLAPI\V1.py"
回答by melancholic_maclean
For Windows, use the following script in your batch file to execute a Python script. Simply change your personal file paths.
对于 Windows,请在批处理文件中使用以下脚本来执行 Python 脚本。只需更改您的个人文件路径。
cmd /c C:\ProgramData\Anaconda3\condabin\conda.bat run "C:\ProgramData\Anaconda3\python.exe" "C:\Users\User Name\Path to your Python File\Python File.py"
cmd /c C:\ProgramData\Anaconda3\condabin\conda.bat run "C:\ProgramData\Anaconda3\python.exe" "C:\Users\User Name\Path to your Python File\Python File.py"
回答by DINA TAKLIT
As an alternative to the above solution if you are having windows os. You can use git bash
如果您使用的是 Windows 操作系统,则作为上述解决方案的替代方案。你可以使用 git bash
you need to add the path to
conda.sh
to you.bash_profile
or whatever it is named to be able to run conda commands. here is an example:echo ". C:/Users/user/Anaconda3/etc/profile.d/conda.sh" >> ~/.bash_profile
Run your script =>
. script.sh
你需要把路径添加到
conda.sh
你.bash_profile
或任何它被命名为能够运行畅达命令。这是一个例子:echo ". C:/Users/user/Anaconda3/etc/profile.d/conda.sh" >> ~/.bash_profile
运行你的脚本 =>
. script.sh
It would be a good alternative too.
这也将是一个不错的选择。
Check thisand thisfor more details. Hope it will help someone :).
回答by Bill Moore
Powershell Version:
Powershell 版本:
$qtconsole="C:\Users\<YourUserName>\.anaconda\navigator\scripts\qtconsole.bat"
start-process $qtconsole -WindowStyle Hidden
Note: this script will only start one instance of the qtconsole at a time due to DLL limitations of Linux QT GUI library only supporting one instance of the same exe running at the same time. That's probably why they use "Anaconda Navigator" to launch the QtConsole programs to get around this restriction.
注意:由于 Linux QT GUI 库的 DLL 限制,此脚本一次只能启动一个 qtconsole 实例,仅支持同一 exe 的一个实例同时运行。这可能就是为什么他们使用“Anaconda Navigator”来启动 QtConsole 程序来绕过这个限制。