tags:

views:

275

answers:

4

Hi,

I'm on Mac OS X 10.6. Python is 2.6.1. I've installed bencode module as

sudo easy_install BitTorrent-bencode

It appeared in site-packages

/Library/Python/2.6/site-packages/BitTorrent_bencode-5.0.8-py2.6.egg

But, how to import and use this module?

>>> import bencode

doesn't work...

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named bencode

How to import modules from site-packages? How to recognize module name BitTorrent_bencode-5.0.8-py2.6.egg contains?

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']

Thanks

A: 

The way to do it is to add the .egg to sys.path. The easiest way to do that is to drop a .pth file into site-packages/ containing the filename of the egg:

BitTorrent_bencode.pth:

BitTorrent_bencode-5.0.8-py2.6.egg

If it still doesn't import properly at this point then you may have the module or package name wrong. Open the .egg file in something that can open .zip files and examine the internal structure for hints.

Ignacio Vazquez-Abrams
There is already easy-install.pth that contains"import sys; sys.__plen = len(sys.path)./BitTorrent_bencode-5.0.8-py2.6.eggimport sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)"Is this enough?
bocco
Sure. In which case you should proceed with the rest of my answer. If *that* also produces no results then the egg itself is broken, and you should notify upstream about this.
Ignacio Vazquez-Abrams
A: 

From what i see, BitTorrent_bencode-5.0.8-py2.4.egg on pypi does not containt bencode.py.
I would download sources and manually copy bencode.py and BTL.py in your site-packages folder.

systempuntoout
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) [GCC 4.2.1 (Apple Inc. build 5646)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> from BitTorrent.bencode import bencodeTraceback (most recent call last): File "<stdin>", line 1, in <module>ImportError: No module named BitTorrent.bencode:-(
bocco
i was changing my answer because i tried on Osx using pip and i got the same error.I've tried to dezip .egg and i did not found sources.
systempuntoout
Yes, you are right, man! Thanks a lot!
bocco
You are welcome.
systempuntoout
+1  A: 

The BitTorrent_bencode-5.0.8-py2.4.egg is broken. If you use unzip to look at the contents of the egg you'll see:

$ 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    

Notice that bencode.py and BTL.py are not included. If you download the source distribution for the package from pypi, you can get the missing files. The problem with the package is that the setup.py does not include the root directory of the distribution in the list of packages that the egg is created by. To fix this you could edit setup.py and replace the line that says:

     packages = find_packages(),

with:

     packages = ['','test'],

Then, running python setup.py install will correctly install the package.

Jason Jenkins
OK, thanks! Actually, I already copied bencode.py and BTL.py to site-packages manually.
bocco
A: 

You can alternatively use the bzrlib.bencode package. This installs with easy_install or pip.