Solving error when using Pyinstaller win32ctypes.pywin32.pywintypes.error: (1920, ‘LoadLibraryExW’, ‘System cannot access the file’)

Error when using Pyinstaller win32ctypes.pywin32.pywintypes.error: (1920, ‘LoadLibraryExW’, ‘System cannot access the file’) is an error which occurs when you try to generate a .exe file from a python file using Pyinstaller.

Today, I will explain why this error takes place and how to fix it, while also presenting the steps in detail and adding other solutions that could solve the error.

Exploring error when using Pyinstaller win32ctypes.pywin32.pywintypes.error: (1920, ‘LoadLibraryExW’, ‘System cannot access the file’)

This is an error which occurs when you try to generate a .exe file from a python file using Pyinstaller.

Please make sure the error message looks like the error message bellow after double checking. Do not mix between errors.

                                                                       #
win32ctypes.pywin32.pywintypes.error: (1920, 'LoadLibraryExW', 'System can not access the file')
                                                                       #

Bellow, I will present to you some methods that have solved the issue for me, good luck.

Solution 1 : Move the .py file to a different location where pyinstaller can run properly

The first method is simple, just find the folder named scripts. 

Its located where python is installed, for me its is in the following path ( I have Python 3.9 )

                                                                       #
C:\Users\griff\AppData\Local\Programs\Python\Python39-32\Scripts
                                                                       #

The solution is to grab the .py file you want to pack into .exe and paste it inside the scripts folder.

Make sure the executable file pyinstaller.exe is there, which should be the case.

Open Cmd as admin and run the operation again. Using the command bellow for example

                                                                       #
pyinstaller --onefile myfile.py
                                                                       #

If the above does not work, then instead of moving your file to

                                                                       #
C:\Users\griff\AppData\Local\Programs\Python\Python39-32\Scripts
                                                                       #

Move it to the temp directory instead of the users directory

                                                                       #
C:\Temp
                                                                       #

This should work. If it does not, please try the method bellow.

Solution 2 : Create a virtual environment and install pyinstaller in the virtualenv

Similar to the method above, it seems Pyinstaller cannot run properly in windows for many developers.

If you tried moving the .py file to a different location like in the previous method and it did not work, then there is a permission issue in windows.

A great solution is to install pyinstaller inside a virtualenv, of course you should create a virtual environment first.

You can also use pycharm with a virtual environment and pyinstaller will almost always work with no problems

This should be enough to get rid of the error for good. Thank you for reading this whole blog post.

Summing-up : 

This is the end of this article, I hope we helped you solve the error or at least show you why it happens and guided you in the right direction. If you want to help, you can donate to our Kofi account.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/