Unfortunately, that wouldn't work. Activating a virtual environment
means setting some env vars in the current shell, and Python is
fundamentally unable to do that - it can only be done within the shell
itself (by sourcing a script).

You can, of course, simply run the Python executable from that venv,
but activation is *by its nature* a shell feature, and will differ by
shell.

ChrisA

It's somewhat easy


def activate_on_linux():
    sys.subprocess([sys.executable, ...])

def activate_on_win():
    sys.subprocess([sys.executable, ...])

def activate_on_mac():
    sys.subprocess([sys.executable, ...])

def activate_on_solaris():
    sys.subprocess([sys.executable, ...])

if sys.platform == linux:
    activate_on_linux()

etc

I believe this is used somewhere else in CPython.
Recently sys.executable replaced python

Kind Regards,

Abdur-Rahmaan Janhangeer
github
Mauritius