问题
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
回答1:
on different os need different solutions: on Windows:
download the libfile, http://www.rarlab.com/rar/UnRARDLL.exe, install it;
you'd better choose the default path, C:\Program Files (x86)\UnrarDLL\
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.
after save the environment path, rerun your pycharm.
on Linux you need to make so file, which is a little difficult.
the same, download the libfile http://www.rarlab.com/rar/unrarsrc-5.4.5.tar.gz, you can choose the latest version.
after download extract the file get the file unrar,
cd unrar
,thenmake lib
, thenmake install-lib
, we'll get filelibunrar.so
(in /usr/lib).last, you also need to set the environment path,
vim /etc/profile
open fileprofile
, addexport UNRAR_LIB_PATH=/usr/lib/libunrar.so
in the end of the file. then save the file, usesource /etc/profile
to make the environment successful.rerun the .py file.
the resource website:https://blog.csdn.net/ysy950803/article/details/52939708
来源:https://stackoverflow.com/questions/55574212/how-to-set-path-to-unrar-library-in-python