Skip to content

Commit e3ee7fb

Browse files
committed
Validate absolute python paths
This makes it easier for someone to realize when they have provided an invalid Python path without missing that message due to the sizable virtualenv traceback which follows it. I first encountered this when someone copy-and-pasted the example from the Conda documentation which has `--python=/path/to/anaconda/python`. See #1862
1 parent 09fb7d0 commit e3ee7fb

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

pipenv/cli.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ def setup_verbose(ctx, param, value):
6767
return value
6868

6969

70+
def validate_python_path(ctx, param, value):
71+
# Validating the Python path is complicated by accepting a number of
72+
# friendly options: the default will be boolean False to enable
73+
# autodetection but it may also be a value which will be searched in
74+
# the path or an absolute path. To report errors as early as possible
75+
# we'll report absolute paths which do not exist:
76+
if isinstance(value, (str, bytes)):
77+
if os.path.isabs(value) and not os.path.isfile(value):
78+
raise click.BadParameter('Expected Python at path %s does not exist' % value)
79+
return value
80+
81+
7082
@group(
7183
cls=PipenvGroup,
7284
invoke_without_command=True,
@@ -117,6 +129,7 @@ def setup_verbose(ctx, param, value):
117129
'--python',
118130
default=False,
119131
nargs=1,
132+
callback=validate_python_path,
120133
help="Specify which version of Python virtualenv should use.",
121134
)
122135
@option(
@@ -285,6 +298,7 @@ def cli(
285298
'--python',
286299
default=False,
287300
nargs=1,
301+
callback=validate_python_path,
288302
help="Specify which version of Python virtualenv should use.",
289303
)
290304
@option(
@@ -404,6 +418,7 @@ def install(
404418
'--python',
405419
default=False,
406420
nargs=1,
421+
callback=validate_python_path,
407422
help="Specify which version of Python virtualenv should use.",
408423
)
409424
@option(
@@ -475,6 +490,7 @@ def uninstall(
475490
'--python',
476491
default=False,
477492
nargs=1,
493+
callback=validate_python_path,
478494
help="Specify which version of Python virtualenv should use.",
479495
)
480496
@option(
@@ -546,6 +562,7 @@ def lock(
546562
'--python',
547563
default=False,
548564
nargs=1,
565+
callback=validate_python_path,
549566
help="Specify which version of Python virtualenv should use.",
550567
)
551568
@option(
@@ -612,6 +629,7 @@ def shell(
612629
'--python',
613630
default=False,
614631
nargs=1,
632+
callback=validate_python_path,
615633
help="Specify which version of Python virtualenv should use.",
616634
)
617635
def run(command, args, three=None, python=False):
@@ -633,6 +651,7 @@ def run(command, args, three=None, python=False):
633651
'--python',
634652
default=False,
635653
nargs=1,
654+
callback=validate_python_path,
636655
help="Specify which version of Python virtualenv should use.",
637656
)
638657
@option(
@@ -672,6 +691,7 @@ def check(
672691
'--python',
673692
default=False,
674693
nargs=1,
694+
callback=validate_python_path,
675695
help="Specify which version of Python virtualenv should use.",
676696
)
677697
@option(
@@ -841,6 +861,7 @@ def graph(bare=False, json=False, reverse=False):
841861
'--python',
842862
default=False,
843863
nargs=1,
864+
callback=validate_python_path,
844865
help="Specify which version of Python virtualenv should use.",
845866
)
846867
@argument('module', nargs=1)
@@ -896,6 +917,7 @@ def run_open(module, three=None, python=None):
896917
'--python',
897918
default=False,
898919
nargs=1,
920+
callback=validate_python_path,
899921
help="Specify which version of Python virtualenv should use.",
900922
)
901923
@option('--bare', is_flag=True, default=False, help="Minimal output.")
@@ -962,6 +984,7 @@ def sync(
962984
'--python',
963985
default=False,
964986
nargs=1,
987+
callback=validate_python_path,
965988
help="Specify which version of Python virtualenv should use.",
966989
)
967990
@option(

0 commit comments

Comments
 (0)