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

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. Commented Jan 8, 2023 at 13:35
  • I recommend to read also: How to call a batch file that is one level up from the current directory? 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? and the Microsoft documentation about Naming Files, Paths, and Namespaces. Commented Jan 8, 2023 at 13:38

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.