0

I'm trying to create a utility CLI that creates an empty python project with just a .venv inside.

One of the features I wanted is that, when the CLI ends the project creation, it leaves the bash inside the project folder and with the virtualenv activated. This way:

(base) ubuntu@pc:~/dev/cli$ python cli.py start ./folder

        ... do it's magic ...

        ... and ends up like this:
(.venv) ubuntu@pc:~/dev/cli/folder$ 

Note that now the cwd is inside folder and **(.venv) ** is activated.

I was able to achieve part of my objective (change the folder) by using:

import os

os.chdir("./folder")
os.execl("/bin/bash", "/bin/bash")

But didn't find a way to keep the venv activated after the program is halted.

Any ideas??

1 Answers1

0

bash has the -rcfile option that allows to use a non standard initialization file.

import os

os.chdir("./folder")
os.execl("/bin/bash", "/bin/bash", "--rcfile", ".venv/bin/activate")

That should be enough for the new .venv to be active in the bash shell.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Thank you that worked! A detail is that with this implementation, the bash returns very raw, without the usual ubuntu terminal syntax highlighting. Any clues about it? – Vittor Faria Dec 16 '21 at 16:52
  • @VittorFaria: by default `bash` reads the `.bashrc` file. Here it reads `bin/activate` **instead** of its default initialization file. – Serge Ballesta Dec 16 '21 at 16:54