indygreg / PyOxidizer

A modern Python application packaging and distribution tool
Mozilla Public License 2.0
5.48k stars 239 forks source link

Feature: add an option for sys.flags.no_user_site=0 #430

Open mharbison72 opened 3 years ago

mharbison72 commented 3 years ago

It looks like this is what disables looking in the user site package. The only reference to this variable is in a test checking that it is 1, so it's evidently on purpose. However, it would be useful to make the user site available for loading Mercurial extensions. The py2 builds already look there, so not having this is a bit of a regression.

indygreg commented 3 years ago

This is already implemented via the PythonInterpreterConfig.user_site_directory attribute exposed to Starlark.

https://pyoxidizer.readthedocs.io/en/stable/pyoxidizer_config_type_python_interpreter_config.html#starlark_pyoxidizer.PythonInterpreterConfig.user_site_directory

What am I missing here?

mharbison72 commented 3 years ago

I missed that somehow, but it doesn't seem to work.

diff --git a/rust/hgcli/pyoxidizer.bzl b/rust/hgcli/pyoxidizer.bzl
--- a/rust/hgcli/pyoxidizer.bzl
+++ b/rust/hgcli/pyoxidizer.bzl
@@ -44,19 +44,6 @@ if extra_path is not None:
     # We do not prepend the values because the Mercurial library wants to be in
     # the front of the sys.path to avoid picking up other installations.
     sys.path.extend(extra_path.split(os.pathsep))
-# Add user site to sys.path to load extensions without the full path
-if os.name == 'nt':
-    vi = sys.version_info
-    appdata = os.environ.get('APPDATA')
-    if appdata:
-        sys.path.append(
-            os.path.join(
-                appdata,
-                'Python',
-                'Python%d%d' % (vi[0], vi[1]),
-                'site-packages',
-            )
-        )
 import hgdemandimport;
 hgdemandimport.enable();
 from mercurial import dispatch;
@@ -111,6 +98,9 @@ def make_exe(dist):
     # We want to let the user load extensions from the file system
     config.filesystem_importer = True

+    # Load `pip install --user` extensions
+    config.user_site_directory = True
+
     # We need this to make resourceutil happy, since it looks for sys.frozen.
     config.sys_frozen = True
     config.legacy_windows_stdio = True

The evolve extension doesn't load (as it did with the removed manual extension of sys.path), and print(sys.flags) from inside the debugshell command of the binary generated with this diff still prints "no_user_site=1, no_site=0".