How to organize source code and "import"s???

Steven Bethard steven.bethard at gmail.com
Mon Mar 28 00:52:36 EST 2005


Tian wrote:
> I also have some problem about the "import". How should I design my
> packages?
> Say, I have all code locates at c:\projects\sami, "c:\project" is in my
> PYTHONPATH environment variable. Suppose my folder structure is like
> this:
> 
> c:
> projects\             <-----this directory is in PYTHONPATH
>           sami\
>                  __init__.py
>                  main.py
>                  xmlparser.py
>                  window.py
>                  proc.py
>                  support\
>                           __init__.py
>                           helper.py
>                  plugins\
>                           __init__.py
>                           BaseClass.py    <---no instance for this one
>                           ExtClassA.py
>                           ExtClassB.py
>                           ExtClassC.py
>                           ExtClassD.py
> 
> 
> 
> Each file in \projects\sami\plugins contains a class with a same name
> as the file, (ExtClassA.py has class ExtClassA), the instance of these
> classes need to be created at runtime while functions in xmlparser.py
> is parsing an XML file. main.py is the start point of the program.
> 
> Other files in the \projects\sami\ and projects\sami\support are
> supporting modules.
> 
> 1. What should I write in each __init__.py ???

If I understand your intent correctly here, you shouldn't need anything 
in any of them.

>    In "main.py", if I need functions in "proc.py", should I write
> "import proc"?  If I need functions in "helper.py", can i write "import
> support.helper"??

No, you should use absolute imports and write:
     import sami.proc
     import sami.support.helper
or perhaps
     import sami.proc as samiproc
     import sami.support.helper as samihelper

> 2. What is the best way to make instance of a class from a string type
> name?

Why do you expect to need to do this?  Usually you can just import the 
class's module directly and then use getattr, e.g.:

py> modules = [__import__('sami.plugins.%s' % filename[:-3],
...                       globals(), locals(), ['ExtClass'])
...            for filename in os.listdir('sami/plugins')
...            if filename.endswith('.py')
...            and filename not in ['BaseClass.py', '__init__.py']]
py> modules
[<module 'sami.plugins.ExtClassA' from 
'D:\Steve\sami\plugins\ExtClassA.py'>,
<module 'sami.plugins.ExtClassB' from 'D:\Steve\sami\plugins\ExtClassB.py'>]
py> modules[0]
<module 'sami.plugins.ExtClassA' from 'D:\Steve\sami\plugins\ExtClassA.py'>
py> modules[0].ExtClass
<class 'sami.plugins.ExtClassA.ExtClass'>
py> getattr(modules[0], 'ExtClass')
<class 'sami.plugins.ExtClassA.ExtClass'>

See the documentation for __import__ for more details[1].

> But How can I import all "ExtClass?.py"?

I would do this as above, using __import__.

STeVe

[1] http://docs.python.org/lib/built-in-funcs.html



More information about the Python-list mailing list