python-unrar icon indicating copy to clipboard operation
python-unrar copied to clipboard

OSError: [WinError 193] %1 is not a valid Win32 application

Open HatScripts opened this issue 6 years ago • 5 comments

Get this error every time I try to import unrar:

Traceback (most recent call last):
  File "D:/IdeaProjects/Python/Downloader/archive.py", line 6, in <module>
    from unrar import rarfile
  File "%LocalAppData%\Programs\Python\Python36\lib\site-packages\unrar\rarfile.py", line 26, in <module>
    from unrar import unrarlib
  File "%LocalAppData%\Programs\Python\Python36\lib\site-packages\unrar\unrarlib.py", line 45, in <module>
    unrarlib = ctypes.WinDLL(lib_path)
  File "%LocalAppData%\Programs\Python\Python36\lib\ctypes\__init__.py", line 348, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application

What I've tried:

  • Installing UnRAR via the exe found at https://www.rarlab.com/rar/UnRARDLL.exe and then adding the C:\Program Files (x86)\UnrarDLL to the system PATH. Also tried setting UNRAR_LIB_PATH to C:\Program Files (x86)\UnrarDLL, C:\Program Files (x86)\UnrarDLL\UnRAR.dll, etc.
  • Manually extracting UnRAR.dll from said exe and pointing UNRAR_LIB_PATH to it. Tried the same also with UnRAR64.dll.
  • Renaming the installer exe to unrar.dll and adding it to PATH (lol i give up)

Edit: Just realised the last commit was in 2016. That was a waste of 2 hours.

HatScripts avatar Mar 14 '19 11:03 HatScripts

Hi there, It has been a while since I tried to set this up in Windows, maybe something broke. FWIW, installing the unrar lib and pointing to it through the env variable should work (assuming you install the right version for your python/system arch). The lib is just a wrapper using ctypes, so as long as the ctypes link to the unrar lib works, it should work. Sorry you couldn't make it work, there should be some trick we are missing.

matiasb avatar Apr 18 '19 14:04 matiasb

Hi @HatScripts , I had the same issue and I found the reason: Your python version should a 64-bit version, thus you should set environment variable to C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll. Hope that helps.

wenwen412 avatar Nov 10 '19 05:11 wenwen412

I am facing the same issue despite following @wenwen412 suggestions. My code is below. I have verified that my Windows is 64 bit, python version running on anaconda is also 64 bit. I am also using the 64bit UnRAR DLL and yet I am getting this error message. How should I resolve?

rarfile.UNRAR_TOOL = r"C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll" url = 'http://techslides.com/demos/samples/sample.rar' r = requests.get(url, stream = True) check = rarfile.is_rarfile(io.BytesIO(r.content)) z = rarfile.RarFile(io.BytesIO(r.content)) z.extractall()

shreyadutt1 avatar Aug 20 '20 23:08 shreyadutt1

I will tell you the answer which I found and it worked for me.

If you're using a 32bit DLL with 64bit Python, or vice-versa, then you'll probably get errors.

Do this:

  1. For 32 Python & 32 bit DLL Change your Environment variables for variable UNRAR_LIB_PATH to : C:\Program Files (x86)\UnrarDLL\UnRAR.dll

  2. For 64 bit Python & 64 bit DLL Change your Environment variables for variable UNRAR_LIB_PATH to : C:\Program Files (x86)\UnrarDLL\x64\UnRAR.dll

Restart your Pycharm or other Development Environment.

kapilmar avatar Jun 10 '21 09:06 kapilmar

Hi @HatScripts , I had the same issue and I found the reason: Your python version should a 64-bit version, thus you should set environment variable to C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll. Hope that helps.

You've solved my problem! Thanks a lot

pporbaha avatar Jun 15 '22 08:06 pporbaha