Repository navigation
Best practices for isolating a Windows Service entirely within a Virtual Environment (.venv)? #2734
Replies: 2 comments 3 replies
|
The problem is that services are global in nature - ie, looking up HKCU doesn't make sense. Most Python installations install themselves under a specific user, and a venv is intended to be even more isolated than that - ie, a "global venv" is a kind of contradiction. That said though, I wouldn't object to changing behaviour based on that (or other) environment variable, but I believe this hasn't come up yet because it kinda doesn't make sense when thinking about the local "system" user etc. |
|
Short version:
With the default install location, step 2 goes wrong inside a venv. Putting What the registry is used for
Why the default does not work in a venv
A setup that keeps everything inside the venv
This isolates your packages, not Python itself. The venv still depends on its base interpreter for the standard library and How I checked this Python 3.13.12, pywin32 312, a fresh
The About your second question @mhammond's reply above makes it clear that running services from a venv is not the recommended setup, so treat this as a workaround. If you find time for a PR, the change would be in Python, not C++. In |
Uh oh!
There was an error while loading. Please reload this page.
Hi everyone,
I am currently working on deploying a web API (FastAPI) as a Windows Service using
win32serviceutil. To ensure clean deployments, my goal is to keep the service entirely isolated within its own Virtual Environment (.venv), without relying on or interfering with any global Python installation on the host server.From my understanding and testing, it seems that the C++ wrapper (
pythonservice.exe) natively looks up the global Windows Registry (HKEY_LOCAL_MACHINE) to locate the Python DLLs, which makes strict.venvisolation tricky, especially if an identical global Python version is present on the system.My questions are:
pythonservice.exeto strictly respect the.venvboundaries (for instance, by prioritizing local paths or detecting theVIRTUAL_ENVenvironment variable)?I would love to contribute! If a
.venv-first isolation feature is something that would benefit the community, I would be more than happy to dive into the C++ source code, try to implement a fallback mechanism (e.g., checking for virtual environment flags before querying the global registry), and submit a Pull Request. Alternatively, if a workaround already exists, I'd be glad to help document it.Thanks in advance for your time and guidance!
All reactions