StabilityMatrix icon indicating copy to clipboard operation
StabilityMatrix copied to clipboard

Launch from Stability Matrix fails due to sitecustomize.py / __main__.__file__ AttributeError on Windows

Open telimai opened this issue 7 months ago • 12 comments

What happened?

Environment:

Windows 10 Pro 22H2 / 10.0.19045 Stability Matrix v2.15.7 Stable Diffusion WebUI / AUTOMATIC1111 v1.10.1 Python venv: Python 3.10.11 GPU: NVIDIA GeForce RTX 4070 Ti SUPER NVIDIA driver: 576.80 Torch: 2.1.2+cu121 CUDA available: True

Issue:

When launching Stable Diffusion WebUI from Stability Matrix, or when opening the Python package inspection screen, Stability Matrix sometimes fails while inspecting the venv Python interpreter or running pip list.

The main error is:

AttributeError: module '__main__' has no attribute '__file__'. Did you mean: '__name__'?

Example Stability Matrix error:

ProcessException: pip list failed with code 2
Failed to inspect Python interpreter from provided path at `venv\Scripts\python.exe`

stdout:
[sitecustomize error]: module '__main__' has no attribute '__file__'
[sitecustomize error]: module '__main__' has no attribute '__file__'
[sitecustomize error]: module '__main__' has no attribute '__file__'

stderr:
AttributeError: module '__main__' has no attribute '__file__'. Did you mean: '__name__'?

What I found:

The issue appears to be related to Stability Matrix's sitecustomize.py inside the WebUI venv.

The error happens when Python is invoked in a way where __main__.__file__ does not exist, such as:

python -c "..."

or during commands such as:

python -m pip list

Example test:

venv\Scripts\python.exe -c "import sys; print(sys.executable); print('OK normal')"

This previously failed with:

AttributeError: module '__main__' has no attribute '__file__'

Temporary workaround:

I restored Stability Matrix's original sitecustomize.py, then added a .pth workaround file inside the venv site-packages directory.

The workaround creates a dummy file:

sm_dummy_main.py

and adds this .pth file:

000_sm_main_file_fix.pth

Current contents:

import sys, os, __main__; (not hasattr(__main__, "__file__")) and setattr(__main__, "__file__", (os.path.abspath(sys.argv[0]) if (sys.argv and sys.argv[0] not in ("-c", "-m", "")) else r"<venv>\Lib\site-packages\sm_dummy_main.py"))

After this workaround, these tests pass:

venv\Scripts\python.exe -c "import sys, __main__; print('argv0=', sys.argv[0]); print('__file__=', getattr(__main__, '__file__', None)); print('OK -c')"

Result:

argv0= -c
__file__= <venv>\Lib\site-packages\sm_dummy_main.py
OK -c
venv\Scripts\python.exe -m pip list > nul && echo pip list OK

Result:

pip list OK

Torch test also passes:

venv\Scripts\python.exe -c "import torch; print('torch=', torch.__version__); print('cuda=', torch.version.cuda); print('is_available=', torch.cuda.is_available()); print('device=', torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'NONE')"

Result:

torch= 2.1.2+cu121
cuda= 12.1
is_available= True
device= NVIDIA GeForce RTX 4070 Ti SUPER

Current status:

Stable Diffusion WebUI now launches from Stability Matrix and image generation works.

The WebUI reaches:

Running on local URL: http://127.0.0.1:7860

Image generation also completes successfully:

Total progress: 100%

I also tested another separate Stable Diffusion WebUI package managed by Stability Matrix. It showed the same __main__.__file__ error before the workaround, and launched successfully after applying the same workaround.

Remaining concern:

The workaround is manual and probably should not be necessary.

It looks like Stability Matrix's sitecustomize.py or Python interpreter inspection may need to safely handle cases where __main__.__file__ does not exist.

Expected behavior:

Stability Matrix should be able to inspect the venv Python interpreter, run pip list, and launch WebUI without requiring a manual __main__.__file__ workaround.

Actual behavior:

Without the workaround, Python commands used by Stability Matrix can fail with:

AttributeError: module '__main__' has no attribute '__file__'

Additional notes:

I use ESET, but the same error still occurred even after adding exclusions, so this does not currently look like an antivirus-related issue.

There are also some GradioDeprecationWarning messages from the --sd-webui-ar-plus extension, but those do not appear to be related to this launch failure and do not prevent image generation.

Steps to reproduce

  1. Use a Stable Diffusion WebUI / AUTOMATIC1111 package managed by Stability Matrix.
  2. Open the Python package inspection screen, or launch the WebUI from Stability Matrix.
  3. Stability Matrix tries to inspect the venv Python interpreter or run pip list.
  4. The command can fail when __main__.__file__ does not exist.

Relevant logs

ProcessException: pip list failed with code 2
Failed to inspect Python interpreter from provided path at `venv\Scripts\python.exe`

stdout:
[sitecustomize error]: module '__main__' has no attribute '__file__'
[sitecustomize error]: module '__main__' has no attribute '__file__'
[sitecustomize error]: module '__main__' has no attribute '__file__'

stderr:
AttributeError: module '__main__' has no attribute '__file__'. Did you mean: '__name__'?

Stack trace:
   at StabilityMatrix.Core.Python.UvVenvRunner.PipList()
   at StabilityMatrix.Avalonia.ViewModels.Dialogs.PythonPackagesViewModel.Refresh()
   at StabilityMatrix.Avalonia.ViewModels.Dialogs.PythonPackagesViewModel.Refresh()
   at StabilityMatrix.Avalonia.ViewModels.Dialogs.PythonPackagesViewModel.OnLoadedAsync()

Version

v2.15.7

What Operating System are you using?

Windows

telimai avatar Apr 27 '26 02:04 telimai

Are you running ESET security software?

NeuralFault avatar Apr 27 '26 16:04 NeuralFault

Yes. I am running ESET NOD32 Antivirus.

I also tried adding ESET detection exclusions for the Stability Matrix / Stable Diffusion WebUI folders, but the issue still occurred.

I tested another separate Stable Diffusion WebUI package managed by Stability Matrix, and it had the same error when Stability Matrix tried to inspect the Python interpreter / run pip list:

module 'main' has no attribute 'file'

After adding the same .pth workaround to the package's venv site-packages folder, pip list worked, Stability Matrix launched the WebUI successfully, and image generation completed successfully.

So ESET is present on my system, but adding ESET exclusions alone did not seem to fix it. The workaround fixed both WebUI installations.

telimai avatar Apr 27 '26 17:04 telimai

Every other user I've seen report it here and on the discord server have stated it worked after excluding the SM folder from HIPS Deep Behavioral Inspection. Excluding from "Scans > Exclusions > Performance Exclusions" and "Scans > Exclusions > Detection Exclusion" may also be needed if not already set.

ESET needs to fix their detection of this false positive interfering with the standalone python distributions in SM on their end. Antivirus and other security software have been known to cause issues with Stability Matrix and it's functions, McAfee historically and Windows Defender and other AVs when using Zluda packages for example, ending up in breakage of the application functions due to false positive detection/heuristics.

NeuralFault avatar Apr 27 '26 22:04 NeuralFault

Additional update:

I tested this again with a fresh A1111 package install.

Before retrying, I added ESET exclusions for the Stability Matrix / package directory, then performed a fresh install test again.

Even with the ESET exclusions in place, the install still failed at the same stage:

Failed to inspect Python interpreter from provided path at `venv\Scripts\python.exe`

AttributeError: module '__main__' has no attribute '__file__'

So in my case, ESET exclusions alone did not fix the issue. I am not saying ESET is completely unrelated, but this specific error was still reproducible after adding exclusions.

After the failed install, I kept the partially created package folder and manually added a .pth workaround inside the venv:

venv\Lib\site-packages\000_sm_main_file_fix.pth

The file sets a dummy __main__.__file__ when it is missing.

After that:

python -c test: OK
pip list: OK

At first, pip was missing in the interrupted venv, so I restored it with:

python -m ensurepip --upgrade

Then pip worked normally.

I also confirmed this with Python 3.10.11:

Python 3.10.11
A1111 Version: v1.10.1
Commit hash: 82a973c04367123ae98bd9abdf80d9eda9b910e2

There was a later separate dependency issue while installing CLIP:

ModuleNotFoundError: No module named 'pkg_resources'

After resolving the Python package dependencies manually, I confirmed:

numpy=1.26.4
torch=2.1.2+cu121
cuda=True
clip OK

Then A1111 successfully launched.

First launch worked without xformers:

Running on local URL: http://127.0.0.1:7860
Model loaded successfully
Generation completed successfully

Then I enabled xformers and launched again:

Launching Web UI with arguments: --xformers --skip-python-version-check
Applying attention optimization: xformers... done.
Model loaded successfully

So my current conclusion is:

  1. The original failure is reproducible even after adding ESET exclusions.
  2. The failure happens when Stability Matrix tries to inspect/query the venv Python.
  3. A workaround that provides __main__.__file__ via a .pth file allows the venv Python inspection to pass.
  4. After restoring pip and fixing the later CLIP/package dependency issue, A1111 can launch normally.
  5. xformers also works after launch.

This suggests that the main install blocker is probably related to something expecting __main__.__file__ to exist during Python inspection, rather than being caused only by antivirus blocking.

telimai avatar Apr 28 '26 03:04 telimai

I tested this again with a fresh A1111 package install.

Before retrying, I added ESET exclusions for the Stability Matrix / package directory, then performed a fresh install test again.

Even with the ESET exclusions in place, the install still failed at the same stage:

In my test, the python.exe causing errors due to interference with ESET was located in the StabilityMatrix's Data\Assets\ folder (not Packages folder). The python.exe in each package's venv acted as a kind of launcher that invoked the one in the Data\Assets\ folder. Therefore, the python.exe that needed to be excluded from ESET's monitoring was the one in the Data\Assets\ folder. This included both the version used by StabilityMatrix itself and those corresponding to each package (installed as needed).

ex.:This screenshot shows the running processes while the WebUI is active. Image

Bit-Nibble avatar Apr 29 '26 11:04 Bit-Nibble

I tested this again with a fresh A1111 package install.

Before retrying, I added ESET exclusions for both Detection exclusions and Performance exclusions.

In my environment, I had already added an ESET exclusion for the entire Stability Matrix installation directory recursively, e.g. INSTALL_DIR\*.

I also added exclusions for the package directory and the A1111 package directory, but the fresh install still failed at the same stage with:

Failed to inspect Python interpreter from provided path at venv\Scripts\python.exe

So in my case, the ESET exclusions alone did not fix the issue.

After the failed install, I left the partially created package folder in place and applied the .pth workaround inside the package venv.

Then I confirmed that python -c no longer failed with the __main__.__file__ error.

At that point, pip was not installed in the venv yet, so I ran:

venv\Scripts\python.exe -m ensurepip --upgrade

After that, pip list worked correctly.

Then I launched the package again from Stability Matrix.

This time, the WebUI proceeded further, installed the required packages, downloaded the default SD 1.5 model, and successfully started.

I was also able to generate an image.

So the workaround did solve the failed install in my environment, but the important point is that ESET exclusions alone were not enough in this case.

telimai avatar Apr 29 '26 12:04 telimai

Before retrying, I added ESET exclusions for both Detection exclusions and Performance exclusions.

Hmm, I wonder what's different here. It's already excluded from ESET's HIPS Deep Behavioral Inspection too, isn't it?

Bit-Nibble avatar Apr 29 '26 13:04 Bit-Nibble

Yes, that’s exactly what confused me too.

In my environment, I had already added an ESET exclusion for the entire Stability Matrix installation directory recursively, e.g. INSTALL_DIR\*.

However, that broad folder exclusion alone did not seem to be enough for this specific issue.

What made the reproducible difference was explicitly excluding the embedded/Base Python executable used by Stability Matrix:

INSTALL_DIR\Data\Assets\Python\[cpython folder]\python.exe

After rebooting, I tested this several times:

  • With that embedded/Base Python python.exe explicitly excluded: torch import works, CUDA is detected, WebUI starts, and image generation works.
  • If I remove only that embedded/Base Python python.exe exclusion and reboot: the sitecustomize / __file__ error comes back.
  • If I add it back and reboot: it works again.

I also tested removing some other exclusions. The WebUI venv python.exe, StabilityMatrix.exe, and uv.exe did not seem to be required for runtime startup in my case.

So it looks like ESET’s Deep Behavioral Inspection may not be treating the broad recursive folder exclusion the same way as an explicit executable exclusion, at least on my machine. I’m not sure why, but the behavior was reproducible.

telimai avatar Apr 29 '26 23:04 telimai

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 29 '26 02:06 github-actions[bot]

This may also be related to #1589, which appears to describe the same underlying issue.

misidia-unagi avatar Jul 01 '26 23:07 misidia-unagi

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 01 '26 01:09 github-actions[bot]

This issue was closed because it has been stale for 7 days with no activity.

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