Usual practice: running/testing modules in a package

alito alitosis at gmail.com
Wed Aug 13 03:57:32 EDT 2008


Hi all,

I am new to using packages to group my modules.  I can't figure out
how to run a module that uses relative imports without writing a
wrapper that imports that module.  Everything I try it complains that
I am attempting a relative import in a non-package.

eg
~/python/testpackage$ ls
config.py  importer.py  __init__.py

~/python/testpackage$ cat importer.py
from . import config
config.hello()

~/python/testpackage$ cat config.py
def hello():
	print 'hello'

__init__.py is empty

~/python/testpackage$ python -V
Python 2.5.2

~/python/testpackage$ echo $PYTHONPATH
.:/home/ale/python/libs:/home/ale/lib/python/:

~/python/testpackage$ python importer.py
Traceback (most recent call last):
  File "importer.py", line 1, in <module>
    from . import config
ValueError: Attempted relative import in non-package

Ok, fair enough.  There's something about running with -m for running
modules:

~/python/testpackage$ python -m importer
Traceback (most recent call last):
  File "/usr/lib/python2.5/runpy.py", line 95, in run_module
    filename, loader, alter_sys)
  File "/usr/lib/python2.5/runpy.py", line 52, in _run_module_code
    mod_name, mod_fname, mod_loader)
  File "/usr/lib/python2.5/runpy.py", line 32, in _run_code
    exec code in run_globals
  File "/home/ale/python/testpackage/importer.py", line 1, in <module>
    from . import config
ValueError: Attempted relative import in non-package

Ok, maybe it can't see the __init__.py because it is in the current
directory.  Go up one.

~/python$ python testpackage/importer.py
Traceback (most recent call last):
  File "testpackage/importer.py", line 1, in <module>
    from . import config
ValueError: Attempted relative import in non-package

No.  What about:
~/python$ python -m testpackage.importer
Traceback (most recent call last):
  File "/usr/lib/python2.5/runpy.py", line 95, in run_module
    filename, loader, alter_sys)
  File "/usr/lib/python2.5/runpy.py", line 52, in _run_module_code
    mod_name, mod_fname, mod_loader)
  File "/usr/lib/python2.5/runpy.py", line 32, in _run_code
    exec code in run_globals
  File "/home/ale/python/testpackage/importer.py", line 1, in <module>
    from . import config
ValueError: Attempted relative import in non-package

That one is very puzzling.  It knows that testpackage is a package
because I am invoking it with testpackage.importer, but still thinks
it isn't a package.

A wrapper on the level up works:

~/python$ cat importercaller.py
from testpackage import config
config.hello()

~/python$ python importercaller.py
hello

So, how do I run these modules without writing a wrapper script for
each one?  My main use actually would be to get pylint to analyse
them, but it seems to use the python import system, so it fails
whenever python fails.  If anyone can tell me how to get pychecker to
even be able to use a wrapper, it would also be very beneficial.




More information about the Python-list mailing list