Skip to content

Commit cc4498a

Browse files
committed
fix(installer): ensure system site-packages has priority over server-libs
Add priority parameter to import_package_bundle that uses sys.path.insert(0) to place packages at the front of sys.path. System site-packages now uses this priority flag to ensure user-installed packages are found before bundled server-libs packages.
1 parent 28a70ec commit cc4498a

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

installer/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def bootstrap():
273273
venv.import_package_bundle(
274274
server_site_packages_path, condition_env="DEEPNOTE_INCLUDE_SERVER_PACKAGES"
275275
)
276-
venv.import_package_bundle(system_site_packages_path)
276+
venv.import_package_bundle(system_site_packages_path, priority=True)
277277
venv.import_package_bundle(kernel_site_package_path)
278278

279279
# Phase 7.2: Symlink notebook to jupyter_server for compatibility with

installer/module/virtual_environment.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,31 +81,40 @@ def start_server(self, command: str, cwd: Optional[str] = None) -> ServerProcess
8181
return server_proc
8282

8383
def import_package_bundle(
84-
self, bundle_site_package_path: str, condition_env: Optional[str] = None
84+
self, bundle_site_package_path: str,
85+
condition_env: Optional[str] = None,
86+
priority: bool = False,
8587
) -> None:
8688
"""
8789
Import a package bundle to the virtual environment.
8890
8991
:param bundle_site_package_path: Absolute path to the package bundle.
9092
:param condition_env: Optional environment variable name. If provided, the bundle
91-
will only be loaded when this env var is set to 'true'.
93+
will only be loaded when this env var is set to 'true'.
94+
:param priority: If True, insert at front of sys.path to override other bundles.
9295
"""
9396
pth_file_path = os.path.join(self._site_packages_path, "deepnote.pth")
9497

9598
with open(pth_file_path, "a+", encoding="utf-8") as pth_file:
9699
if condition_env:
97100
# Write conditional import that checks environment variable
98101
pth_content = (
99-
f"import sys, os, sysconfig; "
100-
f"(sys.path.append('{bundle_site_package_path}'), "
101-
f"sys.path.insert(sys.path.index('{bundle_site_package_path}'), sysconfig.get_path('purelib'))) "
102+
f"import os, sys; "
103+
f"sys.path.insert(0, '{bundle_site_package_path}') "
102104
f"if os.environ.get('{condition_env}', '').lower() == 'true' else None"
103105
)
104106
pth_file.write(pth_content + "\n")
105107
logger.info(
106108
"Bundle was conditionally imported to the virtual environment "
107109
f"(loads when {condition_env}=true)."
108110
)
111+
elif priority:
112+
# Insert at front of sys.path for higher priority (overrides other bundles)
113+
pth_content = f"import sys; sys.path.insert(0, '{bundle_site_package_path}')"
114+
pth_file.write(pth_content + "\n")
115+
logger.info(
116+
"Bundle was imported with priority to the virtual environment."
117+
)
109118
else:
110119
pth_file.write(bundle_site_package_path + "\n")
111120
logger.info(

0 commit comments

Comments
 (0)