Shobhit Tewari
Shobhit Tewari

Reputation: 535

import win32api in python 3.7 resulting in dll import error

I am having the error while running jupyter notebook. Error replication:

python --version
Python 3.7.9
python
import win32api

The error was: ImportError: DLL load failed After doing a some stackoverflow, I got to know that there are 2 dll files missing namely: pythoncom37.dll and pywintypes37.dll

I also got to know that I can run this post command: pywin32_postinstall.py in the Scripts folder. I ran this script. Restarted my pc. I manually download these two dll and copied it to my system32. After manually downloading, I started to have a different type of error: ImportError: DLL load failed: %1 is not a valid Win32 application

I used anaconda and ran my jupyter notebook which was my main aim but can you please make me aware as what is going wrong?

Upvotes: 2

Views: 3556

Answers (4)

Piotr
Piotr

Reputation: 33

In may case helped starting exe-application "As administrator"

Upvotes: 0

Tom
Tom

Reputation: 1

Just FYI: I spend a few hours on this "fun". In the end it turned out that Python 3.8 ran just fine (executed correctly the "import win32com.client" line) -- and the problem was Spyder. I uninstalled and reinstalled the latest version and it all started to work. Go figure...

Upvotes: 0

Jarold
Jarold

Reputation: 29

pip install --upgrade pywin32 ==225 worked for me. Tried version 300 and was unsuccessful.

Upvotes: 0

Jeff Arnold
Jeff Arnold

Reputation: 121

I've run into this recently, but with a different version of the DLLs. What solved it for me was using a different version of pywin32. My solution (conda env, python 3.8.5):

pip install pywin32==300

or try 225, 227, 228. The latest pywin32 (301 as of this post) seems to be having dll search issues (I wouldn't be surprised if whatever version you were using is also having dll search issues). 301 was released after your issue started, but you may have a similar problem nonetheless.

There is currently an issue on pywin32 DLL loading failing: https://github.com/mhammond/pywin32/issues/1709

Factors involved (in my experience) include your PATH variable (if you're using conda). I haven't tested it myself, but I'd be curious to see if this issue occurs without conda. This issue stops happening for me if the first dlls found are those for 301. In my case, that means putting them in my C:\Windows\System32 folder (yeah I'm on Windows; joy).

So a possible solution #2 would be to run the pywin32 post install script which should be located under your venv/Scripts/pywin32_postinstall.py

To try that solution, open an ADMIN command prompt (very important that it's admin), navigate to your venv, and run:

ppython.exe Scripts\pywin32_postinstall.py --install

You shouldn't HAVE to do this, but if you just need a one-off solution and it works, great!

Upvotes: 1

Related Questions