Project layout / Import files from different subdirectories

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

    Project layout / Import files from different subdirectories

    Hi folks.

    I'm new to python and have a slight problem importing - or maybe
    understanding - modules. I'm writing a GUI application using Qt4 and
    wanted to separate the business from the view logic. So I have my folder
    structure as following:

    project/ main.py
    important.py

    project/ gui/ __init__.py
    mainwindow.py
    anotherwindow.p y

    Now I can import mainwindow etc. from main and important, but how do I
    do it the other way round?

    Also, is there maybe a better project layout? I couldn't find anything
    useful on it asking Dr. Google.

    Best regards,
    Markus

    --
    PGP/GPG key 0x2EB39BF9
    --
    --
    PGP/GPG key 0x2EB39BF9
  • Diez B. Roggisch

    #2
    Re: Project layout / Import files from different subdirectories

    Markus Mayer schrieb:
    Hi folks.
    >
    I'm new to python and have a slight problem importing - or maybe
    understanding - modules. I'm writing a GUI application using Qt4 and
    wanted to separate the business from the view logic. So I have my folder
    structure as following:
    >
    project/ main.py
    important.py
    >
    project/ gui/ __init__.py
    mainwindow.py
    anotherwindow.p y
    >
    Now I can import mainwindow etc. from main and important, but how do I
    do it the other way round?
    >
    By placing a __init__.py into project, and then

    import project.main
    import project.gui.mai nwindow


    Diez

    Comment

    • Markus Mayer

      #3
      Re: Project layout / Import files from different subdirectories

      Diez B. Roggisch schrieb:
      >
      By placing a __init__.py into project, and then
      >
      import project.main
      import project.gui.mai nwindow
      >
      >
      Diez
      Ouch. Thanks.

      Markus

      --
      PGP/GPG key 0x2EB39BF9

      Comment

      • Steve Holden

        #4
        Re: Project layout / Import files from different subdirectories

        Markus Mayer wrote:
        Diez B. Roggisch schrieb:
        >By placing a __init__.py into project, and then
        >>
        >import project.main
        >import project.gui.mai nwindow
        >>
        >>
        >Diez
        >
        Ouch. Thanks.
        >
        If you want shorter names in your main code, of course, you can use

        import project.main as main
        import project.gui.mai nwindow as window

        or somethihg similar.

        regards
        Steve
        --
        Steve Holden +1 571 484 6266 +1 800 494 3119
        Holden Web LLC http://www.holdenweb.com/

        Comment

        • Markus Mayer

          #5
          Re: Project layout / Import files from different subdirectories

          Steve Holden schrieb:
          If you want shorter names in your main code, of course, you can use
          >
          import project.main as main
          import project.gui.mai nwindow as window
          >
          or somethihg similar.
          >
          regards
          Steve
          Yeah, I was going with the "from x import y" scheme by now, didn't know
          "as" was available as well. Perfect, that makes it a bit easier.

          Thanks!
          Markus

          --
          PGP/GPG key 0x2EB39BF9

          Comment

          • Almar Klein

            #6
            Re: Project layout / Import files from different subdirectories

            If your main file is in the root of the project, you can just
            use absolute imports. So you can use gui.anotherwind ow
            or project.importa nt from all files.

            I'm not sure this is good practice though...

            I was first under the impression that you can always import
            modules that are in your current working dir. But this seems
            to not always work...

            Almar


            2008/11/11 Jeremiah Dodds <jeremiah.dodds @gmail.com>:
            >
            >
            On Tue, Nov 11, 2008 at 7:08 AM, Markus Mayer <code@organisat i.onwrote:
            >>
            >Steve Holden schrieb:
            If you want shorter names in your main code, of course, you can use
            >
            import project.main as main
            import project.gui.mai nwindow as window
            >
            or somethihg similar.
            >
            regards
            Steve
            >>
            >
            >
            Are explicit relative imports applicable here? Could he do a
            >
            from .. import project.main as main
            >
            without adding another __init__.py?
            >
            Or do I have my head screwed on funny?
            >
            --

            >
            >

            Comment

            • Steve Holden

              #7
              Re: Project layout / Import files from different subdirectories

              Almar Klein wrote:
              If your main file is in the root of the project, you can just
              use absolute imports. So you can use gui.anotherwind ow
              or project.importa nt from all files.
              >
              I'm not sure this is good practice though...
              >
              I was first under the impression that you can always import
              modules that are in your current working dir. But this seems
              to not always work...
              >
              It works when the program you are executing is in the current working
              directory, because Python always puts the directory containing the
              program you are executing (not the current working directory) on the path.

              regards
              Steve
              --
              Steve Holden +1 571 484 6266 +1 800 494 3119
              Holden Web LLC http://www.holdenweb.com/

              Comment

              • Almar Klein

                #8
                Re: Project layout / Import files from different subdirectories

                It works when the program you are executing is in the current working
                directory, because Python always puts the directory containing the
                program you are executing (not the current working directory) on the path.
                Aha, that makes sense.
                I also found with a quick test that importing a module from the current dir
                works because '' is part of sys.path. I'm again amazed how beautifull it is
                all put together :)

                Cheers,
                Almar

                Comment

                • Gabriel Genellina

                  #9
                  Re: Project layout / Import files from different subdirectories

                  En Tue, 11 Nov 2008 09:29:44 -0200, Stef Mientki <stef.mientki@g mail.com>
                  escribió:
                  On Tue, Nov 11, 2008 at 8:46 AM, Markus Mayer <code@organisat i.onwrote:
                  >
                  >I'm new to python and have a slight problem importing - or maybe
                  >understandin g - modules. I'm writing a GUI application using Qt4 and
                  >wanted to separate the business from the view logic. So I have my folder
                  >structure as following:
                  >>
                  >project/ main.py
                  > important.py
                  >>
                  >project/ gui/ __init__.py
                  > mainwindow.py
                  > anotherwindow.p y
                  >>
                  >Now I can import mainwindow etc. from main and important, but how do I
                  >do it the other way round?
                  >>
                  >Also, is there maybe a better project layout? I couldn't find anything
                  >useful on it asking Dr. Google.
                  >
                  >
                  Since a couple of days,
                  (good to know it's not a long established practice!)
                  I use this construct and it seems to work quite well,
                  http://mientki.ruhosting.nl/data_www...importing.html
                  uhm... I don't think extending sys.path to include every package is a good
                  idea. You're basically flattening the directory structure. Just put all
                  your stuff in a single directory if this is what you want -- if that's not
                  what you want, it is what you are actually doing, anyway.
                  Among other nasty things, you may end up having multiple copies of the
                  same module, when imported using different names.

                  --
                  Gabriel Genellina

                  Comment

                  Working...