Merge pull request #1431 from github/rasmuswl/poetry-always-install-pip · github/codeql-action@32be38e · GitHub
Skip to content

Commit 32be38e

Browse files
authored
Merge pull request #1431 from github/rasmuswl/poetry-always-install-pip
python-setup: Handle poetry `virtualenvs.options.no-pip = true`
2 parents 2073a69 + 5ed1e98 commit 32be38e

7 files changed

Lines changed: 45 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion

‎lib/analyze.js‎

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/analyze.js.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎python-setup/find_site_packages.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Print the path to the site-packages directory for the current Python environment.
3+
"""
4+
from __future__ import print_function
5+
6+
try:
7+
import pip
8+
import os
9+
print(os.path.dirname(os.path.dirname(pip.__file__)))
10+
except ImportError:
11+
import sys
12+
print("DEBUG: could not import pip", file=sys.stderr)
13+
# if you use poetry with `virtualenvs.options.no-pip = true` you might end up with a
14+
# virtualenv without pip, so the above trick doesn't actually work. See
15+
# https://python-poetry.org/docs/configuration/#virtualenvsoptionsno-pip
16+
#
17+
# A possible option is to install `pip` into the virtualenv created by poetry
18+
# (`poetry add pip`), but it turns out that doesn't always work :( for the test
19+
# poetry/requests-3, I was not allowed to install pip! So I did not pursue this
20+
# option further.
21+
#
22+
# Instead, testing `site.getsitepackages()` contains has the right path, whereas
23+
# `site.getusersitepackages()` is about the system python (very confusing).
24+
#
25+
# We can't use the environment variable POETRY_VIRTUALENVS_OPTIONS_NO_PIP because it
26+
# does not work, see https://github.com/python-poetry/poetry/issues/5906
27+
import site
28+
29+
if sys.platform.startswith("win32"):
30+
# On windows, the last entry of `site.getsitepackages()` has the right path
31+
print(site.getsitepackages()[-1])
32+
else:
33+
# on unix, the first entry of `site.getsitepackages()` has the right path
34+
print(site.getsitepackages()[0])

‎python-setup/tests/from_python_exe.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ def get_details(path_to_python_exe: str) -> Tuple[str, str]:
99
import_path = subprocess.check_output(
1010
[
1111
path_to_python_exe,
12-
"-c",
13-
"import os; import pip; print(os.path.dirname(os.path.dirname(pip.__file__)))",
12+
os.path.join(os.path.dirname(__file__), "..", "find_site_packages.py")
1413
],
1514
stdin=subprocess.DEVNULL,
1615
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
[virtualenvs]
22
in-project = true
3+
4+
[virtualenvs.options]
5+
no-pip = true

‎src/analyze.ts‎

Lines changed: 3 additions & 4 deletions

0 commit comments

Comments
 (0)