Skip to content

Commit 301dc31

Browse files
committed
Added ability to configure pip to require an activated virtualenv to run and/or to automatically use an activated virtualenv if found
1 parent ea0a4d4 commit 301dc31

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

pip.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,21 @@ def get_backend_from_location(self, location):
253253
type='str',
254254
default='',
255255
help=optparse.SUPPRESS_HELP)
256+
parser.add_option(
257+
# Run only if inside a virtualenv, bail if not.
258+
'--require-virtualenv', '--require-venv',
259+
dest='require_venv',
260+
action='store_true',
261+
default=False,
262+
help=optparse.SUPPRESS_HELP)
263+
parser.add_option(
264+
# Use automatically an activated virtualenv instead of installing
265+
# globally. -E will be ignored if used.
266+
'--respect-virtualenv', '--respect-venv',
267+
dest='respect_venv',
268+
action='store_true',
269+
default=False,
270+
help=optparse.SUPPRESS_HELP)
256271

257272
parser.add_option(
258273
'-v', '--verbose',
@@ -359,11 +374,34 @@ def main(self, complete_args, args, initial_options):
359374
options, args = self.parser.parse_args(args)
360375
self.merge_options(initial_options, options)
361376

377+
if options.require_venv and not options.venv:
378+
# If a venv is required check if it can really be found
379+
if not os.environ.get('VIRTUAL_ENV'):
380+
print 'Could not find an activated virtualenv (required).'
381+
sys.exit(3)
382+
# Automatically install in currently activated venv if required
383+
options.respect_venv = True
384+
362385
if args and args[-1] == '___VENV_RESTART___':
363386
## FIXME: We don't do anything this this value yet:
364387
venv_location = args[-2]
365388
args = args[:-2]
366389
options.venv = None
390+
else:
391+
# If given the option to respect the activated environment
392+
# check if no venv is given as a command line parameter
393+
if options.respect_venv and os.environ.get('VIRTUAL_ENV'):
394+
if options.venv and os.path.exists(options.venv):
395+
# Make sure command line venv and environmental are the same
396+
if (os.path.realpath(os.path.expanduser(options.venv)) !=
397+
os.path.realpath(os.environ.get('VIRTUAL_ENV'))):
398+
print ("Given virtualenv (%s) doesn't match "
399+
"currently activated virtualenv (%s)."
400+
% (options.venv, os.environ.get('VIRTUAL_ENV')))
401+
sys.exit(3)
402+
else:
403+
options.venv = os.environ.get('VIRTUAL_ENV')
404+
print 'Using already activated environment %s' % options.venv
367405
level = 1 # Notify
368406
level += options.verbose
369407
level -= options.quiet
@@ -550,8 +588,7 @@ def __init__(self):
550588
help="Extra arguments to be supplied to the setup.py install "
551589
"command (use like --install-option=\"--install-scripts=/usr/local/bin\"). "
552590
"Use multiple --install-option options to pass multiple options to setup.py install. "
553-
"If you are using an option with a directory path, be sure to use absolute path."
554-
)
591+
"If you are using an option with a directory path, be sure to use absolute path.")
555592

556593
def run(self, options, args):
557594
if not options.build_dir:

0 commit comments

Comments
 (0)