Skip to content

Commit 7d63780

Browse files
authoredJan 14, 2020
bpo-38901: Allow setting a venv's prompt to the basename of the current directory. (GH-17946)
When a prompt value of '.' is specified, os.path.basename(os.getcwd()) is used to configure the prompt for the created venv.
1 parent 4b0d91a commit 7d63780

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed
 

‎Doc/library/venv.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ creation according to their needs, the :class:`EnvBuilder` class.
122122

123123
* ``prompt`` -- a String to be used after virtual environment is activated
124124
(defaults to ``None`` which means directory name of the environment would
125-
be used).
125+
be used). If the special string ``"."`` is provided, the basename of the
126+
current directory is used as the prompt.
126127

127128
* ``upgrade_deps`` -- Update the base venv modules to the latest on PyPI
128129

‎Lib/test/test_venv.py

+9
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ def test_prompt(self):
138138
self.assertEqual(context.prompt, '(My prompt) ')
139139
self.assertIn("prompt = 'My prompt'\n", data)
140140

141+
rmtree(self.env_dir)
142+
builder = venv.EnvBuilder(prompt='.')
143+
cwd = os.path.basename(os.getcwd())
144+
self.run_with_capture(builder.create, self.env_dir)
145+
context = builder.ensure_directories(self.env_dir)
146+
data = self.get_text_file_contents('pyvenv.cfg')
147+
self.assertEqual(context.prompt, '(%s) ' % cwd)
148+
self.assertIn("prompt = '%s'\n" % cwd, data)
149+
141150
def test_upgrade_dependencies(self):
142151
builder = venv.EnvBuilder()
143152
bin_path = 'Scripts' if sys.platform == 'win32' else 'bin'

‎Lib/venv/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ def __init__(self, system_site_packages=False, clear=False,
5151
self.symlinks = symlinks
5252
self.upgrade = upgrade
5353
self.with_pip = with_pip
54+
if prompt == '.': # see bpo-38901
55+
prompt = os.path.basename(os.getcwd())
5456
self.prompt = prompt
5557
self.upgrade_deps = upgrade_deps
5658

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
When you specify prompt='.' or equivalently python -m venv --prompt . ...
2+
the basename of the current directory is used to set the created venv's
3+
prompt when it's activated.

0 commit comments

Comments
 (0)