55

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

C:\Users\Admin\Desktop\venv\Scripts\

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

C:\Users\Admin\Desktop\helloworld\

How should I do? What modules should I use?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
ugavetheroses
  • 561
  • 1
  • 5
  • 6

7 Answers7

89

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

Ashish Gupta
  • 2,174
  • 1
  • 23
  • 53
  • 5
    FYI: this also works when using virtualenvwrapper(-win), e.g: `workon myenv & cd c:\AppPath & python my_app.py` – tutuDajuju Apr 22 '17 at 06:55
  • 1
    downvoted. Our code looks like this, probably thanks to this answer read by someone here a while back: python->bat->python->bat->python->bat. impossible to debug. PLEASE, use ONLY python in your code. .bat inside python is the devil – Gulzar Aug 04 '19 at 16:43
  • In my own experience, activating the virtual env. does not by default run the Python from it. I still have to specify I am running Python iut of the venv – hyankov Jan 11 '20 at 18:13
  • I had to do a bit more faffing to get the modules importing correctly with venv and the -m flag. Hope this helps someone: `cmd /k "cd /d C:\Users\User\repos\Project\venv\Scripts\ & activate & cd /d "C:\Users\User\repos\Project" & C:\Users\User\repos\Project\venv\Scripts\python.exe -m MyProject.main MyProject/main.py"` – Slate Oct 26 '20 at 13:37
  • I had to use double ampersands (&&) on Windows 10 command console. – Robert Simpson Jan 16 '21 at 05:12
32

runserver.bat:

 CALL [your path]\Scripts\activate.bat
 python manage.py runserver
Weihui Guo
  • 2,600
  • 4
  • 25
  • 45
  • This seems to run, but when using it to activate a `venv`, and then running `sphinx-build -b html source build` inside it, it does not recognize any changes in the files: ```@echo on CALL venv\Scripts\activate.bat sphinx-build -b html source build``` – Marie. P. Dec 06 '20 at 20:51
  • 2
    This worked for me. – Robert Simpson Jan 16 '21 at 05:35
11

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.

Mikko Ohtamaa
  • 69,174
  • 40
  • 208
  • 346
  • The path should be bin/python.exe – hd1 Jun 19 '15 at 00:13
  • 7
    Note that, particularly on windows, this method will not see any changes to the environment variables made in the activate.bat script – phil_20686 Apr 12 '16 at 19:26
  • Same Idea for specifying & activating Anaconda virtual enviorenments. save the below codes as .cmd file. 'C:\Users\Jesus\Miniconda3\envs\MyEnvornment01\python.exe MyAPP.py' – Albert H Mar 01 '19 at 21:39
2

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.

Andy
  • 46
  • 2
  • This is what worked for me to run my_script.py using the myenv virtual environment: `call C:\ProgramData\Anaconda3\Scripts\activate.bat myenv` then `python C:\MyProject\my_script.py` – Garland Pope Mar 26 '21 at 19:03
1

Rather than using strings you can use a caret (^) as described in this question: Long commands split over multiple lines in Windows Vista batch (.bat) file

E.g.

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

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
phil_20686
  • 3,712
  • 18
  • 38
0

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.

0

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

pause