question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Some loaded extension modules have no __file__ value, causing e.g. runtime issue with WinRT packages (bleak / bleak_winrt)

See original GitHub issue

Hello,

I have an issue when running a WinRT-dependent application built with Nuitka. This issue was already reported as #1184 but was closed for inactivity.

Environment

  • I installed Python 3.9 using the official Windows 64bit installer from python.org.
  • Nuitka was installed using pip in a virtualenv.

Versions:

(__env__) C:\Users\vm0\Desktop\issue-nuitka>python -m nuitka --version
0.6.19.6
Commercial: None
Python: 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)]
Flavor: Unknown
Executable: C:\Users\vm0\Desktop\issue-nuitka\__env__\Scripts\python.exe
OS: Windows
Arch: x86_64
WindowsRelease: 10

I also tried on another Windows 10 machine + Python 3.10 but I do not have access to it right now. 😃

Step to reproduce

Here is a minimal example to reproduce.

requirements.txt:

bleak==0.14.2
bleak-winrt==1.1.1
Nuitka==0.6.19.6

test.py:

#!/usr/bin/env python3

import bleak

make.bat:

nuitka ^
    --follow-imports ^
    --assume-yes-for-downloads ^
    --standalone ^
    test.py
  • install dependencies (in a venv): pip install -r requirements.txt
  • build the test: .\make.bat
  • run the test: .\test.dist\test.exe

Output:

(__env__) C:\Users\vm0\Desktop\issue-nuitka>test.dist\test.exe
Traceback (most recent call last):
  File "C:\Users\vm0\Desktop\ISSUE-~1\TEST~1.DIS\test.py", line 3, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "C:\Users\vm0\Desktop\ISSUE-~1\TEST~1.DIS\bleak\__init__.py", line 78, in <module bleak>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "C:\Users\vm0\Desktop\ISSUE-~1\TEST~1.DIS\bleak\backends\winrt\scanner.py", line 7, in <module bleak.backends.winrt.scanner>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "C:\Users\vm0\Desktop\ISSUE-~1\TEST~1.DIS\bleak_winrt\windows\devices\bluetooth\__init__.py", line 177, in <module bleak_winrt.windows.devices.bluetooth>
AttributeError: 'NoneType' object has no attribute 'BluetoothAdapter'

Workaround

To workaround the issue, I patched the .\__env__\Lib\site-packages\bleak_winrt\__init__.py file from:

# WARNING: Please don't edit this file. It was generated by Python/WinRT v1.0.0-alpha.6

from . import _winrt

def _import_ns_module(ns):
    import importlib.machinery
    import importlib.util

    try:
        module_name = "_bleak_winrt_" + ns.replace('.', '_')

        loader = importlib.machinery.ExtensionFileLoader(module_name, _winrt.__file__)
        spec = importlib.util.spec_from_loader(module_name, loader)
        module = importlib.util.module_from_spec(spec)
        loader.exec_module(module)
        return module
    except Exception:
        return None

to:

# WARNING: Please don't edit this file. It was generated by Python/WinRT v1.0.0-alpha.6

from . import _winrt

import os
_winrt.__file__ = os.path.join(os.path.dirname(__file__), "_winrt.pyd")

def _import_ns_module(ns):
    import importlib.machinery
    import importlib.util

    try:
        module_name = "_bleak_winrt_" + ns.replace('.', '_')

        loader = importlib.machinery.ExtensionFileLoader(module_name, _winrt.__file__)
        spec = importlib.util.spec_from_loader(module_name, loader)
        module = importlib.util.module_from_spec(spec)
        loader.exec_module(module)
        return module
    except Exception:
        return None

The issue was caused by the missing __file__ attribute on the _winrt object.


Do not hesitate if you need more info or if you want I test things… 😃

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kayhayencommented, Feb 7, 2022

As an aside, I would like to thank you for attempting to track this down yourself.

0reactions
davidleoncommented, Mar 15, 2022

cefpython3 also has problems with missing file. currently the build fails at File “helpers.pyx”, line 60, in cefpython_py38.GetModuleDirectory. the code in cefpython3 is path = os.path.dirname(os.path.realpath(file)).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does this bleak module give such an error - Stack Overflow
It's a bug. This seems to be a bug in either the Bleak or PyWinRT module. There is an open issue for PyWinRT...
Read more >
conda-forge - :: Anaconda.org
bleak-winrt, 1.2.0, MIT, X, Python WinRT bindings for Bleak ... Package providing tools to solve BVP (Boundary Value Problem) and IBVP.
Read more >
Changelog — bleak 0.20.0a1 documentation
Fixed performance issues in BlueZ backend caused by calling “GetManagedObjects” each time a BleakScanner scans or BleakClient is connected. Fixes #500.
Read more >
Frequently-asked questions about C++/WinRT - Microsoft Learn
Do you have any recommendations for decreasing the code size for C++/WinRT binaries? ... For example, if you're using a range-based for on...
Read more >
Simple Index
100-working-free-clash-royal-gems-hack-generator-no-verify ... a-python-package ... abbreviation-uhh-what-is-word-for-tearing-apart-an-abbreviation
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found