1

Can anyone give me some advice on automating the start up of my virtualenv app on Windows? I have a small Flask app that runs on gunicorn. It runs fine, but how do I put it into production? I don't want to have to go in manually and cd into the directory and type activate and then gunicorn app:blog. How does one go about employing a virtualenv? Here is what I've tried scripting:

echo off
cd C:\Users\Darkn\Code\Python\flask-intro
venv\scripts\activate.bat
venv\scripts\waitress-serve --port=5000 app:app

The first two lines get executed, but the last line doesn't do anything.

Darc Nawg
  • 1,415
  • 3
  • 15
  • 25
  • What is it about basic Windows scripting that it isn't able to do here? What have you tried? – Carlos Nov 21 '14 at 18:39
  • Well, in a batch script i can get it to activate, but cant seem to get it to start python. I Will post the file. – Darc Nawg Nov 21 '14 at 18:41
  • Add a pause at the end to see what happens. – Carlos Nov 21 '14 at 19:11
  • It stil leaves me cd'd in my projectdir with virtualenv activated. That is as far as ive gotten. – Darc Nawg Nov 21 '14 at 19:17
  • Instead of relative paths, have you tried full paths? – Carlos Nov 21 '14 at 19:20
  • Carlos, Thanks. Yes I've tried that in my script as well. It seems that once the env is activated that it runs in a sandbox and doesn't execute any commands through absolute paths. I've even just tried to run python.exe instead of waitress, but it responds as if you only typed in a blank line. No errors or anything of that nature. – Darc Nawg Nov 22 '14 at 09:07

3 Answers3

3

The activate script from virtualenv gave me some clues. The trick was to prepend the virtualenv path to the system path. Then the script could just cd into the project directory and start the app.

@echo off
set "VIRTUAL_ENV=C:\Users\Darkn\Code\Python\flask-intro\venv"
set "PATH=%VIRTUAL_ENV%\Scripts;%PATH%"
cd C:\Users\Darkn\Code\Python\flask-intro
waitress-serve --port=5000 app:app
admdrew
  • 3,790
  • 4
  • 27
  • 39
Darc Nawg
  • 1,415
  • 3
  • 15
  • 25
1

I used the answer by Darc Nawg to install a windows service using WinSW with the following xml config file.

<service>
    <id>com.taxicabmanager.django</id>
    <name>Taxicab Manager Django</name>
    <description>Industry standard Django and GraphQL components of Taxicab Manager.</description>
    <env name="VIRTUAL_ENV" value="C:\source\taxicab-manager-django\env-taxicab-manager-django"/>
    <env name="PATH" value="%VIRTUAL_ENV%\Scripts;%PATH%"/>
    <workingdirectory>C:\source\taxicab-manager-django</workingdirectory>
    <executable>waitress-serve</executable>
    <arguments>--port=2003 --url-scheme=http api.wsgi:application</arguments>
    <logmode>rotate</logmode>
    <delayedAutoStart/>
    <onfailure action="restart" />
</service>

Keith John Hutchison
  • 4,955
  • 11
  • 46
  • 64
0

From somewhere else:

@ECHO OFF
:<name>
CALL "C:\path\to\activate.bat"
python -O -m <env> <script>

IF %ERRORLEVEL% NEQ 0 (
    ECHO Restarting <name>...
    GOTO <name>
)