1

My virtualenv works perfect from bash with:

$ workon mylovelyenv
// do something
$ deactivate

When I try to automate with makefile I can't get it right:

$ cat makefile
all:
    workon mylovelyenv           #doesn't work
    $(shell workon mylovelyenv)  #doesn't work either
    bash -c "workon mylovelyenv" #doesn't work either
    python go.py
    $(shell deactivate) # same here ...
Francisco Puga
  • 23,869
  • 5
  • 48
  • 64
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87

1 Answers1

1

make out of the box runs each line in a recipe in a separate shell. That means that things like

all:
    cd there
    pwd

don't do what you might naïvely expect -- the cd happens in one shell instance, which then exits, then pwd runs in a new shell which starts back in the current working directory of the make process.

Similarly, workon and friends have to run in the same shell instance as subsequent commands which depend on it. In your case, probably

all:
    workon mylovelyenv; \
    python go.py

or (in GNU Make 3.82 or newer)

.ONESHELL:
all:
    workon mylovelyenv
    python go.py

There is no need to explicitly deactivate because that happens anyway when the subshell exits.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • doesn't work for me - I get `/bin/sh: 3: workon: not found`, tried both ways, I have make `4.1` ... any suggestions? – OrenIshShalom Oct 28 '20 at 11:16
  • 1
    Sounds like you are running `make` in a place where you don't have all the definitions which you have in your interactive shell. `workon` is really intended for interactive use anyway. If `virtualenvwrapper` (which I guess provides `workon`) works the same as the regular Python `virtualenv` you can use `mylovelyenv/bin/python` to run the virtualenv's Python as if you had activated the environment. – tripleee Oct 28 '20 at 11:19
  • Perhaps see also https://stackoverflow.com/questions/29900090/virtualenv-workon-doesnt-work – tripleee Oct 28 '20 at 11:20
  • Arrrrgggh ... I'll just wrap the makefile in a bash script ... – OrenIshShalom Oct 28 '20 at 11:46