Saturday, February 5, 2022

[SOLVED] Problems with venv & localhost when automating deployment with .bat file

Issue

I am trying to create a batch file, to launch my app with one click on Windows. I have:

cd "C:\Users\<project_dir>"
venv\scripts\activate
python __init__.py
start /d "C:\Program Files (x86)\Google\Chrome\Application\" chrome.exe localhost:5000

Problem 1: Although venv\scripts\activate works manually, it does not work in the batch file (I've also tried start /d).

Problem 2: launching the Flask server with python __init__.py causes the batch script to pause, so that the browser is never launched.


Solution

This should work:

cd "C:\Users\<project_dir>"
start venv\scripts\python __init__.py
start /d "C:\Program Files (x86)\Google\Chrome\Application\" chrome.exe localhost:5000

I have addressed your problem #1 by using the virtual environment directly, without activating it. You can also do call venv\scripts\activate and then invoke your script with start python __init__.py.

The problem #2 is solved by running the server via start so that it spawns a new process.



Answered By - Miguel
Answer Checked By - Clifford M. (WPSolving Volunteer)