KEMBAR78
`os.__file__` not always set · Issue #1943 · microsoft/debugpy · GitHub
Skip to content

os.__file__ not always set #1943

@timrid

Description

@timrid

Environment data

  • debugpy version: 1.8.16
  • OS and version: Windows 11
  • Python version: 3.13
  • Using VS Code or Visual Studio: VSCode
  • briefcase version: 0.3.24

Actual behavior

I am trying to run

import debugpy
debugpy.listen(5678)

in an application that is bundled with briefcase but I get the following error message:

Traceback (most recent call last):
  File "asyncio\events.py", line 89, in _run
  File "...\build\helloworld\windows\app\src\app_packages\toga\app.py", line 655, in _startup
    self.startup()
    ~~~~~~~~~~~~^^
  File "...\build\helloworld\windows\app\src\app\helloworld\app.py", line 218, in startup
    debugpy.listen(5678)
    ~~~~~~~~~~~~~~^^^^^^
  File "...\build\helloworld\windows\app\src\app_packages\debugpy\public_api.py", line 44, in wrapper
    from debugpy.server import api
  File "...\build\helloworld\windows\app\src\app_packages\debugpy\server\__init__.py", line 7, in <module>
    import debugpy._vendored.force_pydevd  # noqa
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\build\helloworld\windows\app\src\app_packages\debugpy\_vendored\force_pydevd.py", line 44, in <module>
    preimport('pydevd', [
    ~~~~~~~~~^^^^^^^^^^^^
        '_pydev_bundle',
        ^^^^^^^^^^^^^^^^
    ...<5 lines>...
        'pydevd',
        ^^^^^^^^^
    ])
    ^^
  File "...\build\helloworld\windows\app\src\app_packages\debugpy\_vendored\__init__.py", line 126, in preimport
    import_module(name)
    ~~~~~~~~~~~~~^^^^^^
  File "importlib\__init__.py", line 88, in import_module
  File "...\build\helloworld\windows\app\src\app_packages\debugpy\_vendored\pydevd\pydevd.py", line 39, in <module>
    import pydevd_file_utils
  File "...\build\helloworld\windows\app\src\app_packages\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 98, in <module>
    _library_dir = _get_library_dir()
  File "...\build\helloworld\windows\app\src\app_packages\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 91, in _get_library_dir
    library_dir = os.path.dirname(os.__file__)
                                  ^^^^^^^^^^^
AttributeError: module 'os' has no attribute '__file__'. Did you mean: '__all__'?

Expected behavior

No error should be thrown.

Steps to reproduce:

  1. Follow the beeware tutorial to create an windows application
  2. Add requires = ["debugpy"] to pyproject.toml
  3. Add
import debugpy
debugpy.listen(5678)
  1. Start application with briefcase run -r -u

Investigations

Since Python 3.11 there are frozen imports for speedup eg. for os. With frozen import the bytecode is statically allocated by the interpreter, so that it is hard to tell the correct location of os.py for the interpreter. There has been an PR python/cpython#28656 that tries setting os.__file__ even with frozen imports, but it fails in some situations.

In my case that error is occuring, because briefcase is using an embedded isolated python instance (initialized with PyConfig_InitIsolatedConfig()) and does not set PyConfig.stdlib_dir explicitly. See here in the briefcase template This results in a configuration, that os.__file__ is not set. If I add modify the template, so that PyConfig.stdlib_dir is set to some location and then os.__file__ is also set. But this is not the right bugfix, because I have no vaild path for PyConfig.stdlib_dir.

As a consequence, one must assume that os.__file__ is not always available and use hasattr(os, '__file__') to check if it is available.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions