ugavetheroses
ugavetheroses

Reputation: 741

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

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?

Upvotes: 73

Views: 111802

Answers (7)

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

Upvotes: 0

Sebastiaan35
Sebastiaan35

Reputation: 31

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.

Upvotes: 3

Andy
Andy

Reputation: 86

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.

Upvotes: 5

phil_20686
phil_20686

Reputation: 4080

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.

Upvotes: 2

Weihui Guo
Weihui Guo

Reputation: 3997

runserver.bat:

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

Upvotes: 53

Ashish Gupta
Ashish Gupta

Reputation: 2614

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

Upvotes: 113

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83758

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.

Upvotes: 13

Related Questions