Skip to content

Commit 805a292

Browse files
committed
Invoke -mvenv with the original python if in a -mvirtualenv venv
1 parent 97fb49a commit 805a292

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

pre_commit/languages/python_venv.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
11
from __future__ import unicode_literals
22

3+
import os.path
4+
35
from pre_commit.languages import python
6+
from pre_commit.util import CalledProcessError
47
from pre_commit.util import cmd_output
58

69

710
ENVIRONMENT_DIR = 'py_venv'
811

912

13+
def orig_py_exe(exe): # pragma: no cover (platform specific)
14+
"""A -mvenv virtualenv made from a -mvirtualenv virtualenv installs
15+
packages to the incorrect location. Attempt to find the _original_ exe
16+
and invoke `-mvenv` from there.
17+
18+
See:
19+
- https://github.com/pre-commit/pre-commit/issues/755
20+
- https://github.com/pypa/virtualenv/issues/1095
21+
- https://bugs.python.org/issue30811
22+
"""
23+
try:
24+
prefix_script = 'import sys; print(sys.real_prefix)'
25+
_, prefix, _ = cmd_output(exe, '-c', prefix_script)
26+
prefix = prefix.strip()
27+
except CalledProcessError:
28+
# not created from -mvirtualenv
29+
return exe
30+
31+
if os.name == 'nt':
32+
expected = os.path.join(prefix, 'python.exe')
33+
else:
34+
expected = os.path.join(prefix, 'bin', os.path.basename(exe))
35+
36+
if os.path.exists(expected):
37+
return expected
38+
else:
39+
return exe
40+
41+
1042
def make_venv(envdir, python):
11-
cmd_output(python, '-mvenv', envdir, cwd='/')
43+
cmd_output(orig_py_exe(python), '-mvenv', envdir, cwd='/')
1244

1345

1446
get_default_version = python.get_default_version

0 commit comments

Comments
 (0)