I just tested microvenv on Windows. The main problem with using the “python” symlink is that the loader on Windows doesn’t resolve symlinks to the real path of the application directory when searching for dependencies. It can be made to work with a few modifications.
- Delete the “python3” and “python3.x” symlinks.
- Rename the “python” symlink to “python.exe”.
- Symlink the DLL dependencies (“python311.dll”, “python3.dll”, “vcruntime140.dll”, and “vcruntime140_1.dll”) in the “Scripts” directory.
- Instead of the latter step, you could add the base installation directory to the
PATH
environment variable, but I prefer to avoid that.
For example, in the CMD shell:
C:\Temp>py -3.11 -m microvenv
C:\Temp>del .venv\Scripts\python3*.*
C:\Temp>ren .venv\Scripts\python python.exe
C:\Temp>for /f "tokens=*" %p in ('py -3.11 -c "import sys; print(sys.prefix)"') do @set "PREFIX=%p"
C:\Temp>for %f in ("%PREFIX%\*.dll") do @mklink ".venv\Scripts\%~nxf" "%f" 1>nul
C:\Temp>.venv\Scripts\python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'C:\\Temp\\.venv\\Scripts\\python.exe'
>>> sys._base_executable
'C:\\Program Files\\Python311\\python.exe'
>>> sys.prefix
'C:\\Temp\\.venv'
>>> sys.base_prefix
'C:\\Program Files\\Python311'
>>> print(*sys.path, sep='\n')
C:\Temp\.venv\Scripts\python311.zip
C:\Program Files\Python311\DLLs
C:\Program Files\Python311\Lib
C:\Program Files\Python311
C:\Temp\.venv
C:\Temp\.venv\Lib\site-packages
Note that the command
value in “pyvenv.cfg” isn’t quoted properly to support paths with spaces in the command line. It doesn’t seem to cause a problem, but I don’t know how this value gets used.
Filesystem symlinks are implemented as reparse points. NTFS and ReFS support reparse points. However, being allowed to actually create a symlink requires either “SeCreateSymbolicLinkPrivilege” or for the system to be in developer mode.
By default “SeCreateSymbolicLinkPrivilege” is only granted to administrators. Also, for an administrator account, only an elevated logon will have the administrators group enabled, given that UAC is enabled for both the system and the account. That said, in the system management console (“secpol.msc” snap-in), an administrator can assign the right to create symbolic links to any user or group, such as the “Users” group or “Authenticated Users” group.
Note that if you need to run a virtual environment directly from Explorer, then you’ll have to use the venv launcher instead of symlinks because Explorer eagerly resolves the final path of a symlink before executing it. Also, the venv launcher is required for an environment that’s based on the store app distribution of Python.