StabilityMatrix icon indicating copy to clipboard operation
StabilityMatrix copied to clipboard

Fresh package install fails with "Failed to inspect Python interpreter" and "__main__.__file__" error

Open yyy1026 opened this issue 8 months ago • 8 comments

What happened?

Summary

On a fresh Stability Matrix installation, package installation fails during venv setup with:

  • Failed to inspect Python interpreter from provided path at venv\Scripts\python.exe
  • AttributeError: module '__main__' has no attribute '__file__'. Did you mean: '__name__'?

This is reproducible even with:

  • a completely different install folder
  • a fresh Data folder
  • Stability Matrix v2.15.5
  • multiple packages (reForge / ComfyUI / kohya_ss)

This does not look like a broken old package environment. It seems to happen during Stability Matrix's venv / Python setup flow itself.


Environment

  • OS: Windows 10/11 (64-bit)
  • GPU: NVIDIA GeForce RTX 4080
  • Stability Matrix versions tested:
    • current version
    • v2.15.5
  • Reproduced on a fresh install in a different folder

Affected packages

I saw similar startup/install failures across multiple packages:

  • Stable Diffusion WebUI reForge
  • ComfyUI
  • kohya_ss

Steps to reproduce

  1. Install Stability Matrix in a completely new folder
  2. Use a fresh Data directory
  3. Try to install reForge (also seen with other packages)
  4. Installation fails during Python/venv inspection

Expected behavior

The package should create its venv and complete installation normally.


Actual behavior

Installation fails with an error like this:

Cloning into 'J:\Downloads\StabilityMatrix-win-x64 (1)\Data\Packages\reforge'...
Updating files: 100% (1699/1699), done.
Download Complete
Error: StabilityMatrix.Core.Exceptions.ProcessException: pip install failed with code 2: 'error: Failed to inspect Python interpreter from provided path at `venv\Scripts\python.exe`
  Caused by: Querying Python at `J:\Downloads\StabilityMatrix-win-x64 (1)\Data\Packages\reforge\venv\Scripts\python.exe` failed with exit status exit code: 1

[stderr]
AttributeError: module '__main__' has no attribute '__file__'. Did you mean: '__name__'?
'
   at StabilityMatrix.Core.Python.UvVenvRunner.PipInstall(ProcessArgs args, Action`1 outputDataReceived)
   at StabilityMatrix.Core.Models.Packages.BaseGitPackage.SetupVenvPure(String installedPackagePath, String venvName, Boolean forceRecreate, Action`1 onConsoleOutput, Nullable`1 pythonVersion)
   at StabilityMatrix.Core.Models.Packages.SDWebForge.InstallPackage(String installLocation, InstalledPackage installedPackage, InstallPackageOptions options, IProgress`1 progress, Action`1 onConsoleOutput, CancellationToken cancellationToken)
   at StabilityMatrix.Core.Models.PackageModification.InstallPackageStep.ExecuteAsync(IProgress`1 progress, CancellationToken cancellationToken)
   at StabilityMatrix.Core.Models.PackageModification.PackageModificationRunner.ExecuteSteps(IEnumerable`1 steps)

### Steps to reproduce

_No response_

### Relevant logs

```shell

Version

v2.15.5 & v2.15.6

What Operating System are you using?

Windows

yyy1026 avatar Apr 03 '26 07:04 yyy1026

I can reproduce this on a fresh Windows install of Stability Matrix v2.15.7 when installing ComfyUI.

Environment

  • OS: Windows
  • Stability Matrix: 2.15.7
  • Package: ComfyUI
  • ComfyUI release selected in Stability Matrix: 0.19.3
  • Install location: I:\SM

Error shown by Stability Matrix Failed to inspect Python interpreter from provided path at venv\Scripts\python.exe Caused by: AttributeError: module 'main' has no attribute 'file'

What I verified

  • This does not seem to be caused by a global Windows Python installation.
  • where python only showed the Microsoft Store alias.
  • py launcher was not installed.
  • uv was not installed globally.
  • No PYTHONHOME, PYTHONPATH, VIRTUAL_ENV, UV_*, or CONDA_* environment variables were set.
  • No sitecustomize.py, usercustomize.py, or .pth files were found in the usual user Python locations.

Things that did NOT help

  • Clearing the failed package folder and uv cache.
  • Reinstalling Stability Matrix fresh.
  • Changing/installing to a different drive.
  • Setting environment variables inside Stability Matrix settings.
  • Selecting Python 3.10.11 in ComfyUI package Advanced Options.

Important finding The Python bundled inside Stability Matrix works correctly when run directly.

This works:

$base = "I:\SM\Data\Assets\Python\cpython-3.12.12-windows-x86_64-none\python.exe"
& $base -V
& $base -c "import sys; print(sys.executable); print(sys.version)"

Creating the venv manually also works:

$py  = "I:\SM\Data\Assets\Python\cpython-3.12.12-windows-x86_64-none\python.exe"
$dst = "I:\SM\Data\Packages\ComfyUI"

Remove-Item "$dst\venv" -Recurse -Force -ErrorAction SilentlyContinue
& $py -m venv "$dst\venv"

& "$dst\venv\Scripts\python.exe" -V
& "$dst\venv\Scripts\python.exe" -c "import sys; print(sys.executable); print(sys.version)"

Both commands succeed.

Then I was also able to install requirements manually:

& "$dst\venv\Scripts\python.exe" -m ensurepip --upgrade
& "$dst\venv\Scripts\python.exe" -m pip install --upgrade pip setuptools wheel
& "$dst\venv\Scripts\python.exe" -m pip install -r "$dst\requirements.txt"

ComfyUI then launched successfully from that manually-created venv.

Additional notes

  • In the ComfyUI installer UI, I selected Python 3.10.11 in Advanced Options, but the install still failed with the same __main__.__file__ error.
  • So changing the Python version in package Advanced Options did not resolve the problem.
  • In my case, the workaround only succeeded when I manually created the venv using the embedded Stability Matrix Python at: I:\SM\Data\Assets\Python\cpython-3.12.12-windows-x86_64-none\python.exe

Conclusion This strongly suggests the problem is not Python itself and not ComfyUI itself, but Stability Matrix’s automatic package setup flow, specifically around uv / venv interpreter inspection.

dimosforaionly avatar Apr 20 '26 21:04 dimosforaionly

Please see https://github.com/LykosAI/StabilityMatrix/issues/1602#issuecomment-4251887595 for more details - likely issue is your ESET security interfering

mohnjiles avatar Apr 20 '26 21:04 mohnjiles

There's a new version in beta, but it's only available to donationists. Ha, I'm not sure if it has fixes, but someone decided to make some extra money, apparently. For update the packages, I simply deleted the file sitecustomize.py from the folder venv\Lib\site-packages, and everything was updated. There is an alternative https://pinokio.co, but I don't want to migrate and move a bunch of packages.

kurttu4 avatar Apr 27 '26 17:04 kurttu4

This issue is stale because it has been open 60 days with no activity. Remove the stale label or comment, else this will be closed in 7 days.

github-actions[bot] avatar Jun 27 '26 02:06 github-actions[bot]

I can confirm this issue is still reproducible.

I reproduced it on:

  • Stability Matrix v2.16.1
  • Windows 11
  • Fresh installation
  • Both portable and non-portable mode
  • Both C: and E: drives
  • ESET temporarily disabled (no change)

Additional findings:

  • The bundled Python works normally.
  • The Python inside the newly created virtual environment fails with:
AttributeError: module '__main__' has no attribute '__file__'

Running Python with -S works correctly.

I also found that renaming:

venv\Lib\site-packages\sitecustomize.py

to

sitecustomize.py.bak

allows:

uv pip list --python venv\Scripts\python.exe

to work correctly, suggesting that sitecustomize.py is involved.

I also tested both Automatic1111 and Stable Diffusion WebUI Forge. Both fail with the same error during a fresh installation.

After manually working around this issue, I was able to continue the installation and successfully launch Automatic1111.

So this issue still appears to affect v2.16.1.

misidia-unagi avatar Jun 30 '26 14:06 misidia-unagi

Thank you for confirming this.

This matches my findings as well. In my case, multiple packages created by Stability Matrix showed the same error, and renaming or removing:

venv\Lib\site-packages\sitecustomize.py

also resolved the issue.

The fact that Python works with -S strongly suggests that the problem is caused by sitecustomize.py being imported during Python startup. It may be assuming that main.file always exists, but this is not true for some invocation modes, including uv-related commands.

A possible fix might be to guard that access with getattr(), for example:

getattr(sys.modules.get("main"), "file", None)

or avoid relying on main.file during startup.

This does not appear to be related to antivirus software, drive location, portable mode, or a specific WebUI package.

yyy1026 avatar Jul 03 '26 02:07 yyy1026

This issue is stale because it has been open 60 days with no activity. Remove the stale label or comment, else this will be closed in 7 days.

github-actions[bot] avatar Sep 02 '26 01:09 github-actions[bot]

Hi there. I got this issue too, with windows 11 pro latest version and Eset, and SM 2.16.3. Running on rtx 3090 24GB, latest nvidia studio drivers, and eset antivirus ( yet I tried disabling it and nothing changed).

Hey, I managed to overcome the error in updating packages. First, I updated to last stability Matrix. And yet the upgrade error was still there! ( comfyui package in this case )

I sent the log to claude, and after some tests he gave me a few lines to paste in the cmd prompt, after that ComfyUi updated itself like a charm! The I asked claude to write a bat to fix the problem for any packages.

Here's what Claude has to say about:

Package install/update fails: "Failed to inspect Python interpreter" / "module 'main' has no attribute 'file'" (v2.16.5, Windows 11 x64)

Same issue as #1589, #1596, #1605, #1620. Still happening on 2.16.5: updating ComfyUI and installing Forge Neo both fail at the uv/pip step.

Diagnosis: running the package venv's python.exe with -c "print('ok')" fails with [sitecustomize error] and the __file__ AttributeError. With -S (skips site/sitecustomize) it prints "ok". -I doesn't help, and no PYTHON* env vars are set. So the problem is the sitecustomize.py in the venv, which doesn't handle __main__.__file__ being missing under python -c (or something interfering with it) [antivirus: ___ / none].

Workaround that fixed it for me: create venv\Lib\site-packages\000_sm_main_file_fix.pth containing this single line:

import main; setattr(main, "file", getattr(main, "file", "sm_dummy_main.py"))

(.pth files run before sitecustomize.) After that python.exe -c "print('ok')" works and [the ComfyUI update completes]. It has to be repeated for each package venv.

Suggested fix: sitecustomize.py should use getattr(main, "file", None) instead of accessing it directly.

Claude created a bat file I renamed "fixstabilityupdatingissues.bat" that fixes the venv environment in all the installed packages : You just have to change the folder path in the bat.

@echo off for /d %%D in ("E:\stability-matrix\Data\Packages*") do call :fix "%%~fD" pause exit /b

:fix if not exist "%~1\venv\Lib\site-packages" exit /b echo import main; setattr(main, "file", getattr(main, "file", "sm_dummy_main.py"))> "%~1\venv\Lib\site-packages\000_sm_main_file_fix.pth" echo Fatto: %~1 exit /b

Cyboroz avatar Sep 06 '26 17:09 Cyboroz