Add venv activate command to the venv modules

Greetings list, put simply, be able to use $ python -m venv venv_name activate To activate an env instead of having each platform have a way of handling it Kind Regards, Abdur-Rahmaan Janhangeer about <https://compileralchemy.github.io/> | blog <https://www.pythonkitchen.com> github <https://github.com/Abdur-RahmaanJ> Mauritius

On Tue, Jan 5, 2021 at 1:42 AM Abdur-Rahmaan Janhangeer <arj.python@gmail.com> wrote:
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

On Mon, Jan 4, 2021 at 9:47 AM Chris Angelico <rosuav@gmail.com> wrote:
This is true, but... it might be possible to include something more like pipenv's "shell" which spawns a new instance of your shell with the right paths. It effectively achieves the same goal, without needing to source,
-- CALVIN SPEALMAN SENIOR QUALITY ENGINEER cspealma@redhat.com M: +1.336.210.5107 [image: https://red.ht/sig] <https://red.ht/sig> TRIED. TESTED. TRUSTED. <https://redhat.com/trusted>

On Tue, Jan 5, 2021 at 1:52 AM Calvin Spealman <cspealma@redhat.com> wrote:
Hmm. In terms of guaranteeing consistency, I'm not sure it's really any better - what if it picks the wrong shell, or (worse) the right shell with slightly different settings? The normal activation method is pretty simple: set three environment variables (including PATH), and change the prompt. Spawning a separate shell would require correctly matching the behaviour of the current shell. Plus, I'm not really a fan of spawning unnecessary shell levels, which you then end up exiting out of as a stack. But if a separate "python3 -m venv env shell" command were to exist, I'm sure it would have some use. It shouldn't be considered activation, but a completely separate feature with some similar use-cases. (Though I wouldn't use it, myself.) ChrisA

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 about <https://compileralchemy.github.io/> | blog <https://www.pythonkitchen.com> github <https://github.com/Abdur-RahmaanJ> Mauritius

Errata: subprocess(['os', 'stuff', ...]) Kind Regards, Abdur-Rahmaan Janhangeer about <https://compileralchemy.github.io/> | blog <https://www.pythonkitchen.com> github <https://github.com/Abdur-RahmaanJ> Mauritius

You just execute the appropriate shell commands via Python Kind Regards, Abdur-Rahmaan Janhangeer about <https://compileralchemy.github.io/> | blog <https://www.pythonkitchen.com> github <https://github.com/Abdur-RahmaanJ> Mauritius On Mon, Jan 4, 2021 at 7:34 PM Chris Angelico <rosuav@gmail.com> wrote:

On 04.01.2021 15:45, Chris Angelico wrote:
Something that would work is using the ssh-agent approach to output shell commands which configure the environment: # For bash et al: `python3 -c "print('export TEST=1')"` A new command: `python3 -m venv activate myenv bash` could do the trick. Of course, venv itself could also create the necessary shell files in the bin/ dir. You'd then just need to run: source myenv/bin/activate.sh (this is how virtualenv does this) -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Experts (#1, Jan 04 2021)
Python Projects, Coaching and Support ... https://www.egenix.com/ Python Product Development ... https://consulting.egenix.com/
::: We implement business ideas - efficiently in both time and costs ::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 https://www.egenix.com/company/contact/ https://www.malemburg.com/

I know nothing of the details, but I think the goal is to have the same command work (almost) everywhere, yes? That does not need to be a python script, however. In fact, it's nice if activating an environment is as fast as possible so firing up Python to do it is less than ideal. Anyway -- I'd encourage folks to look at conda -- it now provides a conda command that can activate on multiple plaatfroms the same way: conda activate env_name I don't know how many different shells that works with, but it at least hits the big few. "conda" used to be a python script but I'm pretty sure it's now some kind of custom executable that re-directs the commands, and I'm sure is different on different platforms. prior art and all that. -CHB On Mon, Jan 4, 2021 at 8:34 AM M.-A. Lemburg <mal@egenix.com> wrote:
-- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython

@Christopher Barker <pythonchb@gmail.com> You nailed it! I've used the conda activate command many times. It should in theory be possible in Python @Chris Angelico <rosuav@gmail.com> Yes mere shell commands passing via Py does not work, maybe a file way with results in current shell as @M.-A. Lemburg <mal@egenix.com> points out, venv already generates the files. On windows it has an activate.bat Kind Regards, Abdur-Rahmaan Janhangeer about <https://compileralchemy.github.io/> | blog <https://www.pythonkitchen.com> github <https://github.com/Abdur-RahmaanJ> Mauritius On Mon, Jan 4, 2021 at 9:03 PM Christopher Barker <pythonchb@gmail.com> wrote:

To get around the fact that activating a virtualenv requires setting environment variables in the current shell poetry has a shell command that simply spawns a new shell with the appropriate environment variables: https://python-poetry.org/docs/cli/#shell So a cross-platform `activate` command wouldn't be possible but something like this could: $ python -m venv venv_name shell (venv_name) $ # the virtualenv is now active (venv_name) $ exit $ # back into the parent shell

