sizmailov / pybind11-stubgen

Generate stubs for python modules
Other
238 stars 49 forks source link

Need support for external dll path with Python3.8+ #88

Closed hellokenlee closed 1 year ago

hellokenlee commented 1 year ago

Accroding to https://docs.python.org/3/library/os.html#os.add_dll_directory and https://docs.python.org/3.8/whatsnew/3.8.html#bpo-36085-whatsnew: The PATH variable is no longer used after Python3.8.

Specifically, PATH and the current working directory are no longer used, and modifications to these will no longer have any effect on normal DLL resolution.

If the pyd generate by pybind11 has external dependent dynamic library, the pybind11-stubgen cannot work correctly:

PS H:\> pybind11-stubgen.exe nene
Traceback (most recent call last):
  File "C:\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Python310\Scripts\pybind11-stubgen.exe\__main__.py", line 7, in <module>
  File "C:\Python310\lib\site-packages\pybind11_stubgen\__init__.py", line 1101, in main
    _module = ModuleStubsGenerator(_module_name)
  File "C:\Python310\lib\site-packages\pybind11_stubgen\__init__.py", line 818, in __init__
    self.module = importlib.import_module(module_or_module_name)
  File "C:\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'nene'

And there is no way to solve this. ( In Python3.7 we can simply modify %PATH% environment variable before we call pybin11-stubgen. ) Maybe we can consider adding a commandline argument to let user specify the external dll paths.

sizmailov commented 1 year ago

Hi! Thanks for the question!

I don't really know your use case, but I think this issue should not be addressed in a stubgen. If you cannot import the library, you can't inspect it and generate the stubs. It's the library author's responsibility to make it importable. How the regular users of the library are supposed to use it? Do they have to alter the dll path too? That just doesn't sound right.

As a workaround, you can stop using pybind11-stubgen as a utility and write around the main() function with the calls of your choice.

hellokenlee commented 1 year ago

Thansk for answering. I am using the workaround you proposed: using pybind11_stubgen as a lib and call main() manualy, that works fine for me. Thank you agian 🍡