如何在 Python 中设置 unrar 库的路径? [英] How to set path to unrar library in Python?

查看:52
本文介绍了如何在 Python 中设置 unrar 库的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Pycharm 作为我的 IDE (Python 3.7) 并试图提取受密码保护的 .rar 文件(我知道密码)并已从 unrar 导入 rarfile 但出现此错误 LookupError:Cablen"t 找到解压缩库的路径."

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."

我还尝试将导入语句更改为仅说导入 rarfile",但出现以下错误 rarfile.RarCannotExec:未安装 Unrar?"

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

我还尝试根据我在 rarfile 文档中找到的内容包含这行代码:rarfile.UNRAR_TOOL = "unrar" 但是我遇到了相同的错误.

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.

这是我的代码片段:

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


推荐答案

在不同的os上需要不同的解决方案:在 Windows 上:

on different os need different solutions: on Windows:

  1. 下载lib文件,http://www.rarlab.com/rar/UnRARDLL.exe,安装;

你最好选择默认路径,C:\Program Files (x86)\UnrarDLL\

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

最重要的是添加环境路径,varname输入UNRAR_LIB_PATH,注意,一定是!!!.那么如果你的系统是 64 位输入 C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll,如果你的系统是 32 位输入 C:\Program Files (x86)\UnrarDLL\UnRAR.dll.

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.

保存环境路径后,重新运行你的pycharm.

after save the environment path, rerun your pycharm.

在Linux上需要制作so文件,有点困难.

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

  1. 同理,下载libfile http://www.rarlab.com/rar/unrarsrc-5.4.5.tar.gz,可以选择最新版本.

下载后解压得到文件unrar,cd unrar,然后make lib,然后make install-lib,我们将得到文件 libunrar.so(在/usr/lib 中).

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).

最后,还需要设置环境路径,vim/etc/profile打开文件profile,添加export UNRAR_LIB_PATH=/usr/lib/libunrar.so 在文件末尾.然后保存文件,使用source/etc/profile使环境成功.

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.

重新运行 .py 文件.

rerun the .py file.

资源网址:https://blog.csdn.net/ysy950803/article/详情/52939708

这篇关于如何在 Python 中设置 unrar 库的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