Python site-packages and import

Maurice LING mauriceling at acm.org
Fri Apr 29 20:38:30 EDT 2005


Peter Saffrey wrote:

> (apologies for starting a new thread - Google can't retrieve the other
> message for some reason)
> 
> Yes, /usr/lib/python/site-packages is in sys.path. This series of
> commands should explain what I mean: I've put the Python ID3 module in
> a sub-directory of site-packages as an illustration.
> 
> pzs at bonnie:~$ ls /usr/lib/python2.3/site-packages/
> ID3.py  ID3.pyc  ID3.pyo  README  apt_inst.so  apt_pkg.so  apt_proxy 
> bsddb3  debconf.py  debconf.pyc  debconf.pyo  id3  pj  twisted
> 
> pzs at bonnie:~$ ls /usr/lib/python2.3/site-packages/apt_proxy/
> __init__.py   __init__.pyo  apt_proxy.pyc  apt_proxy_conf.py  
> apt_proxy_conf.pyo  apt_proxytap.pyc  memleak.py   memleak.pyo 
> misc.pyc  packages.py   packages.pyo
> __init__.pyc  apt_proxy.py  apt_proxy.pyo  apt_proxy_conf.pyc 
> apt_proxytap.py     apt_proxytap.pyo  memleak.pyc  misc.py     
> misc.pyo  packages.pyc  twisted_compat
> 
> pzs at bonnie:~$ ls /usr/lib/python2.3/site-packages/id3       
> ID3.py  ID3.pyc  ID3.pyo
> 
> pzs at bonnie:~$ python
> Python 2.3.5 (#2, Feb  9 2005, 00:38:15) 
> [GCC 3.3.5 (Debian 1:3.3.5-8)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> 
>>>>import sys
>>>>sys.path
> 
> ['', '/usr/lib/python23.zip', '/usr/lib/python2.3',
> '/usr/lib/python2.3/plat-linux2', '/usr/lib/python2.3/lib-tk',
> '/usr/lib/python2.3/lib-dynload',
> '/usr/local/lib/python2.3/site-packages',
> '/usr/lib/python2.3/site-packages']
> 
>>>>import apt_proxy.memleak
>>>>import id3.ID3
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> ImportError: No module named id3.ID3
> 
> 
> 
> 
> Peter

your site-package/id3 lacks a __init__.py file. '__init__.py' file can 
be just an empty file but it needs to be there for python to be 
considered as a package.

Alternatively, you can use 'from id3 import ID3' instead of 'import id3.ID3'

Cheers
Maurice



More information about the Python-list mailing list