virtualenv
virtualenv copied to clipboard
run command immediately after activating environment
I use virtualenv to run 3rd party Django based web-server on Windows.
In order to do that I have to run "start ...activate.bat" and then manually type the command python manage.py runserver
This is tedious and not possible to automate
So I had to hack into activate.bat and add this command in the very end
Shouldn't it be more convenient to have some kind of environment variable named, for example VIRTENV_RUN_ON_ACTIVATE, which content would be executed as a command, if defined, at the end of the activate script (ps1 or cmd) ?
In principle I agree with the request, we'll have to think good about implementation though. An environment variable approach feels at first extensible and doable.
activate is a helper, not a required part of the virtualenv functionality. If you want to save one command, you can always skip activate and use the full program path:
path/to/venv/bin/python manage.py runserver
But I have to say that I don’t understand the original request. I run runserver too, but also makemigrations, migrate, check and other manage.py commands + other programs that are found in path/to/venv/bin, so I don’t see that special-casing one command would really help.
I believe it also has to do with environment variables, although I will check, if it works the way you say.
Also I don't develop nor update this application, just use this command only. Unfortunately I couldn't find a way to write any wrapper yet that I could just put into desktop shortcut and be done with it; thus this request
You could write a script that calls activate then manage.py!
How to do that on Windows?
I don’t know! Look for a PowerShell tutorial?
ok, it seems I can live with .bat-file like this:
cd C:\Code\cubesviewer-server\cvapp\
%USERPROFILE%\Envs\cubes-server\Scripts\python manage.py runserver
thanks to @merwok
I have a script on Windows that does exactly this, for activating a default environment in a given directory and then launching Jupyter Notebook. The core of it is:
call env\Scripts\activate
start jupyter-notebook
start doesn't work for activate.bat b/c it launches it in a new process & so the environment changes don't get applied to the following commands in the script.
If you want the original shell to be left outside the virtualenv, I assume you can just call deactivate at the end.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Just add a comment if you want to keep it open. Thank you for your contributions.
I know this is an old discussion, but if the OP is still watching, does this not work for you? I have jobs like this running with Task Scheduler...
cmd.exe /c venv\activate.bat && python manage.py runserver
My previous questions / disagreement still stand, but I wanted to note virtualenvwrapper offers hooks: https://virtualenvwrapper.readthedocs.io/en/latest/scripts.html#scripts
I would not have runserver in them, but they are useful to define environment variables or do other kind of setup.
This is a won't do for now. One can write a plugin for it.