How to simulate packages?

Jason Scheirer jason.scheirer at
Thu Aug 21 20:52:01 CEST 2008

On Aug 21, 11:26 am, "Gabriel Genellina" <gagsl-... at>
> En Thu, 21 Aug 2008 13:04:51 -0300, Daniel <daniel.watr... at>  
> escribi :
> > I have a project that I've decided to split into packages in order to
> > organize my code better.  So what I have looks something like this
> > src
> >   -module1
> >
> >   -module2
> >
> > Everytime I run I need to import  Right now
> > I'm using an ugly little thing to do my import (see below).  Isn't
> > there some python mechanism to handle packages?
> Sure - Python *has* packages, you don't have to "simulate" them.
> Read
> --
> Gabriel Genellina

You can use relative imports, so in you could

import ..module1.mod1_file as mod1

To pull in a dependent submodule. Also, I think you are thinking about


And in your you issue something like

import mod1
import mod2

if you need them loaded as soon as your package is imported, but you
may omit that and import piecemeal in your scripts as each one needs

from mypackage.mod2 import just_this_one_function

Etc etc etc. But really if you only have a single Python file living
in each subdirectory, you are likely Doing It Wrong and your code
might just make more sense as:


Where your intuitively named submodules are all logical groupings of
functionality. Remember that they don't ALL get imported right away,
in fact, only is imported by default, so if that's missing
or doesn't import the other submodules by default, you have to

import package.intuitively_named_submoduleN

each time you want to import a submodule.

Modules and packages:
Relative imports:

More information about the Python-list mailing list