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: Cannot use virtual environment on Windows 10 in corporate security settings
Type: behavior Stage: resolved
Components: Installation Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, gregory.p.smith, paul.moore, steve.dower, tim.golden, wolfgang-kuehn, zach.ware
Priority: normal Keywords:

Created on 2021-12-12 12:45 by wolfgang-kuehn, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (9)
msg408380 - (view) Author: wolfgang kuehn (wolfgang-kuehn) Date: 2021-12-12 12:45
I just installed Python3.10.1 from the Windows 10 App Store. 
Most workflows depend on creating virtual environments, but (1)

    python -m venv venv

    # -> Error 1260: Windows cannot open this program because it has been prevented by a software restriction policy

However, a (2)

    python -m venv --without-pip venv

completes, only to not allow execution of (3)

    .\venv\Scripts\python.exe
    # -> Error 1260: Windows cannot open this program because it has been prevented by a software restriction policy


Reason for this probably are the (not so unreasonable) cooperate Software Restriction Policy (in our case enforced by Applocker):
You are not allowed to execute from where you are allowed to write.

So basically Python is broken in many MS Windows cooperate settings. Cooperate meaning (Software Restriction Policies) + (Usage of Virtual Environments). 

And my feeling is that it does not need to be, the virtual environment implementation with those *.exe copies being a kludge, IMHO.


Note: (2) is reminiscent of bpo-45337, which was fixed with 3.9.
msg408424 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-12-13 05:08
Try using symlinks if you're allowed, e.g. `python -m venv --symlinks <name>`.

Note that a virtual environment created with symlinks is unreliable in some cases because ShellExecute[Ex]W() eagerly resolves a symlink before calling CreateProcessW().

Also, you won't be able to use EXE script wrappers in an active environment due to the security restrictions in place on your system. You'll need to use `python -m <module>` alternative commands, such as `python -m pip` instead of `pip`.
msg408435 - (view) Author: wolfgang kuehn (wolfgang-kuehn) Date: 2021-12-13 10:05
symlinks do not work for me, this may be another bug (should I create a new issue?):

    python -m venv --without-pip --symlinks venv

Unable to symlink 'C:\\Users\\****\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\idle.exe' to 'C:\\****\venv\\Scripts\\idle.exe'
Error: [Errno 22] Invalid argument: 'C:\\Users\\****\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\idle.exe'
msg408441 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-12-13 10:52
> symlinks do not work for me

Sorry, I forgot that you're using the store app. The store app has to use the copied venv launchers. 

When a store app is run from the command line, the system executes an appexec link from "%LocalAppData%\Microsoft\WindowsApps". Unfortunately appexec links have to be executed directly in order for CreateProcessW() to find the required app information that's in the link. The API doesn't manually reparse symlinks until it reaches an appexec link.
msg408443 - (view) Author: wolfgang kuehn (wolfgang-kuehn) Date: 2021-12-13 11:18
Currently we have a glitch in our internal access rights system. This resulted in me loosing my Admin-privileges.
I can only install python from the app-store, which is ok.
But without venv support I am stuck for the time being.
msg408485 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2021-12-13 22:31
If you execute "python -m venv --without-pip ..." to create, then as a workaround you can set the __PYVENV_LAUNCHER__ environment variable to the full path to the venv's python.exe and run the normal python3.10.exe.

As Eryk mentioned, you'll need to run everything through "-m" instead of the script wrappers that pip would install. This includes "-m ensurepip", which should be able to install pip into the venv normally.

Note that this is an undocumented/unsupported environment variable, and so it may change in later releases. But it should work fine throughout 3.10's lifetime (and it hasn't changed _yet_ in 3.11).
msg408486 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2021-12-13 22:32
Also, if you see people discussing PEP 582, you might want to throw in a vote of support. It is intended to provide the benefits of a venv without needing to do tricks like we do for the current design, but it keeps being rejected for "not being sufficiently useful".

Seems like in your case it would be very useful. That might help sway some of the opponents.
msg408533 - (view) Author: wolfgang kuehn (wolfgang-kuehn) Date: 2021-12-14 14:50
I can confirm that setting __PYVENV_LAUNCHER__ to ANY path with prefix  ./venv/Scripts/ does indeed mark the python session as being a virtual environment, no special permissions needed.

However, you will have no tooling support whatsoever (e.g. PyCharms Package Mngt) because __PYVENV_LAUNCHER__ is, after all, non-standard.
msg408578 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2021-12-15 01:22
While the discussion is interesting for some who are trying to work around security policy mechanisms, we can't control what policies people enforce upon themselves that make their machines useless for software development.  Marking as not a bug.

The normal thing to do is to obtain an exemption to policies from the corporate security org managing your machine(s) because you have a business need for it.

If you have specific implementation changes that would make life easier, file specific issues for those to be addressed.  (ex: PEP 582 is pretty specific - and there is a package manager https://pdm.fming.dev/ that supports it)
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90214
2021-12-15 01:22:53gregory.p.smithsetstatus: open -> closed

nosy: + gregory.p.smith
messages: + msg408578

resolution: not a bug
stage: resolved
2021-12-14 14:50:10wolfgang-kuehnsetmessages: + msg408533
2021-12-13 22:32:32steve.dowersetmessages: + msg408486
2021-12-13 22:31:08steve.dowersetmessages: + msg408485
2021-12-13 11:18:25wolfgang-kuehnsetmessages: + msg408443
2021-12-13 10:52:32eryksunsetmessages: + msg408441
2021-12-13 10:05:24wolfgang-kuehnsetmessages: + msg408435
2021-12-13 05:08:56eryksunsetnosy: + eryksun
messages: + msg408424
components: - Windows
2021-12-12 13:30:56eric.smithsetnosy: + paul.moore, tim.golden, zach.ware, steve.dower

components: + Windows
title: Cannot use virtual environment on Windows 10 in cooperate security settings -> Cannot use virtual environment on Windows 10 in corporate security settings
2021-12-12 12:49:06wolfgang-kuehnsettype: behavior
title: Cannot use virtual environment on Windows 10 in cooperate security settingss -> Cannot use virtual environment on Windows 10 in cooperate security settings
2021-12-12 12:45:16wolfgang-kuehncreate