diff -r c16af48153a4 Doc/library/venv.rst --- a/Doc/library/venv.rst Fri Nov 06 12:00:43 2015 +0200 +++ b/Doc/library/venv.rst Fri Nov 06 09:11:22 2015 -0500 @@ -88,7 +88,8 @@ creation according to their needs, the :class:`EnvBuilder` class. .. class:: EnvBuilder(system_site_packages=False, clear=False, \ - symlinks=False, upgrade=False, with_pip=False) + symlinks=False, upgrade=False, with_pip=False, \ + prompt=None) The :class:`EnvBuilder` class accepts the following keyword arguments on instantiation: @@ -112,9 +113,16 @@ installed in the virtual environment. This uses :mod:`ensurepip` with the ``--default-pip`` option. + * ``prompt`` -- a String to be used after virtual environment is activated + (defaults to ``None`` which means directory name of the environment would + be used). + .. versionchanged:: 3.4 Added the ``with_pip`` parameter + .. versionadded:: 3.6 + Added the ``prompt`` parameter + Creators of third-party virtual environment tools will be free to use the provided ``EnvBuilder`` class as a base class. diff -r c16af48153a4 Doc/whatsnew/3.6.rst --- a/Doc/whatsnew/3.6.rst Fri Nov 06 12:00:43 2015 +0200 +++ b/Doc/whatsnew/3.6.rst Fri Nov 06 09:11:22 2015 -0500 @@ -95,6 +95,14 @@ Improved Modules ================ +venv +---- + +:mod:`venv` accepts a new parameter ``--prompt``. This parameter provides an +alternative prefix for the environement. (Proposed by Łukasz.Balcerzak and +ported to 3.6 by Stéphane Wirtel in :issue:`22829`.) + + datetime -------- diff -r c16af48153a4 Lib/test/test_venv.py --- a/Lib/test/test_venv.py Fri Nov 06 12:00:43 2015 +0200 +++ b/Lib/test/test_venv.py Fri Nov 06 09:11:22 2015 -0500 @@ -110,6 +110,17 @@ print(' %r' % os.listdir(bd)) self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn) + def test_prompt(self): + env_name = os.path.split(self.env_dir)[1] + + builder = venv.EnvBuilder() + context = builder.ensure_directories(self.env_dir) + self.assertEqual(context.prompt, '(%s) ' % env_name) + + builder = venv.EnvBuilder(prompt='My prompt') + context = builder.ensure_directories(self.env_dir) + self.assertEqual(context.prompt, '(My prompt) ') + @skipInVenv def test_prefixes(self): """ diff -r c16af48153a4 Lib/venv/__init__.py --- a/Lib/venv/__init__.py Fri Nov 06 12:00:43 2015 +0200 +++ b/Lib/venv/__init__.py Fri Nov 06 09:11:22 2015 -0500 @@ -26,6 +26,8 @@ of Python, assuming Python has been upgraded in-place. --without-pip Skips installing or upgrading pip in the virtual environment (pip is bootstrapped by default) + --prompt=PROMPT Provides an alternative prompt prefix for this + environment. """ import logging import os @@ -63,12 +65,13 @@ """ def __init__(self, system_site_packages=False, clear=False, - symlinks=False, upgrade=False, with_pip=False): + symlinks=False, upgrade=False, with_pip=False, prompt=None): self.system_site_packages = system_site_packages self.clear = clear self.symlinks = symlinks self.upgrade = upgrade self.with_pip = with_pip + self.prompt = prompt def create(self, env_dir): """ @@ -114,7 +117,8 @@ context = types.SimpleNamespace() context.env_dir = env_dir context.env_name = os.path.split(env_dir)[1] - context.prompt = '(%s) ' % context.env_name + prompt = self.prompt if self.prompt is not None else context.env_name + context.prompt = '(%s) ' % prompt create_if_needed(env_dir) env = os.environ if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in env: @@ -350,7 +354,7 @@ def create(env_dir, system_site_packages=False, clear=False, - symlinks=False, with_pip=False): + symlinks=False, with_pip=False, prompt=None): """ Create a virtual environment in a directory. @@ -368,9 +372,11 @@ virtual environment. :param with_pip: If True, ensure pip is installed in the virtual environment + :param prompt: Alternative terminal prefix for the environment. """ builder = EnvBuilder(system_site_packages=system_site_packages, - clear=clear, symlinks=symlinks, with_pip=with_pip) + clear=clear, symlinks=symlinks, with_pip=with_pip, + prompt=prompt) builder.create(env_dir) def main(args=None): @@ -430,6 +436,9 @@ help='Skips installing or upgrading pip in the ' 'virtual environment (pip is bootstrapped ' 'by default)') + parser.add_argument('--prompt', + help='Provides an alternative prompt prefix for ' + 'this environment.') options = parser.parse_args(args) if options.upgrade and options.clear: raise ValueError('you cannot supply --upgrade and --clear together.') @@ -437,7 +446,8 @@ clear=options.clear, symlinks=options.symlinks, upgrade=options.upgrade, - with_pip=options.with_pip) + with_pip=options.with_pip, + prompt=options.prompt) for d in options.dirs: builder.create(d)