[英]How to get an executable for a Python script to be run using anaconda environment in Windows?
I don't want to convert my .py file to .exe file.我不想将我的 .py 文件转换为 .exe 文件。 Rather I want a simple .exe file which can activate my anaconda virtual environment and run the script kept at a particular directory path.
相反,我想要一个简单的 .exe 文件,它可以激活我的 anaconda 虚拟环境并运行保存在特定目录路径中的脚本。 Any idea how to do it!
知道怎么做!
Suppose you have an Anaconda environment with name saputara with which you want to run hello_world.py kept at the directory *E:\\vaibhav\\work*.假设您有一个名为saputara的 Anaconda 环境,您想用它运行hello_world.py保存在目录 *E:\\vaibhav\\work* 中。 Copy following peace of code to the notepad
将以下代码复制到记事本
E:
cd vaibhav\work\
call C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
call activate saputara
python hello_world.py
pause
Now save this file with extension .bat .现在用扩展名.bat保存这个文件。 Double clicking on this file will automatically execute hello_world.py
双击这个文件会自动执行hello_world.py
use the bat scripting to get your task done.使用 bat 脚本来完成您的任务。
@echo off
conda activate env_name
python file_name.py
copy this to a notepad and save it as a .exe file.将其复制到记事本并将其另存为 .exe 文件。 This will activate a conda environment there and run the python script.
这将在那里激活 conda 环境并运行 python 脚本。
@echo off
cd %1
conda activate env_name
python %2.py
run this using cmd cause it takes two parameters.使用 cmd 运行它,因为它需要两个参数。 first the directory path and second the file name you can google if you wanna know more about bat scripting.
首先是目录路径,其次是文件名,如果您想了解有关 bat 脚本的更多信息,可以在 google 上搜索。 You can add it to path as well to use it from anywhere.
您也可以将其添加到路径以从任何地方使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.