A Python script that activates the virtualenv and then runs another Python script?


Python Problem Overview

On Windows Vista, I need a script that starts the activate (to activate the virtualenv) script in:


And later, in the virtual environment, starts to the manage.py runserver in the folder:


How should I do? What modules should I use?

Python Solutions

Solution 1 - Python

You can activate your virtualenv and then start server using a bat file. Copy this script in to a file and save it with .bat extension (eg. runserver.bat)

@echo off
cmd /k "cd /d C:\Users\Admin\Desktop\venv\Scripts & activate & cd /d    C:\Users\Admin\Desktop\helloworld & python manage.py runserver"

Then you can just run this bat file (just double click) to start the server

Solution 2 - Python


 CALL [your path]\Scripts\activate.bat
 python manage.py runserver

Solution 3 - Python

If you want call virtualenv'ed Python directly you can do something like this:

 C:\Users\Admin\Desktop\venv\Scripts\bin\python.exe manage.py runserver

Double check python.exe location on your virtualenv folder - don't remember how it is out of my head. This Python associates itself with the virtualenv and uses its site-packages by default.

Solution 4 - Python

I am using Anaconda 3 and python 3.7.6 on Windows. Had to do this in my .bat file:

CALL path\to\base\virtual\environment\Scripts\activate.bat path\to\your\virtual\environment [path\to\your\virtual\environment]python.exe path\to\your\script\yoursript.py

Without activate.bat nothing works. I was getting an error about mkl-server. This error is described here https://github.com/numpy/numpy/issues/15523. People complained there about conda being broken, i.e. just calling python.exe yoursript.py does not work.

Solution 5 - Python

For me the above didn't work and therefore I will provide a more general answer.

But first specifically, this worked for me:

  1. Open a notepad
  2. paste this:
@echo off
CALL c:\1\env\Scripts\activate.bat
python c:\1\app.py runserver
  1. save as whatever.bat
  2. double-click this file to run

And generally: it is important to locate "activate.bat" under your python project. My project in this case was in c:\1 and the activate.bat under the relative directory env\Scripts which apparently may be situation dependent or have changed over time. This makes the general script:

@echo off
CALL [Your python project path]\[the relative path of your activate.bat]\activate.bat
python [Your python project path]\[your python filename].py runserver

In my case the project path was: c:\1 The relative path: env\Scripts And the python filename: app

When I make a virtual environment the env files are placed relative to my python file. Just in case your situation is like in the question the call line in the script would change to CALL [your activate.bat location]\activate.bat

i.e. in this situation the following should work:

@echo off
CALL C:\Users\Admin\Desktop\venv\Scripts\activate.bat
python C:\Users\Admin\Desktop\helloworld\manage.py runserver

Tip: I just found that python took my desktop as the working directory. It may therefore be a good idea to change your working directory to your python path. In my case adding cd\1 under @echo off does that trick.

Solution 6 - Python

Rather than using strings you can use a caret (^) as described in this question: https://stackoverflow.com/questions/69068/long-commands-split-over-multiple-lines-in-windows-vista-batch-bat-file


cmd /k cd path/to/activate ^
pip uninstall --yes package ^
pip install git+https://git.server.com/user/project@remote/branch ^

will open a venv and uninstall and reinstall a branch of a Git repository. This is a useful pattern for automating deployment of code into a venv.

Solution 7 - Python

For me, working with this code: (script_file.bat)

@echo off

CALL C:\Users\apo1979\Anaconda3\Scripts\activate.bat PyPWBI

C:/Users/apo1979/Anaconda3/envs/PyPWBI/python.exe "d:/.APO_OneDrive/script_SpeedTest.py" runserver



All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionugavetherosesView Question on Stackoverflow
Solution 1 - PythonAshish GuptaView Answer on Stackoverflow
Solution 2 - PythonWeihui GuoView Answer on Stackoverflow
Solution 3 - PythonMikko OhtamaaView Answer on Stackoverflow
Solution 4 - PythonAndyView Answer on Stackoverflow
Solution 5 - PythonSebastiaan35View Answer on Stackoverflow
Solution 6 - Pythonphil_20686View Answer on Stackoverflow
Solution 7 - PythonAnderson Paim de OliveiraView Answer on Stackoverflow