You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using the shebang to specify the specific version of the intepreter to be used (3.8, 3.9, ...) knowing that the user's laptop that will run the pyz have the different versions installed as "%USERPROFILE%\AppData\Local\Programs\Python\PythonNN".
Hence I would need the shebang line to accept the environment variable USERPROFILE.
The current shebang line does not allow this and shiv will "expand_user" the python interpreter (for '~').
The workaround I found (on windows) is to use a shebang line looking like cmd.exe /C call "%USERPROFILE%\AppData\Local\Programs\Python\Python38\python.exe"
However, specifying such an argument via the CLI is cumbersome:
one need to remember this specific shebang construct
the %USERPROFILE% will be interpreted directly by the shell and so will come to python already replaced.
What would be nice is to be able to specify a python interpreter like "$USERPROFILE$\AppData\Local\Programs\Python\Python38\python.exe" and have shiv detect the "$" and replace the intepreter by the ad-hoc shebang line with the $ replaced by %.
In the general case, when the interpreter path includes the %USERPROFILE% string, it could even be nice to automatically replace it by the shebang with the dynamic $USERPROFILE$ (this could be disabled through an option).
The two logics are implemented and illustrated here below:
importosfrompathlibimportPathdefreplace_userprofile(python):
r"""convert a path string with "C:\Users\name-of-user\AppData\Local\Programs\Python\Python38\python.exe" to the path string $USERPROFILE$\AppData\Local\Programs\Python\Python38\python.exe """if (user_profile:=os.environ.get("USERPROFILE")) :
try:
returnstr("$USERPROFILE$"/Path(python).relative_to(Path(user_profile)))
exceptValueError:
returnpythondefbuild_shebang(python):
# handle $ENVIRONMENT_VARIABLES$ in python path (use an trampoline shebang)if"$"inpython:
# if the python path contains a $, we replace it by % and use a cmd.exe shebang to trampoline to pythonreturnf'cmd.exe /C call "{python.replace("$", "%")}"'else:
returnpythonif__name__=="__main__":
forpythonin [
r"C:\Users\GFJ138\AppData\Local\Programs\Python\Python38\python.exe",
r"$USERPROFILE$\AppData\Local\Programs\Python\Python38\python.exe",
r"C:\Program Files\Python\Python38\python.exe",
]:
print(
f"Original interpreter: {python}\n"f"Dynamic interpreter: {replace_userprofile(python)}\n"f"Shebang: {build_shebang(replace_userprofile(python))}\n"
)
which outputs (given USERPROFILE=C:\Users\JohnDoe):
I am using the shebang to specify the specific version of the intepreter to be used (3.8, 3.9, ...) knowing that the user's laptop that will run the pyz have the different versions installed as "%USERPROFILE%\AppData\Local\Programs\Python\PythonNN".
Hence I would need the shebang line to accept the environment variable USERPROFILE.
The current shebang line does not allow this and shiv will "expand_user" the python interpreter (for '~').
The workaround I found (on windows) is to use a shebang line looking like
cmd.exe /C call "%USERPROFILE%\AppData\Local\Programs\Python\Python38\python.exe"
However, specifying such an argument via the CLI is cumbersome:
What would be nice is to be able to specify a python interpreter like "$USERPROFILE$\AppData\Local\Programs\Python\Python38\python.exe" and have shiv detect the "$" and replace the intepreter by the ad-hoc shebang line with the $ replaced by %.
In the general case, when the interpreter path includes the %USERPROFILE% string, it could even be nice to automatically replace it by the shebang with the dynamic$USERPROFILE$ (this could be disabled through an option).
The two logics are implemented and illustrated here below:
which outputs (given USERPROFILE=C:\Users\JohnDoe):
The text was updated successfully, but these errors were encountered: