导入BitTorrent bencode模块
我在使用 Mac OS X 10.6,Python 版本是 2.6.1。
我通过以下命令安装了 bencode 模块:
sudo easy_install BitTorrent-bencode
这个模块成功安装在了 site-packages 目录下:
/Library/Python/2.6/site-packages/BitTorrent_bencode-5.0.8-py2.6.egg
但是,我该如何导入并使用这个模块呢?
>>> import bencode
这样做不行……
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named bencode
我该如何从 site-packages 中导入模块?
如何识别模块名称 BitTorrent_bencode-5.0.8-py2.6.egg 中包含的内容?
sys.path
['', '/Library/Python/2.6/site-packages/BitTorrent_bencode-5.0.8-py2.6.egg', '/Library/Python/2.6/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode']
谢谢
5 个回答
使用命令 pip install bencode.py 来安装这个库,
而不是使用 pip install bencode。
BitTorrent_bencode-5.0.8-py2.4.egg这个文件有问题。如果你用解压工具查看这个文件的内容,你会发现:
$ unzip BitTorrent_bencode-5.0.8-py2.6.egg
Archive: BitTorrent_bencode-5.0.8-py2.6.egg
inflating: EGG-INFO/dependency_links.txt
inflating: EGG-INFO/PKG-INFO
inflating: EGG-INFO/SOURCES.txt
inflating: EGG-INFO/top_level.txt
inflating: EGG-INFO/zip-safe
inflating: test/__init__.py
inflating: test/__init__.pyc
inflating: test/benchmarkbencode.py
inflating: test/benchmarkbencode.pyc
inflating: test/benchmarkdata.py
inflating: test/benchmarkdata.pyc
inflating: test/testbencode.py
inflating: test/testbencode.pyc
注意到bencode.py和BTL.py这两个文件没有包含在里面。如果你从pypi下载这个包的源代码,你就能找到缺失的文件。这个包的问题在于setup.py文件没有把分发包的根目录包含在创建egg文件的包列表里。要解决这个问题,你可以编辑setup.py文件,把里面的一行:
packages = find_packages(),
替换成:
packages = ['','test'],
然后,运行python setup.py install
就能正确安装这个包了。
根据我看到的,pypi上的BitTorrent_bencode-5.0.8-py2.4.egg里没有bencode.py这个文件。
我建议你下载源代码,然后手动把bencode.py和BTL.py这两个文件复制到你的site-packages文件夹里。