2

I have the following Makefile

env:
    source venv/bin/activate

When I do make env to activate a python virtual env, I'm getting the following error

source venv/bin/activate make: source: No such file or directory make: *** [env] Error 1

But when I copy source venv/bin/activate and run in the shell, it runs fine. Do I need to set the current dir inside the Makefile?

3

1 Answer 1

5

source is a shell built-in command, not an executable that you can start from anywhere but a shell. What source does is to read and execute the contents of a file in the current shell, without starting a new shell.

The purpose of that is to modify the state of the current shell (if you just sh venv/bin/activate, your shell would also execute the contents of the activate script, but then be done, and quit).

But what you want to do is modify the state of your make program. Running the activate script in a shell that you spawn, in whatever way, from make, is not going to change anything about the environment that make sees.
That's because every program (A) launched by another program (B) gets its own copy of the environment of the launching program (B), which it (A) can change to its heart's desire, without affecting the environment of the launching program (B).

So, what you want to do cannot work, even theoretically.

If you need to run some Makefile inside a venv, you will have to source the activate script first, and from the thus modified shell then start make; not the other way around.

2
  • Your facts are correct, but I question your interpretation of the OP’s intent.  I guess that they want to be able to use make env as an alias for source venv/bin/activate in their interactive shell.  Of course, I could be wrong. Commented Dec 18, 2022 at 18:52
  • 2
    @Scott-СлаваУкраїні but then my answer would still stand 100 % – you cannot use make to change the state of the shell that called make. Commented Dec 18, 2022 at 19:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .