How to set path to unrar library in Python?
Asked Answered
D

3

7

I am using Pycharm as my IDE (Python 3.7) and am trying to extract a password protected .rar file (I know the password) and have imported rarfile from unrar but am getting this error "LookupError: Couldn't find path to unrar library."

I also attempted changing my import statement to just say "import rarfile" but instead got the following error "rarfile.RarCannotExec: Unrar not installed?"

I also tried including this line of code, based on something I found in the rarfile documentation: rarfile.UNRAR_TOOL = "unrar" however I got the same errors.

Here is a snippet of my code:

from unrar import rarfile

def hacker(file_path):
    passwords = open('pwds.txt', 'r')
    with rarfile.RarFile(file_path) as file:
        for line in passwords:
            try:
                file.pwd = line
                file.extractall()
            except RuntimeError:
                pass


Dowson answered 8/4, 2019 at 13:7 Comment(0)
J
5

on different os need different solutions: on Windows:

  1. download the libfile, http://www.rarlab.com/rar/UnRARDLL.exe, install it;

  2. you'd better choose the default path, C:\Program Files (x86)\UnrarDLL\

  3. the most important is add the environment path, the varname enter UNRAR_LIB_PATH, pay attention, it must be!!!. then if your system is 64bit enter C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll, if your system is 32bit enter C:\Program Files (x86)\UnrarDLL\UnRAR.dll.

  4. after save the environment path, rerun your pycharm.

on Linux you need to make so file, which is a little difficult.

  1. the same, download the libfile http://www.rarlab.com/rar/unrarsrc-5.4.5.tar.gz, you can choose the latest version.

  2. after download extract the file get the file unrar, cd unrar ,then make lib, then make install-lib, we'll get file libunrar.so(in /usr/lib).

  3. last, you also need to set the environment path, vim /etc/profile open file profile, add export UNRAR_LIB_PATH=/usr/lib/libunrar.so in the end of the file. then save the file, use source /etc/profile to make the environment successful.

  4. rerun the .py file.

the resource website:https://blog.csdn.net/ysy950803/article/details/52939708

Jarlathus answered 8/4, 2019 at 15:36 Comment(0)
S
6

In addition to @tom answer for Windows 10 environment, the following steps should help:

  1. Download the libfile via the link and install it.
  2. For easy replication the following steps, choose the default path, C:\Program Files (x86)\UnrarDLL\
  3. Go to Environment Variables window (link) and selected Advanced.
  4. Click Environment Setting.
  5. Under the User variables, select New.
  6. In the New User Variables, rename the Variable name as UNRAR_LIB_PATH
  7. To select the Variable Value, select Browse file. Depending on your system, 64bit enter C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll, if your system is 32bit enter C:\Program Files (x86)\UnrarDLL\UnRAR.dll.
  8. Save the environment path and rerun your Pycharm.

The graphical illustration is as below,

enter image description here

Sessler answered 10/6, 2020 at 3:0 Comment(0)
J
5

on different os need different solutions: on Windows:

  1. download the libfile, http://www.rarlab.com/rar/UnRARDLL.exe, install it;

  2. you'd better choose the default path, C:\Program Files (x86)\UnrarDLL\

  3. the most important is add the environment path, the varname enter UNRAR_LIB_PATH, pay attention, it must be!!!. then if your system is 64bit enter C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll, if your system is 32bit enter C:\Program Files (x86)\UnrarDLL\UnRAR.dll.

  4. after save the environment path, rerun your pycharm.

on Linux you need to make so file, which is a little difficult.

  1. the same, download the libfile http://www.rarlab.com/rar/unrarsrc-5.4.5.tar.gz, you can choose the latest version.

  2. after download extract the file get the file unrar, cd unrar ,then make lib, then make install-lib, we'll get file libunrar.so(in /usr/lib).

  3. last, you also need to set the environment path, vim /etc/profile open file profile, add export UNRAR_LIB_PATH=/usr/lib/libunrar.so in the end of the file. then save the file, use source /etc/profile to make the environment successful.

  4. rerun the .py file.

the resource website:https://blog.csdn.net/ysy950803/article/details/52939708

Jarlathus answered 8/4, 2019 at 15:36 Comment(0)
T
0

Additionally, after you do the things as mentioned by Tom.chen.kang and balandongiv, if you're using a 32bit DLL with 64bit Python, or vice-versa, then you'll probably get an error like this when you try to import unrar:-

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

In that case do this:

For 32 Python & 32 bit DLL Change your Environment variables for variable UNRAR_LIB_PATH to :

C:\Program Files (x86)\UnrarDLL\UnRAR.dll

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.

Thiosinamine answered 10/6, 2021 at 9:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.