Skip to content

Commit 1fa5d72

Browse files
committed
python-setup: Fail early if installing for Python 2, and python2 not available
1 parent 3f97671 commit 1fa5d72

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

python-setup/auto_install_packages.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import subprocess
66
from tempfile import mkdtemp
77
from typing import Optional
8+
import shutil
89

910
import extractor_version
1011

@@ -154,6 +155,17 @@ def install_packages(codeql_base_dir) -> Optional[str]:
154155
# get_extractor_version returns the Python version the extractor thinks this repo is using
155156
version = extractor_version.get_extractor_version(codeql_base_dir, quiet=False)
156157

158+
if version == 2 and not sys.platform.startswith('win32'):
159+
# On Ubuntu 22.04 'python2' is not available by default. We want to give a slightly better
160+
# error message than a traceback + `No such file or directory: 'python2'`
161+
if shutil.which("python2") is None:
162+
sys.exit(
163+
"package installation failed: we detected this code as Python 2, but 'python2' executable was not available."
164+
"To enable automatic package installation, please install 'python2' before the 'github/codeql-action/init' step, "
165+
"such as running 'sudo apt install python2' (Ubuntu 22.04)."
166+
"If your code is not Python 2, but actually Python 3, please file a bug report at https://github.com/github/codeql-action/issues/new"
167+
)
168+
157169
if os.path.exists('requirements.txt'):
158170
print('Found requirements.txt, will install packages with pip', flush=True)
159171
return install_requirements_txt_packages(version)

0 commit comments

Comments
 (0)