On Tue, Jan 5, 2021 at 1:42 AM Abdur-Rahmaan Janhangeer <arj.python@gmail.com> wrote:
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

On Mon, Jan 4, 2021 at 9:47 AM Chris Angelico <rosuav@gmail.com> wrote:
This is true, but... it might be possible to include something more like pipenv's "shell" which spawns a new instance of your shell with the right paths. It effectively achieves the same goal, without needing to source,
-- CALVIN SPEALMAN SENIOR QUALITY ENGINEER cspealma@redhat.com M: +1.336.210.5107 [image: https://red.ht/sig] <https://red.ht/sig> TRIED. TESTED. TRUSTED. <https://redhat.com/trusted>

On Tue, Jan 5, 2021 at 1:52 AM Calvin Spealman <cspealma@redhat.com> wrote:
Hmm. In terms of guaranteeing consistency, I'm not sure it's really any better - what if it picks the wrong shell, or (worse) the right shell with slightly different settings? The normal activation method is pretty simple: set three environment variables (including PATH), and change the prompt. Spawning a separate shell would require correctly matching the behaviour of the current shell. Plus, I'm not really a fan of spawning unnecessary shell levels, which you then end up exiting out of as a stack. But if a separate "python3 -m venv env shell" command were to exist, I'm sure it would have some use. It shouldn't be considered activation, but a completely separate feature with some similar use-cases. (Though I wouldn't use it, myself.) ChrisA

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 about <https://compileralchemy.github.io/> | blog <https://www.pythonkitchen.com> github <https://github.com/Abdur-RahmaanJ> Mauritius

Errata: subprocess(['os', 'stuff', ...]) Kind Regards, Abdur-Rahmaan Janhangeer about <https://compileralchemy.github.io/> | blog <https://www.pythonkitchen.com> github <https://github.com/Abdur-RahmaanJ> Mauritius

You just execute the appropriate shell commands via Python Kind Regards, Abdur-Rahmaan Janhangeer about <https://compileralchemy.github.io/> | blog <https://www.pythonkitchen.com> github <https://github.com/Abdur-RahmaanJ> Mauritius On Mon, Jan 4, 2021 at 7:34 PM Chris Angelico <rosuav@gmail.com> wrote:

On 04.01.2021 15:45, Chris Angelico wrote:
Something that would work is using the ssh-agent approach to output shell commands which configure the environment: # For bash et al: `python3 -c "print('export TEST=1')"` A new command: `python3 -m venv activate myenv bash` could do the trick. Of course, venv itself could also create the necessary shell files in the bin/ dir. You'd then just need to run: source myenv/bin/activate.sh (this is how virtualenv does this) -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Experts (#1, Jan 04 2021)
Python Projects, Coaching and Support ... https://www.egenix.com/ Python Product Development ... https://consulting.egenix.com/
::: We implement business ideas - efficiently in both time and costs ::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 https://www.egenix.com/company/contact/ https://www.malemburg.com/

I know nothing of the details, but I think the goal is to have the same command work (almost) everywhere, yes? That does not need to be a python script, however. In fact, it's nice if activating an environment is as fast as possible so firing up Python to do it is less than ideal. Anyway -- I'd encourage folks to look at conda -- it now provides a conda command that can activate on multiple plaatfroms the same way: conda activate env_name I don't know how many different shells that works with, but it at least hits the big few. "conda" used to be a python script but I'm pretty sure it's now some kind of custom executable that re-directs the commands, and I'm sure is different on different platforms. prior art and all that. -CHB On Mon, Jan 4, 2021 at 8:34 AM M.-A. Lemburg <mal@egenix.com> wrote:
-- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython

@Christopher Barker <pythonchb@gmail.com> You nailed it! I've used the conda activate command many times. It should in theory be possible in Python @Chris Angelico <rosuav@gmail.com> Yes mere shell commands passing via Py does not work, maybe a file way with results in current shell as @M.-A. Lemburg <mal@egenix.com> points out, venv already generates the files. On windows it has an activate.bat Kind Regards, Abdur-Rahmaan Janhangeer about <https://compileralchemy.github.io/> | blog <https://www.pythonkitchen.com> github <https://github.com/Abdur-RahmaanJ> Mauritius On Mon, Jan 4, 2021 at 9:03 PM Christopher Barker <pythonchb@gmail.com> wrote:

To get around the fact that activating a virtualenv requires setting environment variables in the current shell poetry has a shell command that simply spawns a new shell with the appropriate environment variables: https://python-poetry.org/docs/cli/#shell So a cross-platform `activate` command wouldn't be possible but something like this could: $ python -m venv venv_name shell (venv_name) $ # the virtualenv is now active (venv_name) $ exit $ # back into the parent shell
participants (7)
-
Abdur-Rahmaan Janhangeer
-
Calvin Spealman
-
Chris Angelico
-
Christopher Barker
-
M.-A. Lemburg
-
Stestagg
-
Valentin Berlier