need clarification with import statements

Discussion in 'Python' started by Tool69, Dec 14, 2006.

  1. Tool69

    Tool69 Guest

    Hi,
    I've got the following hierarchy:

    mainprog/
    __init__.py
    prog.py
    utils/
    __init__.py
    myutils.py
    others/
    __init__.py
    myothers.py

    Inside prog.py I need to have full access to myutils.py and
    myothers.py;
    Inside myutils.py, I need to access two classes from myothers.py (ie
    myotherClass1 and myotherClass2);
    Inside myothers.py, I need some functions of myutils.py (ie
    myutils_func1 and myutils_func2);

    Do you have some hints please ? I'm trying to avoid name conflicts, so
    I haven't used "from ... import *".
    I suspect ther's something wrong, but I think I really don't understand
    what's going on internally (no errors (for the moment !))

    Inside prog.py : from utils.myutils import myotherClass1, myotherClass2
    + some functions of myutils I need
    Inside myutils.py : from other.myothers import myotherClass1,
    myotherClass2
    Inside others.py : import utils.myutils

    Thanks.
     
    Tool69, Dec 14, 2006
    #1
    1. Advertising

  2. Tool69

    John Machin Guest

    Tool69 wrote:
    > Hi,
    > I've got the following hierarchy:
    >
    > mainprog/
    > __init__.py
    > prog.py
    > utils/
    > __init__.py
    > myutils.py
    > others/
    > __init__.py
    > myothers.py
    >
    > Inside prog.py I need to have full access to myutils.py and
    > myothers.py;
    > Inside myutils.py, I need to access two classes from myothers.py (ie
    > myotherClass1 and myotherClass2);
    > Inside myothers.py, I need some functions of myutils.py (ie
    > myutils_func1 and myutils_func2);
    >
    > Do you have some hints please ?


    1. Your directory/package hierarchy is far too complicated. Flatten it.

    2. You have circular references: the others module will import from
    utils, but utils wants to import from others. This is prima facie
    evidence that your modules are not structured properly. Rule 1: Modules
    should contain /related/ classes and functions. Rule 2: A graph of what
    imports what should not have loops.

    Consider combining others and utils -- does that make sense?

    Another alternative: split out those "some functions of myutils.py (ie
    myutils_func1 and myutils_func2)" into a fourth module. This module
    might be the nucleus of a tool-kit of miscellaneous functions that you
    might import in /any/ app -- in that case, move it outside the current
    package.

    HTH,
    John
     
    John Machin, Dec 15, 2006
    #2
    1. Advertising

  3. Tool69

    Tool69 Guest

    Hi John,
    > 1. Your directory/package hierarchy is far too complicated. Flatten it.


    Ok.

    > 2. You have circular references: the others module will import from
    > utils, but utils wants to import from others. This is prima facie
    > evidence that your modules are not structured properly


    Yes, that's why I asked the question in fact, circular references are a
    bit hazardous.

    >. Rule 1: Modules should contain /related/ classes and functions.
    > Rule 2: A graph of what imports what should not have loops.
    >
    > Consider combining others and utils -- does that make sense?


    Yes, I can combine them as they need eachother.

    > Another alternative: split out those "some functions of myutils.py (ie
    > myutils_func1 and myutils_func2)" into a fourth module. This module
    > might be the nucleus of a tool-kit of miscellaneous functions that you
    > might import in /any/ app -- in that case, move it outside the current
    > package.


    Good idea indeed,
    Thanks for all,
    6TooL9
     
    Tool69, Dec 15, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    8
    Views:
    1,208
    mfmehdi
    Oct 20, 2006
  2. Harry George
    Replies:
    6
    Views:
    417
    Bart Nessux
    Feb 23, 2004
  3. Vince
    Replies:
    12
    Views:
    762
    Martin Gregorie
    Jan 21, 2008
  4. Raymond Schanks
    Replies:
    1
    Views:
    401
  5. John Crichton
    Replies:
    6
    Views:
    271
    John Crichton
    Jul 12, 2010
Loading...

Share This Page