Simple Python Project Structure

A

amit

Hi,

I recently wrote a fairly complex project in python. It works great
and it was completed fairly quickly thanks to python!

Anyways, I am in the process of cleaning the code/directory and I had
a simple question....

How do create my own modules and import them? Right now it works but
they all have to be in the same directory. For example,

project/
.....util/
.....config/
.....tests/
.....start.py

I have a module in util/console.py, how do I import that. In start.py
I have:

from project.util.console import filetest

but I get an ImportError.

Thanks!

Amit
 
G

George Sakkis

You need an __init__.py file (it doesn't matter if it's empty) in the
directory to turn it into a package.

http://www.python.org/doc/2.5.2/tut/node8.html#SECTION008400000000000...

I'm wondering if this is one of the few cases where Python's choice to
be explicit causes more trouble than it's worth. The official
reasoning is:
'''
The __init__.py files are required to make Python treat the
directories as containing packages; this is done to prevent
directories with a common name, such as "string", from unintentionally
hiding valid modules that occur later on the module search path.
'''

Is this a real problem or a speculation ? I would guess that it's at
least as likely for a newbie to create a "string.py" module than have
an irrelevant "string" subdirectory under a code directory tree.
Having to create an empty file as a flag to denote a package doesn't
seem very pythonic.

George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top