Reputation: 21
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?
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
Upvotes: 1
Views: 2877
Reputation: 49086
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.
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.
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.
If the batch file activate
has .cmd
as file extension, then the second line in batch code must be adapted accordingly.
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.
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.
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:
Upvotes: 3