Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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


like image 246
programmingflaw Avatar asked Apr 08 '19 13:04

programmingflaw


2 Answers

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

like image 146
rpb Avatar answered Sep 23 '22 08:09

rpb


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

like image 34
Tom.chen.kang Avatar answered Sep 23 '22 08:09

Tom.chen.kang