Prevent Python from importing the host's pip packages
This patch prevents Python from searching the user's installed pip
packages as well as the host's `site` package. The host's `site`
package normally performs the setup necessary to allow importing
globally installed pip packages. We don't want that.
From the upstream documentation:
https://docs.python.org/3/using/cmdline.html
-s
Don’t add the user site-packages directory to sys.path.
See also PEP 370 – Per user site-packages directory
-S
Disable the import of the module site and the
site-dependent manipulations of sys.path that it
entails. Also disable these manipulations if site is
explicitly imported later (call site.main() if you
want them to be triggered).
Change-Id: I11a64c33920fc174dd8ac5d200e2e8550a3c084a
Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
diff --git a/debian/opencv_python.BUILD b/debian/opencv_python.BUILD
index 5afa180..5074bd3 100644
--- a/debian/opencv_python.BUILD
+++ b/debian/opencv_python.BUILD
@@ -5,6 +5,9 @@
include = ["**/*"],
exclude = ["**/*.py"],
),
+ deps = [
+ "@python_repo//:numpy",
+ ],
imports = ["."],
visibility = ["//visibility:public"],
)
diff --git a/debian/python.BUILD b/debian/python.BUILD
index 17543df..009a7e2 100644
--- a/debian/python.BUILD
+++ b/debian/python.BUILD
@@ -55,11 +55,45 @@
visibility = ["//visibility:public"],
)
-filegroup(
+py_library(
name = "scipy",
srcs = glob([
- "usr/lib/python3/dist-packages/numpy",
- "usr/lib/python3/dist-packages/scipy",
+ "usr/lib/python3/dist-packages/scipy/**/*.py",
+ ]),
+ data = glob([
+ "usr/lib/python3/dist-packages/scipy/**/*",
+ ], exclude = [
+ "usr/lib/python3/dist-packages/scipy/**/*.py",
+ ]),
+ deps = [
+ ":numpy",
+ ],
+ visibility = ["//visibility:public"],
+ imports = [
+ "usr/lib/python3/dist-packages",
+ ],
+ target_compatible_with = [
+ "@platforms//os:linux",
+ "@platforms//cpu:x86_64",
+ ],
+)
+
+py_library(
+ name = "numpy",
+ srcs = glob([
+ "usr/lib/python3/dist-packages/numpy/**/*.py",
+ ]),
+ data = glob([
+ "usr/lib/python3/dist-packages/numpy/**/*",
+ ], exclude = [
+ "usr/lib/python3/dist-packages/numpy/**/*.py",
]),
visibility = ["//visibility:public"],
+ imports = [
+ "usr/lib/python3/dist-packages",
+ ],
+ target_compatible_with = [
+ "@platforms//os:linux",
+ "@platforms//cpu:x86_64",
+ ],
)
diff --git a/frc971/control_loops/python/BUILD b/frc971/control_loops/python/BUILD
index a4b8fb8..8679374 100644
--- a/frc971/control_loops/python/BUILD
+++ b/frc971/control_loops/python/BUILD
@@ -26,12 +26,12 @@
],
data = [
"//third_party/cddlib:_cddlib.so",
- "@python_repo//:scipy",
],
target_compatible_with = ["@platforms//cpu:x86_64"],
deps = [
":python_init",
"//external:python-glog",
+ "@python_repo//:scipy",
],
)
@@ -98,6 +98,7 @@
deps = [
":libspline",
":python_init",
+ "@python_repo//:numpy",
],
)
diff --git a/motors/fet12/BUILD b/motors/fet12/BUILD
index ac9849b..be744b2 100644
--- a/motors/fet12/BUILD
+++ b/motors/fet12/BUILD
@@ -84,7 +84,7 @@
srcs = [
"calib_sensors.py",
],
- data = [
+ deps = [
"@python_repo//:scipy",
],
target_compatible_with = ["@platforms//os:linux"],
@@ -95,7 +95,7 @@
srcs = [
"current_equalize.py",
],
- data = [
+ deps = [
":calib_sensors",
"@python_repo//:scipy",
],
diff --git a/motors/python/BUILD b/motors/python/BUILD
index 90ab608..2b2b80b 100644
--- a/motors/python/BUILD
+++ b/motors/python/BUILD
@@ -3,9 +3,6 @@
srcs = [
"big_phase_current.py",
],
- data = [
- "@python_repo//:scipy",
- ],
legacy_create_init = False,
target_compatible_with = ["@platforms//cpu:x86_64"],
deps = [
@@ -14,6 +11,7 @@
"//external:python-glog",
"//frc971/control_loops/python:controls",
"@matplotlib_repo//:matplotlib3",
+ "@python_repo//:scipy",
],
)
diff --git a/tools/python/runtime_binary.sh b/tools/python/runtime_binary.sh
index 8380424..9cd1519 100755
--- a/tools/python/runtime_binary.sh
+++ b/tools/python/runtime_binary.sh
@@ -36,4 +36,5 @@
export LD_LIBRARY_PATH="${BASE_PATH}/usr/lib/lapack:${BASE_PATH}/usr/lib/libblas:${BASE_PATH}/usr/lib/x86_64-linux-gnu"
-exec "$BASE_PATH"/usr/bin/python3 "$@"
+# Prevent Python from importing the host's installed packages.
+exec "$BASE_PATH"/usr/bin/python3 -sS "$@"
diff --git a/y2018/control_loops/python/BUILD b/y2018/control_loops/python/BUILD
index 26b2da4..c74396c 100644
--- a/y2018/control_loops/python/BUILD
+++ b/y2018/control_loops/python/BUILD
@@ -191,6 +191,7 @@
target_compatible_with = ["@platforms//os:linux"],
deps = [
":python_init",
+ "@python_repo//:numpy",
],
)
diff --git a/y2020/vision/tools/python_code/BUILD b/y2020/vision/tools/python_code/BUILD
index 1e27f65..7579a75 100644
--- a/y2020/vision/tools/python_code/BUILD
+++ b/y2020/vision/tools/python_code/BUILD
@@ -4,12 +4,10 @@
py_library(
name = "train_and_match",
srcs = ["train_and_match.py"],
- data = [
- "@python_repo//:scipy",
- ],
deps = [
"//external:python-glog",
"@opencv_contrib_nonfree_amd64//:python_opencv",
+ "@python_repo//:scipy",
],
)
@@ -18,13 +16,11 @@
srcs = [
"define_training_data.py",
],
- data = [
- "@python_repo//:scipy",
- ],
deps = [
":train_and_match",
"//external:python-glog",
"@opencv_contrib_nonfree_amd64//:python_opencv",
+ "@python_repo//:scipy",
],
)