[Tutor] accessing modules found throughout a package?

Ben Finney ben+python at benfinney.id.au
Sat Oct 17 21:29:58 EDT 2015


James Hartley <jjhartley at gmail.com> writes:

> In my current project, I am developing a package. It makes sense to
> embed tests throughout the package's directory structure as they
> should be part of the package & its distribution.

Yet, as you're finding, it also causes headaches.

First, know that you don't need to scatter the test suite through out the
rest of your code in order to meet the goal of including it in the
distribution.

You can distribute the test suite along with the rest, by specifying it
as part of the source distribution. See the “manifest” for Distutils.

To have your test suite run correctly when it's located separately from
the system under test, you need to specify the top-level directory as
being in the Python import path, during the test suite run.

Then, you can have all the test suite modules do relative imports from
that top level, to get the modules they need to import from the system
under test.

That way, the test suite imports modules the same way any other user of
your code will import them: from the top level package.

Look up “relative and absolute imports” to see how they work, and how
they differ, in Python.

> Any suggestions you may have will be very welcomed.

I hope that helps.

-- 
 \      “Faith is generally nothing more than the permission religious |
  `\     people give to one another to believe things strongly without |
_o__)      evidence.” —Sam Harris, _Letter to a Christian Nation_ 2006 |
Ben Finney



More information about the Tutor mailing list