Skip to content
This repository was archived by the owner on Apr 21, 2021. It is now read-only.

Commit a07e0d6

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 pypa#1862
1 parent b0445a2 commit a07e0d6

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
@@ -56,6 +56,18 @@ def setup_verbose(ctx, param, value):
5656
return value
5757

5858

59+
def validate_python_path(ctx, param, value):
60+
# Validating the Python path is complicated by accepting a number of
61+
# friendly options: the default will be boolean False to enable
62+
# autodetection but it may also be a value which will be searched in
63+
# the path or an absolute path. To report errors as early as possible
64+
# we'll report absolute paths which do not exist:
65+
if isinstance(value, (str, bytes)):
66+
if os.path.isabs(value) and not os.path.isfile(value):
67+
raise click.BadParameter('Expected Python at path %s does not exist' % value)
68+
return value
69+
70+
5971
@click.group(
6072
cls=PipenvGroup,
6173
invoke_without_command=True,
@@ -106,6 +118,7 @@ def setup_verbose(ctx, param, value):
106118
'--python',
107119
default=False,
108120
nargs=1,
121+
callback=validate_python_path,
109122
help="Specify which version of Python virtualenv should use.",
110123
)
111124
@click.option(
@@ -263,6 +276,7 @@ def cli(
263276
'--python',
264277
default=False,
265278
nargs=1,
279+
callback=validate_python_path,
266280
help="Specify which version of Python virtualenv should use.",
267281
)
268282
@click.option(
@@ -382,6 +396,7 @@ def install(
382396
'--python',
383397
default=False,
384398
nargs=1,
399+
callback=validate_python_path,
385400
help="Specify which version of Python virtualenv should use.",
386401
)
387402
@click.option(
@@ -453,6 +468,7 @@ def uninstall(
453468
'--python',
454469
default=False,
455470
nargs=1,
471+
callback=validate_python_path,
456472
help="Specify which version of Python virtualenv should use.",
457473
)
458474
@click.option(
@@ -527,6 +543,7 @@ def lock(
527543
'--python',
528544
default=False,
529545
nargs=1,
546+
callback=validate_python_path,
530547
help="Specify which version of Python virtualenv should use.",
531548
)
532549
@click.option(
@@ -594,6 +611,7 @@ def shell(
594611
'--python',
595612
default=False,
596613
nargs=1,
614+
callback=validate_python_path,
597615
help="Specify which version of Python virtualenv should use.",
598616
)
599617
def run(command, args, three=None, python=False):
@@ -616,6 +634,7 @@ def run(command, args, three=None, python=False):
616634
'--python',
617635
default=False,
618636
nargs=1,
637+
callback=validate_python_path,
619638
help="Specify which version of Python virtualenv should use.",
620639
)
621640
@click.option(
@@ -655,6 +674,7 @@ def check(
655674
'--python',
656675
default=False,
657676
nargs=1,
677+
callback=validate_python_path,
658678
help="Specify which version of Python virtualenv should use.",
659679
)
660680
@click.option(
@@ -819,6 +839,7 @@ def graph(bare=False, json=False, reverse=False):
819839
'--python',
820840
default=False,
821841
nargs=1,
842+
callback=validate_python_path,
822843
help="Specify which version of Python virtualenv should use.",
823844
)
824845
@click.argument('module', nargs=1)
@@ -874,6 +895,7 @@ def run_open(module, three=None, python=None):
874895
'--python',
875896
default=False,
876897
nargs=1,
898+
callback=validate_python_path,
877899
help="Specify which version of Python virtualenv should use.",
878900
)
879901
@click.option('--bare', is_flag=True, default=False, help="Minimal output.")
@@ -940,6 +962,7 @@ def sync(
940962
'--python',
941963
default=False,
942964
nargs=1,
965+
callback=validate_python_path,
943966
help="Specify which version of Python virtualenv should use.",
944967
)
945968
@click.option(

0 commit comments

Comments
 (0)