Simple Python Project Structure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • amit

    Simple Python Project Structure

    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.co nsole import filetest

    but I get an ImportError.

    Thanks!

    Amit
  • jay graves

    #2
    Re: Simple Python Project Structure

    On Oct 10, 7:17 pm, amit <amit.ut...@gma il.comwrote:
    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
    >
    You need an __init__.py file (it doesn't matter if it's empty) in the
    directory to turn it into a package.



    HTH.

    ....
    Jay Graves

    Comment

    • George Sakkis

      #3
      Re: Simple Python Project Structure

      On Oct 10, 9:26 pm, jay graves <jaywgra...@gma il.comwrote:
      On Oct 10, 7:17 pm, amit <amit.ut...@gma il.comwrote:
      >
      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
      >
      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/...08400000000000...
      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

      Comment

      • Ben Finney

        #4
        Re: Simple Python Project Structure

        George Sakkis <george.sakkis@ gmail.comwrites :
        '''
        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.
        The underlying problem, of course, is that Python's ‘import’ statement
        doesn't let the programmer distinguish between “import from the
        system search path” versus “import from this local package”. If it
        did, you could say “import ‘string’ from the system search path”
        without worrying about what happens when a ‘string’ module or package
        is also located in your local package.

        Fortunately, this is already addressed with absolute imports versus
        relative imports <URL:http://www.python.org/dev/peps/pep-0328>. See
        the “Timeline” section in that PEP for what import behaviour to
        expect under different Python versions.

        --
        \ “Contentment is a pearl of great price, and whosoever procures |
        `\ it at the expense of ten thousand desires makes a wise and |
        _o__) happy purchase.” —J. Balguy |
        Ben Finney

        Comment

        Working...