python - 如何干净地导入 bencode?

标签 python python-2.7 import importerror bencoding

我正在尝试使用 bencode 库中的 bdecode,也就是说:

def bdecode(x):
    try:
        r, l = decode_func[x[0]](x, 0)
    except (IndexError, KeyError, ValueError):
        raise BTFailure("not a valid bencoded string")
    if l != len(x):
        raise BTFailure("invalid bencoded value (data after valid prefix)")
    return r

from types import StringType, IntType, LongType, DictType, ListType, TupleType

位于初始化的此处:

enter image description here

但是由于错误,我的代码无法得到任何结果。确实导入没有找到 bdecode 但我不明白为什么。 这是简单的代码和错误输出:

from bencode import *

blabla = 'd8:announce70:http://tracker.t411.io:56969/c5faa6720249d33ff6ba2af48640af89/announce7:comment29:https://www.t411.io/t/524280210:created by19:https://www.t411.io13:creation datei1431685353e4:infod6:lengthi14634059e4:name22:Charlie-Hebdo-1178.pdf12:piece lengthi262144e6:pieces1120:'
myprint = bdecode(blabla)
print myprint

enter image description here

有关我刚刚制作的“pip install bencode”的 bencode 安装信息

最佳答案

您将程序命名为 bencode.py 这会屏蔽已安装的库。重命名您的脚本并重试:

更好:

import bencode

bencode.bdecode(string_to_decode)

关于python - 如何干净地导入 bencode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30437013/

相关文章:

python - 如何使用 python 使用驱动器 API 将文件上传到谷歌驱动器?

python - 用户输入

mysql - 导入大sql文件时如何计算mysql的最大性能?

python - 导入后的 For 循环在 Python 单行中不起作用

python - Django Rest 模型配置不当 :

python - 模块导入适用于解释器,但不适用于脚本

python - 从 GitHub Actions 中的 conda 环境运行时找不到 pytest

python - 操作 numpy 数组(连接内部子数组)

python - 如果列表中的元素在另一个列表中,则删除该列表的元素

Python:__slots__ 及其描述符的使用