This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Python Windows DLL search paths
Type: behavior Stage: resolved
Components: Windows Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, jmoguill2, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2021-02-09 02:45 by jmoguill2, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg386688 - (view) Author: Jeff Moguillansky (jmoguill2) Date: 2021-02-09 02:45
Hi,
What's the correct way to set the DLL search path when running a python script?

It seems that it doesn't search the directories specified in PATH environment variable. 

FYI: For debugging the DLL loading issues, I'm using "Process Monitor" from sysinternals:
https://docs.microsoft.com/en-us/sysinternals/downloads/procmon,
but please share any tips if you have a better approach.

Also, the Python error message is not very informative:
when loading a python module (built using Cython), if it fails to load a specific DLL, it says 
"import module <module name> failed, DLL not found" but it doesn't say the name of the actual DLL that is not found.
It would be helpful if it listed the actual name of the DLL that it cannot find.
msg386691 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-02-09 04:29
> What's the correct way to set the DLL search path when running a python script?

If possible, the simplest approach is to put dependent DLLs in the same directory as the extension module. 

In 3.8+, the search path for the dependent DLLs of a normally imported extension module includes the following directories:

    * the loaded extension module's directory
    * the application directory (e.g. that of python.exe)
    * the user DLL search directories that get added by 
      SetDllDirectory() and AddDllDirectory(), such as with 
      os.add_dll_directory()
    * %SystemRoot%\System32

Note that the above list does not include the current working directory or %PATH% directories.

> It would be helpful if it listed the actual name of 
> the DLL that it cannot find.

WinAPI LoadLibraryExW() doesn't have an out parameter to get the missing DLL or procedure name that caused the call to fail. All we have is the error code to report, such as ERROR_MOD_NOT_FOUND (126) and ERROR_PROC_NOT_FOUND (127). Using a debugger, you can see the name of the missing DLL or procedure if loader snaps are enabled for the application.
msg386736 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2021-02-09 18:16
Eryk answered the question sufficiently, here's the link to the add_dll_directory docs: https://docs.python.org/library/os.html#os.add_dll_directory
msg386738 - (view) Author: Jeff Moguillansky (jmoguill2) Date: 2021-02-09 18:26
Thanks for the feedback

On Mon, Feb 8, 2021, 8:29 PM Eryk Sun <report@bugs.python.org> wrote:

>
> Eryk Sun <eryksun@gmail.com> added the comment:
>
> > What's the correct way to set the DLL search path when running a python
> script?
>
> If possible, the simplest approach is to put dependent DLLs in the same
> directory as the extension module.
>
> In 3.8+, the search path for the dependent DLLs of a normally imported
> extension module includes the following directories:
>
>     * the loaded extension module's directory
>     * the application directory (e.g. that of python.exe)
>     * the user DLL search directories that get added by
>       SetDllDirectory() and AddDllDirectory(), such as with
>       os.add_dll_directory()
>     * %SystemRoot%\System32
>
> Note that the above list does not include the current working directory or
> %PATH% directories.
>
> > It would be helpful if it listed the actual name of
> > the DLL that it cannot find.
>
> WinAPI LoadLibraryExW() doesn't have an out parameter to get the missing
> DLL or procedure name that caused the call to fail. All we have is the
> error code to report, such as ERROR_MOD_NOT_FOUND (126) and
> ERROR_PROC_NOT_FOUND (127). Using a debugger, you can see the name of the
> missing DLL or procedure if loader snaps are enabled for the application.
>
> ----------
> nosy: +eryksun
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue43173>
> _______________________________________
>
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87339
2021-02-09 18:26:07jmoguill2setmessages: + msg386738
2021-02-09 18:16:16steve.dowersetstatus: open -> closed
resolution: not a bug
messages: + msg386736

stage: resolved
2021-02-09 04:29:25eryksunsetnosy: + eryksun
messages: + msg386691
2021-02-09 02:45:38jmoguill2create