python module dlls

后端 未结 4 1246

Is there a way to make a python module load a dll in my application directory rather than the version that came with the python installation, without making changes to the pytho

4条回答
  •  一生所求
    2021-02-12 11:43

    The answer with modifying os.environ['PATH'] is right but it didn't work for me because I use python 3.9. Still I was getting an error:

    ImportError: DLL load failed while importing module X: The specified module could not be found.

    Turned out since version python 3.8 they added a mechanism to do this more securely. Read documentation on os.add_dll_directory https://docs.python.org/3/library/os.html#os.add_dll_directory

    Specifically see python 3.8 what's new:

    DLL dependencies for extension modules and DLLs loaded with ctypes on Windows are now resolved more securely. Only the system paths, the directory containing the DLL or PYD file, and directories added with add_dll_directory() are searched for load-time dependencies. 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 your application relies on these mechanisms, you should check for add_dll_directory() and if it exists, use it to add your DLLs directory while loading your library.

    So now this is the new way to make it work in python 3.8 and later:

    import os
    os.add_dll_directory('my-app-dir')
    

    Again, the old way is still correct and you will have to use it in python 3.7 and older:

    import os
    os.environ['PATH'] = 'my-app-dir' + os.pathsep + os.environ['PATH']
    

    After that my module with a dll dependency has been successfully loaded.

提交回复
热议问题