python-3.xpycharmrarunrar

How to set path to unrar library in Python?


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



Solution

  • 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