This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: venv: activate.bat fails for venv with special characters in PATH
Type: behavior Stage: needs patch
Components: Library (Lib), Windows Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: MB113, eryksun, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2021-06-30 12:44 by MB113, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
activate.bat eryksun, 2021-12-09 05:40
deactivate.bat eryksun, 2021-12-09 05:41
Messages (2)
msg396776 - (view) Author: (MB113) Date: 2021-06-30 12:44
If your virtual env Scripts folder is in a path with a special character (for batch-scripts) it will give this error:  

The system cannot find the path specified.

In my case it was a '&' in my path.

Now I see that the fix from specifically this issue: https://bugs.python.org/issue36634 has caused my problems.

So in the end neither works.
set "VIRTUAL_ENV=__VENV_DIR__" doesn't work with quotes
set VIRTUAL_ENV=__VENV_DIR__  doesn't work with 'special chars' (&*)
msg408073 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-12-09 05:40
There's no parsing problem with delayed expansion (e.g. "!VAR!" instead of %VAR%) and for loop variables (e.g. "%%a"). The solution is thus to execute in a local scope that enables delayed expansion and command extensions [1]. 

That's easy enough, but then there's the problem of how to evaluate the `set` commands in the global scope, without the help of delayed expansion to prevent parsing errors. I did some research, and apparently the common trick is to end the local scope inside of a `for /f` loop. Then subsequent `set "%%a"` statements in iterations of the loop execute in the global scope. 

The final problem is building the command to execute in the local scope, for which the `for /f` loop iterates the output lines. `set var` returns a line with "var=value", which can be passed back into `set "%%a"`. So I decided to build up a sequence of `&` concatenated `set var` and `echo var=` (clear) statements in an EXPORTS variable.

I added default values for PROMPT ($P$G) and PATH (%SystemRoot%;%SystemRoot%\System32), for when they're not defined. I made sure that deactivate.bat does not leave these defaults in place. The original values, whether defined or not, should be restored exactly as they were.

All of this adds to the length of the scripts. They're roughly doubled in size. But there should be no more problems with an odd number of quotes and/or special characters in any of the variables that get set.

---
[1] The activate.bat and deactivate.bat scripts have been naively relying 
    on the default enabled state of command extensions, e.g. `for /f`
    loops and `if defined` statements.
History
Date User Action Args
2022-04-11 14:59:47adminsetgithub: 88706
2021-12-09 05:41:07eryksunsetfiles: + deactivate.bat
2021-12-09 05:40:50eryksunsetfiles: + activate.bat
nosy: + eryksun
messages: + msg408073

2021-12-05 03:43:01eryksunsetstage: needs patch
versions: + Python 3.10, Python 3.11
2021-12-05 02:54:32andrei.avklinkissue44735 superseder
2021-06-30 12:44:01MB113create