Code Management

Ben Finney bignose+hates-spam at benfinney.id.au
Tue Nov 20 23:34:03 EST 2007


Jens <j3nsby at gmail.com> writes:

> On 21 Nov., 04:16, Jens <j3n... at gmail.com> wrote:
> > On 21 Nov., 01:46, brzr... at gmail.com wrote:
> > dummy/
> >   dummy_package/
> >     __init__.py
> >     moduleA.py
> >     tests/
> >        __init__.py
> >        test.py

To avoid confusion, the directory that is the package should be named
as you want the imports to appear; e.g. if you want to 'import
foo.module_a', then name the directory containing 'module_a.py' as
'foo/'.

This often results in::

    foo/
      setup.py
      foo/
        __init__.py
        module_a.py
      test/
        __init__.py
        test_module_a.py

That is, the *project* directory (containing all the files) is named
'foo/'; the *package* directory (where all the implementation code is
found) is named 'foo/foo/', and the unit tests are found in the
directory 'foo/test/'.

That's normal, though if it confuses you you might want to rename the
project directory. I'm often doing development on multiple
version-control branches, so each project directory is named for the
branch it contains; within each of those, the same 'foo/' name is used
for the package directory.

> > I'm using Python 2.5.1. When I'm trying to call a function in
> > 'moduleA' from 'test' it won't work unless I make the 'dummy'
> > folder a package as well. That's pretty weird. Does
> > 'dummy_package' have to be in my pythonpath or something? How do I
> > reference moduleA from test?

You should install the package into a place where Python's import path
will find it. Read up on using the standard library 'distutils'
mechanism for this; it involves writing a 'setup.py' file to define
the parameters for installation of your package.

> > I would like to avoid making 'dummy' into a package as well.

Yes. The top-level directory is used for containing a number of files,
including 'setup.py', that should not be part of the installed
package.

> Problem solved. I added 'dummy' to the PYTHONPATH. (Do I really have
> to do that for every project I create?) Anyway, it works the way I'd
> like it to now.

I hope the above makes it clearer what I prefer for this situation.

-- 
 \         "True greatness is measured by how much freedom you give to |
  `\      others, not by how much you can coerce others to do what you |
_o__)                                             want."  --Larry Wall |
Ben Finney



More information about the Python-list mailing list