简体   繁体   中英

how run a python script via Batch file

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

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.

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')

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM