0

There are many answers on SO about how to use a virtual environment inside a makefile. This is not what I am asking.

I have a makefile that creates a virtual environment for the user and does a series of installs. At the end of the script's running, I would like to leave the caller inside the virtual environment. Why? Because it is easy to forget to activate when in a rush and install subsequent libraries into the os-python.

What have I tried? I've looked at as many of the SO answers I could find on the subject, such as this, and this. All seem to deal with using the venv inside Make, but none tell me (as far as I can tell) how to leave a user inside the virtual environment.

Specifically, I want the equivalent of this;

make install  #creates a virtual environment
source venv/bin/activate

(venv) prompt:>
SteveJ
  • 3,034
  • 2
  • 27
  • 47
  • 1
    What you want to do is impossible, due to operating system restrictions. Just the same way you can't write a python script or a perl script or any other kind of program that will leave you in a venv after they exit, you can't have make leave you in a venv after it exits. – MadScientist Apr 26 '21 at 21:46
  • 1
    See this answer for example: https://stackoverflow.com/a/62802316/939557 – MadScientist Apr 26 '21 at 21:51
  • @MadScientist; (Thank you) What about the prospect of leaving a statement on the callers command prompt so that press "enter" is all that is required. Is that, too, not possible? – SteveJ Apr 26 '21 at 21:58
  • @MadScientist; Additionally, from your answer; "You can also create a shell function or shell alias to do it, because those also run in the same shell process, not in a child process." Does that mean I could hack my way around it by creating a function that calls make then activates the environment? – SteveJ Apr 26 '21 at 22:03
  • I agree that you cannot do this, you can't jump between shell processes like that. But what about creating a function or logic in your `.bashrc` that activates the venv when a new shell opens (conditionally upon whatever contrainst you need)? – John Kealy Apr 26 '21 at 22:44
  • Simply add these two lines into `install_me` and tell people to call it with `source install_me`. Alternatively, tell people to always run: `make install && source venv/bin/activate` – Oo.oO Apr 27 '21 at 06:50
  • No it is not possible to "leave a statement on the caller's command prompt". That would involve the child process still modifying some aspect of the parent process (that controls the prompt). That is only possible with the support of the parent process; that is the shell would have to provide some facility that would allow a program that it invokes to "give back" a string that would be installed as a default next command to run. Nothing like that exists. – MadScientist Apr 27 '21 at 13:06
  • Yes, you can certainly add a function to your shell that will invoke make then run venv. A shell function is just a shortcut for running commands by hand so any commands you can run by hand, can be put into a shell function. But, everyone will need to create that function themselves in their own shell configuration (or run a script or something that will do it, then restart their shells and/or load the function by hand). – MadScientist Apr 27 '21 at 13:08
  • Ok, thank you everyone for your help. Not sure if I should delete this question or leave it for others in the future. – SteveJ Apr 27 '21 at 14:35

0 Answers0