从Windows批处理文件运行python脚本的问题

时间:2021-12-12 02:31:59

I have created a .bat file where i want to automatize the run of an opensource program. As you may see i am using the code below but once the virtual env is activated, it does not change directory from there so it may run a python script. I have tried several modifications found here but none of it seemed to work for me. What should i do so for the script to work properly?

我创建了一个.bat文件,我希望自动化一个开源程序的运行。正如您可能看到我正在使用下面的代码但是一旦激活虚拟环境,它就不会从那里更改目录,因此它可能运行python脚本。我在这里尝试了几个修改,但似乎没有一个适合我。我该怎么做才能使脚本正常工作?

Thank you.

rem Virtual environment works
cd "C:\Projects"
start ENV\Scripts\activate

rem DOES NOT CHANGE THE DIRECTORY SO IT MAY RUN PYTHON SERVER, TRIED ALSO SEVERAL MODIFICATIONS BUT STILL HAVE THE SAME ISSUE, ALSO PYTHON WONT START
rem COMMAND TO START SERVER--> python manage.py runserver

cd "C:\Projects\my_project"
start C:\Python27\python.exe C:\Projects\my_project\manage.py  runserver

1 个解决方案

#1


1  

Use this batch code:

使用此批次代码:

cd /D "C:\Projects"
call ENV\Scripts\activate.bat
start "Run Server" /D "C:\Projects\my_project" C:\Python27\python.exe C:\Projects\my_project\manage.py runserver

The command CD without option /D does nothing if the current directory and specified directory are not on same drive. Therefore it is advisable to use always option /D on specifying full path of a directory which should become the current directory.

如果当前目录和指定目录不在同一驱动器上,则没有选项/ D的命令CD不执行任何操作。因此,建议在指定应成为当前目录的目录的完整路径时始终使用选项/ D.

The command START runs a batch file or console application in a new command process which is executed parallel to current command process on not using additionally the option /WAIT to halt current command process until started command process terminated.

命令START在新的命令进程中运行批处理文件或控制台应用程序,该进程与当前命令进程并行执行,而不另外使用选项/ WAIT来暂停当前命令进程,直到启动命令进程终止。

The batch file obviously sets environment variables. This is done in the additional command process which has no effect on environment variables of current command process because each process has its own list of environment variables copied on start of the process by Windows from the current process.

批处理文件显然设置了环境变量。这是在附加命令过程中完成的,该过程对当前命令进程的环境变量没有影响,因为每个进程都有自己的环境变量列表,这些变量是在Windows从当前进程开始的过程中复制的。

If the batch file activate has .cmd as file extension, then the second line in batch code must be adapted accordingly.

如果激活的批处理文件具有.cmd作为文件扩展名,则必须相应地调整批处理代码中的第二行。

The command CALL is needed to call the batch file which sets the environment variables now in environment of current command process. Once the execution of this called batch file finished, the current command process continues with third line of current batch file execution, except the batch file activate contains command exit without option /B or a syntax error.

调用批处理文件需要命令CALL,该批处理文件现在在当前命令进程的环境中设置环境变量。完成此调用批处理文件的执行后,当前命令进程将继续执行当前批处理文件的第三行,但批处理文件激活包含不带选项/ B的命令exit或语法错误。

Last the command START is used to run Python in a new command process with the environment variables copied by Windows from current command process with C:\Projects\my_project set as current directory.

最后,命令START用于在新的命令进程中运行Python,其中Windows从当前命令进程复制环境变量,并将C:\ Projects \ my_project设置为当前目录。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

要了解使用的命令及其工作方式,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • call /?
  • cd /?
  • start /?

See also the Stack Overflow answers on the questions:

另请参阅有关问题的Stack Overflow答案:

#1


1  

Use this batch code:

使用此批次代码:

cd /D "C:\Projects"
call ENV\Scripts\activate.bat
start "Run Server" /D "C:\Projects\my_project" C:\Python27\python.exe C:\Projects\my_project\manage.py runserver

The command CD without option /D does nothing if the current directory and specified directory are not on same drive. Therefore it is advisable to use always option /D on specifying full path of a directory which should become the current directory.

如果当前目录和指定目录不在同一驱动器上,则没有选项/ D的命令CD不执行任何操作。因此,建议在指定应成为当前目录的目录的完整路径时始终使用选项/ D.

The command START runs a batch file or console application in a new command process which is executed parallel to current command process on not using additionally the option /WAIT to halt current command process until started command process terminated.

命令START在新的命令进程中运行批处理文件或控制台应用程序,该进程与当前命令进程并行执行,而不另外使用选项/ WAIT来暂停当前命令进程,直到启动命令进程终止。

The batch file obviously sets environment variables. This is done in the additional command process which has no effect on environment variables of current command process because each process has its own list of environment variables copied on start of the process by Windows from the current process.

批处理文件显然设置了环境变量。这是在附加命令过程中完成的,该过程对当前命令进程的环境变量没有影响,因为每个进程都有自己的环境变量列表,这些变量是在Windows从当前进程开始的过程中复制的。

If the batch file activate has .cmd as file extension, then the second line in batch code must be adapted accordingly.

如果激活的批处理文件具有.cmd作为文件扩展名,则必须相应地调整批处理代码中的第二行。

The command CALL is needed to call the batch file which sets the environment variables now in environment of current command process. Once the execution of this called batch file finished, the current command process continues with third line of current batch file execution, except the batch file activate contains command exit without option /B or a syntax error.

调用批处理文件需要命令CALL,该批处理文件现在在当前命令进程的环境中设置环境变量。完成此调用批处理文件的执行后,当前命令进程将继续执行当前批处理文件的第三行,但批处理文件激活包含不带选项/ B的命令exit或语法错误。

Last the command START is used to run Python in a new command process with the environment variables copied by Windows from current command process with C:\Projects\my_project set as current directory.

最后,命令START用于在新的命令进程中运行Python,其中Windows从当前命令进程复制环境变量,并将C:\ Projects \ my_project设置为当前目录。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

要了解使用的命令及其工作方式,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • call /?
  • cd /?
  • start /?

See also the Stack Overflow answers on the questions:

另请参阅有关问题的Stack Overflow答案: