virtualenv icon indicating copy to clipboard operation
virtualenv copied to clipboard

run command immediately after activating environment

Open 62mkv opened this issue 6 years ago • 11 comments

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) ?

62mkv avatar Dec 16 '18 17:12 62mkv

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.

gaborbernat avatar Dec 16 '18 19:12 gaborbernat

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.

merwok avatar Dec 17 '18 04:12 merwok

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

62mkv avatar Dec 17 '18 04:12 62mkv

You could write a script that calls activate then manage.py!

merwok avatar Dec 17 '18 14:12 merwok

How to do that on Windows?

62mkv avatar Dec 17 '18 15:12 62mkv

I don’t know! Look for a PowerShell tutorial?

merwok avatar Dec 17 '18 15:12 merwok

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

62mkv avatar Dec 19 '18 17:12 62mkv

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.

bskinn avatar Jan 16 '19 01:01 bskinn

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.

stale[bot] avatar Apr 16 '19 03:04 stale[bot]

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

rmoser avatar Jan 14 '21 13:01 rmoser

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.

merwok avatar Jan 14 '21 16:01 merwok

This is a won't do for now. One can write a plugin for it.

gaborbernat avatar Jun 27 '23 03:06 gaborbernat