2424 raised.
2525 --upgrade Upgrade the environment directory to use this version
2626 of Python, assuming Python has been upgraded in-place.
27+ --without-pip Skips installing or upgrading pip in the virtual
28+ environment (pip is bootstrapped by default)
2729"""
2830import logging
2931import os
3032import shutil
33+ import subprocess
3134import sys
3235import sysconfig
3336import types
@@ -56,14 +59,17 @@ class EnvBuilder:
5659 :param symlinks: If True, attempt to symlink rather than copy files into
5760 virtual environment.
5861 :param upgrade: If True, upgrade an existing virtual environment.
62+ :param with_pip: If True, ensure pip is installed in the virtual
63+ environment
5964 """
6065
6166 def __init__ (self , system_site_packages = False , clear = False ,
62- symlinks = False , upgrade = False ):
67+ symlinks = False , upgrade = False , with_pip = False ):
6368 self .system_site_packages = system_site_packages
6469 self .clear = clear
6570 self .symlinks = symlinks
6671 self .upgrade = upgrade
72+ self .with_pip = with_pip
6773
6874 def create (self , env_dir ):
6975 """
@@ -76,6 +82,8 @@ def create(self, env_dir):
7682 context = self .ensure_directories (env_dir )
7783 self .create_configuration (context )
7884 self .setup_python (context )
85+ if self .with_pip :
86+ self ._setup_pip (context )
7987 if not self .upgrade :
8088 self .setup_scripts (context )
8189 self .post_setup (context )
@@ -224,6 +232,12 @@ def setup_python(self, context):
224232 shutil .copyfile (src , dst )
225233 break
226234
235+ def _setup_pip (self , context ):
236+ """Installs or upgrades pip in a virtual environment"""
237+ cmd = [context .env_exe , '-m' , 'ensurepip' , '--upgrade' ,
238+ '--default-pip' ]
239+ subprocess .check_output (cmd )
240+
227241 def setup_scripts (self , context ):
228242 """
229243 Set up scripts into the created environment from a directory.
@@ -317,7 +331,8 @@ def install_scripts(self, context, path):
317331 shutil .copymode (srcfile , dstfile )
318332
319333
320- def create (env_dir , system_site_packages = False , clear = False , symlinks = False ):
334+ def create (env_dir , system_site_packages = False , clear = False ,
335+ symlinks = False , with_pip = False ):
321336 """
322337 Create a virtual environment in a directory.
323338
@@ -333,9 +348,11 @@ def create(env_dir, system_site_packages=False, clear=False, symlinks=False):
333348 raised.
334349 :param symlinks: If True, attempt to symlink rather than copy files into
335350 virtual environment.
351+ :param with_pip: If True, ensure pip is installed in the virtual
352+ environment
336353 """
337354 builder = EnvBuilder (system_site_packages = system_site_packages ,
338- clear = clear , symlinks = symlinks )
355+ clear = clear , symlinks = symlinks , with_pip = with_pip )
339356 builder .create (env_dir )
340357
341358def main (args = None ):
@@ -390,12 +407,19 @@ def main(args=None):
390407 'directory to use this version '
391408 'of Python, assuming Python '
392409 'has been upgraded in-place.' )
410+ parser .add_argument ('--without-pip' , dest = 'with_pip' ,
411+ default = True , action = 'store_false' ,
412+ help = 'Skips installing or upgrading pip in the '
413+ 'virtual environment (pip is bootstrapped '
414+ 'by default)' )
393415 options = parser .parse_args (args )
394416 if options .upgrade and options .clear :
395417 raise ValueError ('you cannot supply --upgrade and --clear together.' )
396418 builder = EnvBuilder (system_site_packages = options .system_site ,
397- clear = options .clear , symlinks = options .symlinks ,
398- upgrade = options .upgrade )
419+ clear = options .clear ,
420+ symlinks = options .symlinks ,
421+ upgrade = options .upgrade ,
422+ with_pip = options .with_pip )
399423 for d in options .dirs :
400424 builder .create (d )
401425
0 commit comments