1

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

1 Answer 1

3

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:

3
  • Without /b, the start command tells CreateProcess to create a new console, if that's what you mean by a "new command process". In Windows 7, this has the Windows server (csrss.exe) create a new console (conhost.exe) and attach the process to it. In Windows 8+, startup code in the process (kernelbase.dll) creates and attaches to a console via file handles it opens on the console device (\Device\ConDrv). It opens "Server", "Reference", and "Connect" to create and attach to the console. Then it opens "Input" as its StandardInput and "Output" as its StandardOutput and StandardError.
    – Eryk Sun
    Commented Jul 10, 2017 at 17:32
  • @Mofi i have tried the code but it does not seem to work for me...also the cmd closes rapidly so i have included "pause" command so to check its progress but it also closes after execution so i cant run the Elasticsearch afterwards with "start C:\Projects\elasticsearch-5.2.1\bin\elasticsearch ". Maybe there is another work around? checked the hints/help you gave me they are very informative. Thanks in advance!
    – George L
    Commented Jul 13, 2017 at 13:36
  • @GeorgeL Read Debugging a batch file and make use of these information. The batch file execution fails because of a syntax error output by Windows command interpreter either in your batch file like saving the file as Uniode file with BOM instead of ANSI file or in called batch file activate. If the later is the reason we can't help you without seeing the code of this batch file and the output error message.
    – Mofi
    Commented Jul 13, 2017 at 14:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.