Python sub folder modules not able to import other subfolder modules












1















I have been struggling to figure out how to organize my code effectively with many modules of various types. I have a three layered folder system. The parent directory contains the main.py file which imports and runs the main code. The modules are in a subfolder called lib, and different modules are placed in further subfolders. However, when I import one module from a subfolder, that module itself fails to import modules in the same sub directory. Sorry if this is a bad question, but I cannot figure out why the import statement isn't working, and I have looked at google and stack overflow and have not found any similar problem. All advice is welcome.



Parent Directory
---->lib
|--->module_group
|module_one.py(that tries but fails to import module_one)
|module_two.py
|main.py


In main I run



from lib.module_group.module_one import Module_One


Which works until an error is handled saying that there is no such module as module two. However, when I run module_one by itself, it works fine with the following import statement.



from module_two import Module_Two









share|improve this question



























    1















    I have been struggling to figure out how to organize my code effectively with many modules of various types. I have a three layered folder system. The parent directory contains the main.py file which imports and runs the main code. The modules are in a subfolder called lib, and different modules are placed in further subfolders. However, when I import one module from a subfolder, that module itself fails to import modules in the same sub directory. Sorry if this is a bad question, but I cannot figure out why the import statement isn't working, and I have looked at google and stack overflow and have not found any similar problem. All advice is welcome.



    Parent Directory
    ---->lib
    |--->module_group
    |module_one.py(that tries but fails to import module_one)
    |module_two.py
    |main.py


    In main I run



    from lib.module_group.module_one import Module_One


    Which works until an error is handled saying that there is no such module as module two. However, when I run module_one by itself, it works fine with the following import statement.



    from module_two import Module_Two









    share|improve this question

























      1












      1








      1


      2






      I have been struggling to figure out how to organize my code effectively with many modules of various types. I have a three layered folder system. The parent directory contains the main.py file which imports and runs the main code. The modules are in a subfolder called lib, and different modules are placed in further subfolders. However, when I import one module from a subfolder, that module itself fails to import modules in the same sub directory. Sorry if this is a bad question, but I cannot figure out why the import statement isn't working, and I have looked at google and stack overflow and have not found any similar problem. All advice is welcome.



      Parent Directory
      ---->lib
      |--->module_group
      |module_one.py(that tries but fails to import module_one)
      |module_two.py
      |main.py


      In main I run



      from lib.module_group.module_one import Module_One


      Which works until an error is handled saying that there is no such module as module two. However, when I run module_one by itself, it works fine with the following import statement.



      from module_two import Module_Two









      share|improve this question














      I have been struggling to figure out how to organize my code effectively with many modules of various types. I have a three layered folder system. The parent directory contains the main.py file which imports and runs the main code. The modules are in a subfolder called lib, and different modules are placed in further subfolders. However, when I import one module from a subfolder, that module itself fails to import modules in the same sub directory. Sorry if this is a bad question, but I cannot figure out why the import statement isn't working, and I have looked at google and stack overflow and have not found any similar problem. All advice is welcome.



      Parent Directory
      ---->lib
      |--->module_group
      |module_one.py(that tries but fails to import module_one)
      |module_two.py
      |main.py


      In main I run



      from lib.module_group.module_one import Module_One


      Which works until an error is handled saying that there is no such module as module two. However, when I run module_one by itself, it works fine with the following import statement.



      from module_two import Module_Two






      python-3.x import






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 0:23









      jake_314jake_314

      308




      308
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Set PYTHONPATH to the directory you want to act as your root. For example, if you want to use the following code:



          from lib.module_group.module_one import Module_One


          Then set PYTHONPATH to the directorying containing lib. For example:



          lib/module_group/module_one.py:



          from lib.module_group.module_two import Module_Two


          lib/module_group/module_two.py:



          class Module_Two:
          print('Loaded Module_Two')


          Then, to run module_one.py directly and still enable it to use lib.module_group.module_two to load Module_Two, use something like:



          $ PYTHONPATH="${PWD}:${PYTHONPATH}" python3 lib/module_group/module_one.py
          Loaded Module_Two





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473369%2fpython-sub-folder-modules-not-able-to-import-other-subfolder-modules%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Set PYTHONPATH to the directory you want to act as your root. For example, if you want to use the following code:



            from lib.module_group.module_one import Module_One


            Then set PYTHONPATH to the directorying containing lib. For example:



            lib/module_group/module_one.py:



            from lib.module_group.module_two import Module_Two


            lib/module_group/module_two.py:



            class Module_Two:
            print('Loaded Module_Two')


            Then, to run module_one.py directly and still enable it to use lib.module_group.module_two to load Module_Two, use something like:



            $ PYTHONPATH="${PWD}:${PYTHONPATH}" python3 lib/module_group/module_one.py
            Loaded Module_Two





            share|improve this answer




























              1














              Set PYTHONPATH to the directory you want to act as your root. For example, if you want to use the following code:



              from lib.module_group.module_one import Module_One


              Then set PYTHONPATH to the directorying containing lib. For example:



              lib/module_group/module_one.py:



              from lib.module_group.module_two import Module_Two


              lib/module_group/module_two.py:



              class Module_Two:
              print('Loaded Module_Two')


              Then, to run module_one.py directly and still enable it to use lib.module_group.module_two to load Module_Two, use something like:



              $ PYTHONPATH="${PWD}:${PYTHONPATH}" python3 lib/module_group/module_one.py
              Loaded Module_Two





              share|improve this answer


























                1












                1








                1







                Set PYTHONPATH to the directory you want to act as your root. For example, if you want to use the following code:



                from lib.module_group.module_one import Module_One


                Then set PYTHONPATH to the directorying containing lib. For example:



                lib/module_group/module_one.py:



                from lib.module_group.module_two import Module_Two


                lib/module_group/module_two.py:



                class Module_Two:
                print('Loaded Module_Two')


                Then, to run module_one.py directly and still enable it to use lib.module_group.module_two to load Module_Two, use something like:



                $ PYTHONPATH="${PWD}:${PYTHONPATH}" python3 lib/module_group/module_one.py
                Loaded Module_Two





                share|improve this answer













                Set PYTHONPATH to the directory you want to act as your root. For example, if you want to use the following code:



                from lib.module_group.module_one import Module_One


                Then set PYTHONPATH to the directorying containing lib. For example:



                lib/module_group/module_one.py:



                from lib.module_group.module_two import Module_Two


                lib/module_group/module_two.py:



                class Module_Two:
                print('Loaded Module_Two')


                Then, to run module_one.py directly and still enable it to use lib.module_group.module_two to load Module_Two, use something like:



                $ PYTHONPATH="${PWD}:${PYTHONPATH}" python3 lib/module_group/module_one.py
                Loaded Module_Two






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 27 '18 at 19:14









                binkibinki

                3,35223055




                3,35223055
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473369%2fpython-sub-folder-modules-not-able-to-import-other-subfolder-modules%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen