Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switching environment in the CLI should also change the value of environment variables in the current terminal #11

Open
smarie opened this issue Oct 6, 2017 · 7 comments

Comments

@smarie
Copy link
Owner

smarie commented Oct 6, 2017

Currently if you use the CLI as in envswitch <my_env>, you need to restart your command terminal to see the change.

Hints on how to do this:

@jayvdb
Copy link

jayvdb commented Jun 4, 2019

RefreshEnv.cmd works quite well, but unlikely to help you as you want to cause the parent process to be reset.

https://github.com/tischda/refresh does that, using the broadcast mentioned in the SO you linked to above.
https://github.com/SecsAndCyber/PathFix/blob/master/src/pathfix.py#L11 does it in Python, and there are a few other examples in Python that can be found on GitHub , e.g. https://github.com/WiggleWizard/unrealtoolkit/blob/90dbb11e78314e854ffda55ad18ecfa8bb372436/unrealtoolkit/env.py#L5, but I am not sure exactly what works for those flags - I see a lot of variation.

See also saltstack/salt#23504 (comment) and rest of the thread.

@jayvdb
Copy link

jayvdb commented Jun 5, 2019

I've played around with this quite a bit over the last day for my own script, and I am pretty confident that RefreshEnv.cmd is the only way to fix this.

Maximus5/ConEmu#468 also covers some of the reasons why a terminal shouldnt automatically refresh its env vars, and it has a patch very recently uploaded ... ;-)

@smarie
Copy link
Owner Author

smarie commented Jun 13, 2019

Thanks @jayvdb !

I am not sure of the final conclusion though: would there be a chance for a python process (the commandline setuptools entrypoint included in envswitch) to perform what RefreshEnv.cmd does ? If so even if it would only work on windows I would gladly accept a PR or an example code snippet.

@jayvdb
Copy link

jayvdb commented Jun 14, 2019

RefreshEnv.cmd generates a temporary .cmd file and call's it. We can use python to create the generated .cmd file, but there is no way that python can call it -- that has to be done in the running cmd.exe process.

The best you could do is tell users

Please run call %TEMP%\foo.cmd to refresh your current console

But then why not include RefreshEnv.cmd and ask them to run it.

@smarie
Copy link
Owner Author

smarie commented Jun 14, 2019

You are right: the best is probably to include it as a resource and ask them to run it. Thanks !

@jayvdb
Copy link

jayvdb commented Jun 14, 2019

would there be a chance for a python process (the commandline setuptools entrypoint included in envswitch) to perform what RefreshEnv.cmd does ?

Ahhh, yes ... if you can get control of the contents of envswitch.cmd , it could invoke refreshenv. I havent tried to override the setuptools .cmd helper. My guess is that it wont be easy to do during setup.py, and would be very tricky to do in a wheel (but see amazon-ion/ion-python#76 (comment) and my comment below it; and also https://github.com/ninjaaron/fast-entry_points), but you could do it at startup which would then be effective for all subsequent usage.

Then you have the problem of being cross platform ... :) See chocolatey/choco#1851

@smarie
Copy link
Owner Author

smarie commented Jun 14, 2019

Modifying the setuptools-generated entrypoint is a good idea ! We'll see how this goes when I have a window of time to have a look (or if someone proposes a PR).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants