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: ImportError: DLL load failed while importing binascii: %1 is not a valid Win32 application.
Type: behavior Stage: resolved
Components: Library (Lib), Windows Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: MatthewJ, eryksun, jkloth, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

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

Messages (9)
msg416573 - (view) Author: Matthew (MatthewJ) Date: 2022-04-02 18:09
When I try importing the default library "binascii" using the command "import binascii", the result is the following error (also in the title):

ImportError: DLL load failed while importing binascii: %1 is not a valid Win32 application.

I've reinstalled Python and tried using the 32 bit version of Python, along with Python 3.9, but this error still persists.

Any help would be appreciated!
msg416584 - (view) Author: Jeremy Kloth (jkloth) * Date: 2022-04-02 20:02
This error will occur when there is a 64-bit/32-bit conflict.  Normally, Python extension modules are installed in architecture dependent locations, however user-installed modules (pip install) can share a path referred to as "user site".

A quick check from the command-line can give you its location:

  py -m site

A scan of the paths listed as USER_BASE and USER_SITE might reveal a binascii.pyd  which would be shadowing the normally built-in module.

Another source of conflict would be a PYTHONPATH environment variable, if set.
msg416626 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2022-04-03 15:27
The user site packages directory is architecture specific starting with 3.10, e.g. "%AppData%\Python\Python310-32\site-packages". The PYTHONPATH environment variable is shared by all versions. 

However, I don't understand how the binascii module could be a problem because it's built into the interpreter DLL itself, i.e. it's in sys.builtin_module_names. There is no binascii.pyd, and, even if there were, it would be ignored.
msg416629 - (view) Author: Jeremy Kloth (jkloth) * Date: 2022-04-03 16:09
Well, to really see where things are going wrong, there is always the verbose option when launching Python:

  py -v -c "import binascii"

This will output a lot of information but should help pin down what is exactly being imported when the error occurs.  If the above doesn't give enough of a clue as to the offending file, try '-vv' instead of '-v'.  This increases the amount of debugging output, but does show each filename attempted for each particular import.  You will need to scroll back quite awhile to get to the error location (past the cleanup messages).
msg416660 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2022-04-04 09:45
It could also be something on your PATH interfering, as that particular ImportError could be pretty deep in the load chain.

If none of the built-in options narrow it down enough, try https://pypi.org/project/dlltracer/ as well. Its output takes a bit of interpretation, but it'll definitely get us closer.
msg416702 - (view) Author: Matthew (MatthewJ) Date: 2022-04-04 22:43
Hello,

Thanks for all the help people have given me! I've found the solution to my problem. The Environment Variable was set below every other, leading to a different Python interpreter to being used, which was probably bundled with a different software. I moved the Env. Variable up to the top, and the issue was fixed.

Thanks again!
msg416758 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2022-04-05 07:41
There is something fundamentally wrong with the way modules built into the interpreter DLL (python3x.dll) are loaded if anything in sys.path or the system PATH can cause an import to fail.
msg416784 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2022-04-05 14:50
> There is something fundamentally wrong with the way modules built into the interpreter DLL (python3x.dll) are loaded if anything in sys.path or the system PATH can cause an import to fail.

Probably there was also shadowing involved, since the built-in module doesn't try to load anything else. Would be nice to know for sure (@Matthew) to make sure we don't have some other issue here, but you're right, I don't see any way for this to happen without other causes.
msg416811 - (view) Author: Matthew (MatthewJ) Date: 2022-04-05 19:51
> Probably there was also shadowing involved, since the built-in module doesn't try to load anything else. Would be nice to know for sure (@Matthew) to make sure we don't have some other issue here, but you're right, I don't see any way for this to happen without other causes.

I'm pretty sure the Python interpreter that was causing the issue was bundled with the MSYS2 Mingw64 compiler. I tried reproducing the bug, but I've recently reinstalled the compiler due to some issues I was having with it, and the bug with importing binascii is no longer present.
History
Date User Action Args
2022-04-11 14:59:58adminsetgithub: 91359
2022-04-05 19:51:45MatthewJsetmessages: + msg416811
2022-04-05 14:50:11steve.dowersetmessages: + msg416784
2022-04-05 07:41:16eryksunsetmessages: + msg416758
2022-04-04 22:43:02MatthewJsetstatus: open -> closed
resolution: not a bug
messages: + msg416702

stage: resolved
2022-04-04 09:45:20steve.dowersetmessages: + msg416660
2022-04-03 16:09:04jklothsetmessages: + msg416629
2022-04-03 15:27:55eryksunsetnosy: + eryksun
messages: + msg416626
2022-04-02 20:26:20eric.smithsetnosy: + paul.moore, tim.golden, zach.ware, steve.dower
components: + Windows
2022-04-02 20:02:47jklothsetnosy: + jkloth
messages: + msg416584
2022-04-02 18:09:59MatthewJcreate