0

hi i have a question how can run a python script in Batch file

i have a setup file for my flask app and every time should set up some environment variable and run app.py

i create a setup for this but i don't know how run python app and it's not working

my setup.cmd file looks like this:

set app_debug=True
set API_KEY=secret
set SECRET_KEY=development mode
set WTF_CSRF_SECRET_KEY=development mode
set email_password=development mode
set RECAPTCHA_PUBLIC_KEY=secret
set RECAPTCHA_PRIVATE_KEY=secret

./venv/Scripts/activate.bat
python app.py

and it's goes to ./venv/Scripts/activate.bat and then stop and don't work

Mofi
  • 46,139
  • 17
  • 80
  • 143
abcdefg
  • 9
  • 2
  • `./venv/Scripts/activate.bat` is wrong and correct would be `call "%~dp0venv\Scripts\activate.bat"` which __calls__ the batch file `activate.bat` in subdirectory `Scripts` in subdirectory `venv` in the __directory of the currently processed batch file__ independent on current directory, or `call ".\venv\Scripts\activate.bat"` or just `call venv\Scripts\activate.bat` to __call__ the batch file `activate.bat` in subdirectory `Scripts` in subdirectory `venv` in the __current directory__ independent on batch file directory. – Mofi Jan 08 '23 at 13:35
  • I recommend to read also: [How to call a batch file that is one level up from the current directory?](https://stackoverflow.com/a/24725044/3074564) It explains all four methods which exist to execute a batch file from within a batch file and what are the differences in execution behavior. There should be read also: [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) and the Microsoft documentation about [Naming Files, Paths, and Namespaces](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file). – Mofi Jan 08 '23 at 13:38

3 Answers3

1

I think you have to “call” an external batch file if you want execution to return your current batch file. So write

call ./venv/Scripts/activate.bat

in your batch file.

Wilf Rosenbaum
  • 518
  • 3
  • 10
0

Consider having a .env file to store your environment variables.

From your app.py file you can load those env vars.

Reference: https://towardsdatascience.com/environment-variables-python-aecb9bf01b85

Example:

import os
env_var = os.environ.get('ENV')
if not env_var:
    print('ENV environment variable does not exist')
cavalcantelucas
  • 1,362
  • 3
  • 12
  • 34
0

I face the same issue with my flask app virtual enviroment setup.bat script, I resolve this issue using below script.

setup.bat

python -m venv venv
call venv\Scripts\activate.bat
pip install -r requirements.txt
call venv\Scripts\deactivate.bat

where, You can see I have used keyword call which will call the activate.bat file and keep my virtual environment activated while I am doing my pip install. Once it is done I have deactivated the virtual environment using the same call command in the batch file.

Sanjay R B
  • 39
  • 4