How to import properly from the nested folder above?










0















I have the following structure:



/a
/b
module1.py
/c
module2.py


So, from some root folder there are 2 modules:



a/b/module1.py and



c/module2.py



I want to do import of some function of module1 from module2.



All 3 folders have ____init___.py



Both py files have inside:



if __name__ == '__main__':
func()


module2.py has the following import code:



from .. a.b.module1 import func1


If I just run module2.py from a terminal (staying in the root folder):



python -m c.module2.py 


I have the following error:



ValueError: attempted relative import beyond top-level package


How to solve this problem?










share|improve this question


























    0















    I have the following structure:



    /a
    /b
    module1.py
    /c
    module2.py


    So, from some root folder there are 2 modules:



    a/b/module1.py and



    c/module2.py



    I want to do import of some function of module1 from module2.



    All 3 folders have ____init___.py



    Both py files have inside:



    if __name__ == '__main__':
    func()


    module2.py has the following import code:



    from .. a.b.module1 import func1


    If I just run module2.py from a terminal (staying in the root folder):



    python -m c.module2.py 


    I have the following error:



    ValueError: attempted relative import beyond top-level package


    How to solve this problem?










    share|improve this question
























      0












      0








      0








      I have the following structure:



      /a
      /b
      module1.py
      /c
      module2.py


      So, from some root folder there are 2 modules:



      a/b/module1.py and



      c/module2.py



      I want to do import of some function of module1 from module2.



      All 3 folders have ____init___.py



      Both py files have inside:



      if __name__ == '__main__':
      func()


      module2.py has the following import code:



      from .. a.b.module1 import func1


      If I just run module2.py from a terminal (staying in the root folder):



      python -m c.module2.py 


      I have the following error:



      ValueError: attempted relative import beyond top-level package


      How to solve this problem?










      share|improve this question














      I have the following structure:



      /a
      /b
      module1.py
      /c
      module2.py


      So, from some root folder there are 2 modules:



      a/b/module1.py and



      c/module2.py



      I want to do import of some function of module1 from module2.



      All 3 folders have ____init___.py



      Both py files have inside:



      if __name__ == '__main__':
      func()


      module2.py has the following import code:



      from .. a.b.module1 import func1


      If I just run module2.py from a terminal (staying in the root folder):



      python -m c.module2.py 


      I have the following error:



      ValueError: attempted relative import beyond top-level package


      How to solve this problem?







      python-3.x relative-path






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 1:42









      mimicmimic

      1,21622453




      1,21622453






















          1 Answer
          1






          active

          oldest

          votes


















          1














          This is a common problem with the python import system, but one way to make it more manageable may be to define some imports in another __init__.py in your projects root directory, which will create a separate namespace for your package and all subdirectories.



          The way I setup the directories to reproduce your error is this structure:



          Root directory: /package:



          /package
          /package/__init__.py
          /package/a
          /package/a/__init__.py
          /package/a/b
          /package/a/b/__init__.py
          /package/a/b/module1.py
          /package/c
          /package/c/__init__.py
          /package/c/module2.py


          In package/__init__.py put the top-level module imports:



          from a.b import module1


          and then the only change I made is to module2.py to contain only this function for testing:



          from package import a # import top-level module from root package 

          def func():
          a.b.module1.some_object() # call function of module1 from module2 (this module).

          if __name__ == '__main__':
          func()


          then in the top-level root of the package, from terminal you should be able to run module2.py:



          $ python package/c/module2.py 


          should print out:



          from a.b.module1: some object


          Improvement and refinements can be made to get the exact behavior you desire.



          For reference, I used this answer's suggestions: How to avoid circular imports in Python?






          share|improve this answer























          • Why is it sooooo complicated? Python pretends to be a simple language, but turns out to be a nightmare as soon as you do one small step aside :(

            – mimic
            Nov 13 '18 at 18:08










          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%2f53272566%2fhow-to-import-properly-from-the-nested-folder-above%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














          This is a common problem with the python import system, but one way to make it more manageable may be to define some imports in another __init__.py in your projects root directory, which will create a separate namespace for your package and all subdirectories.



          The way I setup the directories to reproduce your error is this structure:



          Root directory: /package:



          /package
          /package/__init__.py
          /package/a
          /package/a/__init__.py
          /package/a/b
          /package/a/b/__init__.py
          /package/a/b/module1.py
          /package/c
          /package/c/__init__.py
          /package/c/module2.py


          In package/__init__.py put the top-level module imports:



          from a.b import module1


          and then the only change I made is to module2.py to contain only this function for testing:



          from package import a # import top-level module from root package 

          def func():
          a.b.module1.some_object() # call function of module1 from module2 (this module).

          if __name__ == '__main__':
          func()


          then in the top-level root of the package, from terminal you should be able to run module2.py:



          $ python package/c/module2.py 


          should print out:



          from a.b.module1: some object


          Improvement and refinements can be made to get the exact behavior you desire.



          For reference, I used this answer's suggestions: How to avoid circular imports in Python?






          share|improve this answer























          • Why is it sooooo complicated? Python pretends to be a simple language, but turns out to be a nightmare as soon as you do one small step aside :(

            – mimic
            Nov 13 '18 at 18:08















          1














          This is a common problem with the python import system, but one way to make it more manageable may be to define some imports in another __init__.py in your projects root directory, which will create a separate namespace for your package and all subdirectories.



          The way I setup the directories to reproduce your error is this structure:



          Root directory: /package:



          /package
          /package/__init__.py
          /package/a
          /package/a/__init__.py
          /package/a/b
          /package/a/b/__init__.py
          /package/a/b/module1.py
          /package/c
          /package/c/__init__.py
          /package/c/module2.py


          In package/__init__.py put the top-level module imports:



          from a.b import module1


          and then the only change I made is to module2.py to contain only this function for testing:



          from package import a # import top-level module from root package 

          def func():
          a.b.module1.some_object() # call function of module1 from module2 (this module).

          if __name__ == '__main__':
          func()


          then in the top-level root of the package, from terminal you should be able to run module2.py:



          $ python package/c/module2.py 


          should print out:



          from a.b.module1: some object


          Improvement and refinements can be made to get the exact behavior you desire.



          For reference, I used this answer's suggestions: How to avoid circular imports in Python?






          share|improve this answer























          • Why is it sooooo complicated? Python pretends to be a simple language, but turns out to be a nightmare as soon as you do one small step aside :(

            – mimic
            Nov 13 '18 at 18:08













          1












          1








          1







          This is a common problem with the python import system, but one way to make it more manageable may be to define some imports in another __init__.py in your projects root directory, which will create a separate namespace for your package and all subdirectories.



          The way I setup the directories to reproduce your error is this structure:



          Root directory: /package:



          /package
          /package/__init__.py
          /package/a
          /package/a/__init__.py
          /package/a/b
          /package/a/b/__init__.py
          /package/a/b/module1.py
          /package/c
          /package/c/__init__.py
          /package/c/module2.py


          In package/__init__.py put the top-level module imports:



          from a.b import module1


          and then the only change I made is to module2.py to contain only this function for testing:



          from package import a # import top-level module from root package 

          def func():
          a.b.module1.some_object() # call function of module1 from module2 (this module).

          if __name__ == '__main__':
          func()


          then in the top-level root of the package, from terminal you should be able to run module2.py:



          $ python package/c/module2.py 


          should print out:



          from a.b.module1: some object


          Improvement and refinements can be made to get the exact behavior you desire.



          For reference, I used this answer's suggestions: How to avoid circular imports in Python?






          share|improve this answer













          This is a common problem with the python import system, but one way to make it more manageable may be to define some imports in another __init__.py in your projects root directory, which will create a separate namespace for your package and all subdirectories.



          The way I setup the directories to reproduce your error is this structure:



          Root directory: /package:



          /package
          /package/__init__.py
          /package/a
          /package/a/__init__.py
          /package/a/b
          /package/a/b/__init__.py
          /package/a/b/module1.py
          /package/c
          /package/c/__init__.py
          /package/c/module2.py


          In package/__init__.py put the top-level module imports:



          from a.b import module1


          and then the only change I made is to module2.py to contain only this function for testing:



          from package import a # import top-level module from root package 

          def func():
          a.b.module1.some_object() # call function of module1 from module2 (this module).

          if __name__ == '__main__':
          func()


          then in the top-level root of the package, from terminal you should be able to run module2.py:



          $ python package/c/module2.py 


          should print out:



          from a.b.module1: some object


          Improvement and refinements can be made to get the exact behavior you desire.



          For reference, I used this answer's suggestions: How to avoid circular imports in Python?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 2:25









          davedwardsdavedwards

          5,51621132




          5,51621132












          • Why is it sooooo complicated? Python pretends to be a simple language, but turns out to be a nightmare as soon as you do one small step aside :(

            – mimic
            Nov 13 '18 at 18:08

















          • Why is it sooooo complicated? Python pretends to be a simple language, but turns out to be a nightmare as soon as you do one small step aside :(

            – mimic
            Nov 13 '18 at 18:08
















          Why is it sooooo complicated? Python pretends to be a simple language, but turns out to be a nightmare as soon as you do one small step aside :(

          – mimic
          Nov 13 '18 at 18:08





          Why is it sooooo complicated? Python pretends to be a simple language, but turns out to be a nightmare as soon as you do one small step aside :(

          – mimic
          Nov 13 '18 at 18:08

















          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%2f53272566%2fhow-to-import-properly-from-the-nested-folder-above%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo