Skip to content

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

@telimai

Description

@telimai

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions