73

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?

7 Answers 7

113

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

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

5 Comments

FYI: this also works when using virtualenvwrapper(-win), e.g: workon myenv & cd c:\AppPath & python my_app.py
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
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
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"
I had to use double ampersands (&&) on Windows 10 command console.
53

runserver.bat:

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

1 Comment

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
13

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.

3 Comments

The path should be bin/python.exe
Note that, particularly on windows, this method will not see any changes to the environment variables made in the activate.bat script
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'
5

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.

1 Comment

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
3

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.

Comments

2

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.

Comments

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

1 Comment

What does this add that isn't covered in the other answers yet?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.