A Python script that activates the virtualenv and then runs another Python script?
Question:
On Windows Vista, I need a script that starts the activate
(to activate the virtualenv) script in:
C:UsersAdminDesktopvenvScripts
And later, in the virtual environment, starts to the manage.py runserver
in the folder:
C:UsersAdminDesktophelloworld
How should I do? What modules should I use?
Answers:
If you want call virtualenv’ed Python directly you can do something like this:
C:UsersAdminDesktopvenvScriptsbinpython.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.
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:UsersAdminDesktopvenvScripts & activate & cd /d C:UsersAdminDesktophelloworld & python manage.py runserver"
Then you can just run this bat file (just double click) to start the server
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.
runserver.bat:
CALL [your path]Scriptsactivate.bat
python manage.py runserver
I am using Anaconda 3 and python 3.7.6 on Windows. Had to do this in my .bat file:
CALL pathtobasevirtualenvironmentScriptsactivate.bat pathtoyourvirtualenvironment
[pathtoyourvirtualenvironment]python.exe pathtoyourscriptyoursript.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.
For me the above didn’t work and therefore I will provide a more general answer.
But first specifically, this worked for me:
- Open a notepad
- paste this:
@echo off
CALL c:1envScriptsactivate.bat
python c:1app.py runserver
- save as whatever.bat
- 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 envScripts 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: envScripts
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:UsersAdminDesktopvenvScriptsactivate.bat
python C:UsersAdminDesktophelloworldmanage.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 cd1 under @echo off does that trick.
For me, working with this code: (script_file.bat)
@echo off
CALL C:Usersapo1979Anaconda3Scriptsactivate.bat PyPWBI
C:/Users/apo1979/Anaconda3/envs/PyPWBI/python.exe "d:/.APO_OneDrive/script_SpeedTest.py" runserver
pause
On Windows Vista, I need a script that starts the activate
(to activate the virtualenv) script in:
C:UsersAdminDesktopvenvScripts
And later, in the virtual environment, starts to the manage.py runserver
in the folder:
C:UsersAdminDesktophelloworld
How should I do? What modules should I use?
If you want call virtualenv’ed Python directly you can do something like this:
C:UsersAdminDesktopvenvScriptsbinpython.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.
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:UsersAdminDesktopvenvScripts & activate & cd /d C:UsersAdminDesktophelloworld & python manage.py runserver"
Then you can just run this bat file (just double click) to start the server
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.
runserver.bat:
CALL [your path]Scriptsactivate.bat
python manage.py runserver
I am using Anaconda 3 and python 3.7.6 on Windows. Had to do this in my .bat file:
CALL pathtobasevirtualenvironmentScriptsactivate.bat pathtoyourvirtualenvironment
[pathtoyourvirtualenvironment]python.exe pathtoyourscriptyoursript.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.
For me the above didn’t work and therefore I will provide a more general answer.
But first specifically, this worked for me:
- Open a notepad
- paste this:
@echo off
CALL c:1envScriptsactivate.bat
python c:1app.py runserver
- save as whatever.bat
- 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 envScripts 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: envScripts
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:UsersAdminDesktopvenvScriptsactivate.bat
python C:UsersAdminDesktophelloworldmanage.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 cd1 under @echo off does that trick.
For me, working with this code: (script_file.bat)
@echo off
CALL C:Usersapo1979Anaconda3Scriptsactivate.bat PyPWBI
C:/Users/apo1979/Anaconda3/envs/PyPWBI/python.exe "d:/.APO_OneDrive/script_SpeedTest.py" runserver
pause