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
Use USERBASE based package installation into local directory #249
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This did work on my end and installed the module into CAD_Sketcher/lib/python3.10/site-packages.
For some reason it cannot find the module in the same session, i had to restart blender.
Also this added a bin folder which should also be ignored by git.
def install_pip(): | ||
import ensurepip | ||
|
||
ret_val = ensurepip._bootstrap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why the protected function is called and not just bootstrap itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh i actually had that commented in a previous version but it must've gotten lost somewhere. The bootstrap function itself doesn't do anything but call _bootstrap - but it hides the exit code of the process. will add comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the docs, they do warn about environment changes when using this function.
Note The bootstrapping process has side effects on both
sys.path
andos.environ
. Invoking the command line interface in a subprocess instead allows these side effects to be avoided.-- https://docs.python.org/3/library/ensurepip.html#ensurepip.bootstrap
This was initially achieved when using the cmd
tool as before
cmd = [global_data.PYPATH, "-m", "ensurepip", "--upgrade"]
return not subprocess.call(cmd)
My question being: Do we absolutely need to keep it in this process in order to maintain the environment variables such as PYTHONUSERBASE
? The warning message could hint at future environment-related bugs.
Alternatively, I would suggest using subprocess.call
as done before or subprocess.run()
where you can also pass environment variables using env=env_vars
in case the environment inheritance is somehow compromised.
If env is not
None
, it must be a mapping that defines the environment variables for the new process; these are used instead of the default behavior of inheriting the current process’ environment. It is passed directly toPopen
.
I'll have to add those, the folder names depend on what Python does for different OSs so we have to gitignore all versions - i forgot it only uses the Python3x folder scheme on windows. Maybe it's better to add another folder to USERBASE so that we can gitignore that without caring about OS specifics. As for the finding the module: I'm not sure why that's failing - I'll have to boot into my linux install to test. wierd. |
Well path and user_base seems to be right:
|
Calling importlib.invalidate_caches seems to do the trick on my end. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes do not seem applicable to my platform (linux). Restarting Blender doesn't fix the issue either
if subprocess.call(cmd, env=global_data.env_vars) == 0: | ||
return True | ||
return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if subprocess.call(cmd, env=global_data.env_vars) == 0: | |
return True | |
return False | |
return subprocess.call(cmd, env=global_data.env_vars) == 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also this doesn't seem to function. In the terminal stdout/err I get No module named pip
but the return value is 0
. Which is very strange because it should return 1
in that case. In turn, it doesn't want to install pip.
# Running with the debugger, and tried this right before executing line 40.
>>> subprocess.run(cmd, env=global_data.env_vars, capture_output=True)
CompletedProcess(args=['/usr/bin/python3.10', '-m', 'pip', '--version'], returncode=0, stdout=b'', stderr=b'No module named pip\n')
I do run on Linux. Given the difference for our platforms, I will try to investigate myself
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a fix, I would recommend to always use ensurepip
as it will, at most, update pip to the latest version in case pip is already present.
python -m ensurepip
This invocation will install
pip
if it is not already installed, but otherwise does nothing.
def install_pip(): | ||
import ensurepip | ||
|
||
ret_val = ensurepip._bootstrap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the docs, they do warn about environment changes when using this function.
Note The bootstrapping process has side effects on both
sys.path
andos.environ
. Invoking the command line interface in a subprocess instead allows these side effects to be avoided.-- https://docs.python.org/3/library/ensurepip.html#ensurepip.bootstrap
This was initially achieved when using the cmd
tool as before
cmd = [global_data.PYPATH, "-m", "ensurepip", "--upgrade"]
return not subprocess.call(cmd)
My question being: Do we absolutely need to keep it in this process in order to maintain the environment variables such as PYTHONUSERBASE
? The warning message could hint at future environment-related bugs.
Alternatively, I would suggest using subprocess.call
as done before or subprocess.run()
where you can also pass environment variables using env=env_vars
in case the environment inheritance is somehow compromised.
If env is not
None
, it must be a mapping that defines the environment variables for the new process; these are used instead of the default behavior of inheriting the current process’ environment. It is passed directly toPopen
.
if no_deps: | ||
cmd += ["--no-deps"] | ||
|
||
cmd = cmd + package.split(" ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't strictly your change, but the .split(" ")
will break file paths containing spaces. If we are parsing some input value, it should be done at the source View3D_OT_slvs_install_package.execute()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and you can expect file paths if we are installing .whl
wheels from a different folder.
# code which is needed for the currect error messages. | ||
ret_val = ensurepip._bootstrap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo + link to ref
# code which is needed for the currect error messages. | |
ret_val = ensurepip._bootstrap() | |
# code which is needed for the correct error messages. | |
# https://github.com/python/cpython/blob/f4c03484da59049eb62a9bf7777b963e2267d187/Lib/ensurepip/__init__.py#L115-127 | |
ret_val = ensurepip._bootstrap() |
The Linux issues here are fairly interesting, and indeed need to be solved ; thankfully I'm on Linux too, but I'll need someone to take a look at my fixed version later on other platforms, to make sure I didn't break anything. :-) |
The linux issue might be py_slv 1.0.5 wheel package's problem. realthunder/slvs_py#7 |
This PR sets and uses USERBASE for pip installations. This has the advantage of never interacting with the systems python installation and should prevent bugs which might happend due to wrong versions of pip (shimmed by conda)
This also localises the dependencies within the addons folder to make sure permission errors don't happen.
This is after i deletd both py-slvs and pip from any python packages directories blender can see, and clicking the "install from pip" button once:
closes: #243
also i formatted the files i touched with black, which is in line with #248 so i hope its fine even before that is closed.
TODO: