File importing on python project












1















I want to have following project directory structure in python:



project
generic.py
subdir1
file1.py
subdir2
file2.py
subdir3
file3.py


within file1.py I want to import generic.py from a root dir of project.



import generic


Unfortunatelly I got error when a try to execute "file1.py"



root:~/project/sidir1# python file1.py
Traceback (most recent call last):
File "file1.py", line 7, in <module>
import generic
ImportError: No module named generic


I want to execute individual file*.py and have generic.py included.



How to correctly include it under such a drirectory structure?










share|improve this question



























    1















    I want to have following project directory structure in python:



    project
    generic.py
    subdir1
    file1.py
    subdir2
    file2.py
    subdir3
    file3.py


    within file1.py I want to import generic.py from a root dir of project.



    import generic


    Unfortunatelly I got error when a try to execute "file1.py"



    root:~/project/sidir1# python file1.py
    Traceback (most recent call last):
    File "file1.py", line 7, in <module>
    import generic
    ImportError: No module named generic


    I want to execute individual file*.py and have generic.py included.



    How to correctly include it under such a drirectory structure?










    share|improve this question

























      1












      1








      1


      1






      I want to have following project directory structure in python:



      project
      generic.py
      subdir1
      file1.py
      subdir2
      file2.py
      subdir3
      file3.py


      within file1.py I want to import generic.py from a root dir of project.



      import generic


      Unfortunatelly I got error when a try to execute "file1.py"



      root:~/project/sidir1# python file1.py
      Traceback (most recent call last):
      File "file1.py", line 7, in <module>
      import generic
      ImportError: No module named generic


      I want to execute individual file*.py and have generic.py included.



      How to correctly include it under such a drirectory structure?










      share|improve this question














      I want to have following project directory structure in python:



      project
      generic.py
      subdir1
      file1.py
      subdir2
      file2.py
      subdir3
      file3.py


      within file1.py I want to import generic.py from a root dir of project.



      import generic


      Unfortunatelly I got error when a try to execute "file1.py"



      root:~/project/sidir1# python file1.py
      Traceback (most recent call last):
      File "file1.py", line 7, in <module>
      import generic
      ImportError: No module named generic


      I want to execute individual file*.py and have generic.py included.



      How to correctly include it under such a drirectory structure?







      python






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 15:05









      user3428154user3428154

      759




      759
























          4 Answers
          4






          active

          oldest

          votes


















          1














          You can make your python project a package by making __init__.py in the root directory.



          You can then use relative imports, ex: from .. import generic






          share|improve this answer


























          • I added /__init__.py and within my file1.py did "import ..generic" but still syntax error

            – user3428154
            Nov 14 '18 at 15:40













          • @user3428154 try my edit

            – Qwerty
            Nov 14 '18 at 15:42



















          0














          Have a Look here:
          How to import a Python class that is in a directory above? How to import a Python class that is in a directory above?






          share|improve this answer
























          • using from .. import generic I got error ValueError: Attempted relative import in non-package by following the recommendation

            – user3428154
            Nov 14 '18 at 19:43





















          0














          It's cause of Python path, when you use import, python will try to find in every directory in sys.path if module with name given exist, if he don't found, he raise.



          For fix your problem, just add parent directory to your python path



          import sys
          sys.path.append("..")





          share|improve this answer
























          • at which directory should I need to reside when adding sys.path.append("..")?

            – user3428154
            Nov 14 '18 at 15:43













          • your working dir is not important, it's relative to the directory of the file that is launched

            – iElden
            Nov 14 '18 at 15:46



















          0














          I would encourage you to instead wrap generic.py within a package along with the other submodules. Importing from root is usually a good recipe to later needing to debug imports that no longer work the way they should. Relative imports within a package can also become problematic.



          Just build a structure like this:



          project
          my_package
          generic.py
          __init__.py
          subdir1
          file1.py
          __init__.py
          subdir2
          file2.py
          __init__.py
          subdir3
          file3.py
          __init__.py


          And now in file*.py do: from my_package import generic



          Now on the project level put a main.py or similar script where you use the functions that are available in my_package.






          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%2f53303202%2ffile-importing-on-python-project%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You can make your python project a package by making __init__.py in the root directory.



            You can then use relative imports, ex: from .. import generic






            share|improve this answer


























            • I added /__init__.py and within my file1.py did "import ..generic" but still syntax error

              – user3428154
              Nov 14 '18 at 15:40













            • @user3428154 try my edit

              – Qwerty
              Nov 14 '18 at 15:42
















            1














            You can make your python project a package by making __init__.py in the root directory.



            You can then use relative imports, ex: from .. import generic






            share|improve this answer


























            • I added /__init__.py and within my file1.py did "import ..generic" but still syntax error

              – user3428154
              Nov 14 '18 at 15:40













            • @user3428154 try my edit

              – Qwerty
              Nov 14 '18 at 15:42














            1












            1








            1







            You can make your python project a package by making __init__.py in the root directory.



            You can then use relative imports, ex: from .. import generic






            share|improve this answer















            You can make your python project a package by making __init__.py in the root directory.



            You can then use relative imports, ex: from .. import generic







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 14 '18 at 15:42

























            answered Nov 14 '18 at 15:07









            QwertyQwerty

            835619




            835619













            • I added /__init__.py and within my file1.py did "import ..generic" but still syntax error

              – user3428154
              Nov 14 '18 at 15:40













            • @user3428154 try my edit

              – Qwerty
              Nov 14 '18 at 15:42



















            • I added /__init__.py and within my file1.py did "import ..generic" but still syntax error

              – user3428154
              Nov 14 '18 at 15:40













            • @user3428154 try my edit

              – Qwerty
              Nov 14 '18 at 15:42

















            I added /__init__.py and within my file1.py did "import ..generic" but still syntax error

            – user3428154
            Nov 14 '18 at 15:40







            I added /__init__.py and within my file1.py did "import ..generic" but still syntax error

            – user3428154
            Nov 14 '18 at 15:40















            @user3428154 try my edit

            – Qwerty
            Nov 14 '18 at 15:42





            @user3428154 try my edit

            – Qwerty
            Nov 14 '18 at 15:42













            0














            Have a Look here:
            How to import a Python class that is in a directory above? How to import a Python class that is in a directory above?






            share|improve this answer
























            • using from .. import generic I got error ValueError: Attempted relative import in non-package by following the recommendation

              – user3428154
              Nov 14 '18 at 19:43


















            0














            Have a Look here:
            How to import a Python class that is in a directory above? How to import a Python class that is in a directory above?






            share|improve this answer
























            • using from .. import generic I got error ValueError: Attempted relative import in non-package by following the recommendation

              – user3428154
              Nov 14 '18 at 19:43
















            0












            0








            0







            Have a Look here:
            How to import a Python class that is in a directory above? How to import a Python class that is in a directory above?






            share|improve this answer













            Have a Look here:
            How to import a Python class that is in a directory above? How to import a Python class that is in a directory above?







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 15:09









            FlowFlow

            358




            358













            • using from .. import generic I got error ValueError: Attempted relative import in non-package by following the recommendation

              – user3428154
              Nov 14 '18 at 19:43





















            • using from .. import generic I got error ValueError: Attempted relative import in non-package by following the recommendation

              – user3428154
              Nov 14 '18 at 19:43



















            using from .. import generic I got error ValueError: Attempted relative import in non-package by following the recommendation

            – user3428154
            Nov 14 '18 at 19:43







            using from .. import generic I got error ValueError: Attempted relative import in non-package by following the recommendation

            – user3428154
            Nov 14 '18 at 19:43













            0














            It's cause of Python path, when you use import, python will try to find in every directory in sys.path if module with name given exist, if he don't found, he raise.



            For fix your problem, just add parent directory to your python path



            import sys
            sys.path.append("..")





            share|improve this answer
























            • at which directory should I need to reside when adding sys.path.append("..")?

              – user3428154
              Nov 14 '18 at 15:43













            • your working dir is not important, it's relative to the directory of the file that is launched

              – iElden
              Nov 14 '18 at 15:46
















            0














            It's cause of Python path, when you use import, python will try to find in every directory in sys.path if module with name given exist, if he don't found, he raise.



            For fix your problem, just add parent directory to your python path



            import sys
            sys.path.append("..")





            share|improve this answer
























            • at which directory should I need to reside when adding sys.path.append("..")?

              – user3428154
              Nov 14 '18 at 15:43













            • your working dir is not important, it's relative to the directory of the file that is launched

              – iElden
              Nov 14 '18 at 15:46














            0












            0








            0







            It's cause of Python path, when you use import, python will try to find in every directory in sys.path if module with name given exist, if he don't found, he raise.



            For fix your problem, just add parent directory to your python path



            import sys
            sys.path.append("..")





            share|improve this answer













            It's cause of Python path, when you use import, python will try to find in every directory in sys.path if module with name given exist, if he don't found, he raise.



            For fix your problem, just add parent directory to your python path



            import sys
            sys.path.append("..")






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 15:12









            iEldeniElden

            684517




            684517













            • at which directory should I need to reside when adding sys.path.append("..")?

              – user3428154
              Nov 14 '18 at 15:43













            • your working dir is not important, it's relative to the directory of the file that is launched

              – iElden
              Nov 14 '18 at 15:46



















            • at which directory should I need to reside when adding sys.path.append("..")?

              – user3428154
              Nov 14 '18 at 15:43













            • your working dir is not important, it's relative to the directory of the file that is launched

              – iElden
              Nov 14 '18 at 15:46

















            at which directory should I need to reside when adding sys.path.append("..")?

            – user3428154
            Nov 14 '18 at 15:43







            at which directory should I need to reside when adding sys.path.append("..")?

            – user3428154
            Nov 14 '18 at 15:43















            your working dir is not important, it's relative to the directory of the file that is launched

            – iElden
            Nov 14 '18 at 15:46





            your working dir is not important, it's relative to the directory of the file that is launched

            – iElden
            Nov 14 '18 at 15:46











            0














            I would encourage you to instead wrap generic.py within a package along with the other submodules. Importing from root is usually a good recipe to later needing to debug imports that no longer work the way they should. Relative imports within a package can also become problematic.



            Just build a structure like this:



            project
            my_package
            generic.py
            __init__.py
            subdir1
            file1.py
            __init__.py
            subdir2
            file2.py
            __init__.py
            subdir3
            file3.py
            __init__.py


            And now in file*.py do: from my_package import generic



            Now on the project level put a main.py or similar script where you use the functions that are available in my_package.






            share|improve this answer




























              0














              I would encourage you to instead wrap generic.py within a package along with the other submodules. Importing from root is usually a good recipe to later needing to debug imports that no longer work the way they should. Relative imports within a package can also become problematic.



              Just build a structure like this:



              project
              my_package
              generic.py
              __init__.py
              subdir1
              file1.py
              __init__.py
              subdir2
              file2.py
              __init__.py
              subdir3
              file3.py
              __init__.py


              And now in file*.py do: from my_package import generic



              Now on the project level put a main.py or similar script where you use the functions that are available in my_package.






              share|improve this answer


























                0












                0








                0







                I would encourage you to instead wrap generic.py within a package along with the other submodules. Importing from root is usually a good recipe to later needing to debug imports that no longer work the way they should. Relative imports within a package can also become problematic.



                Just build a structure like this:



                project
                my_package
                generic.py
                __init__.py
                subdir1
                file1.py
                __init__.py
                subdir2
                file2.py
                __init__.py
                subdir3
                file3.py
                __init__.py


                And now in file*.py do: from my_package import generic



                Now on the project level put a main.py or similar script where you use the functions that are available in my_package.






                share|improve this answer













                I would encourage you to instead wrap generic.py within a package along with the other submodules. Importing from root is usually a good recipe to later needing to debug imports that no longer work the way they should. Relative imports within a package can also become problematic.



                Just build a structure like this:



                project
                my_package
                generic.py
                __init__.py
                subdir1
                file1.py
                __init__.py
                subdir2
                file2.py
                __init__.py
                subdir3
                file3.py
                __init__.py


                And now in file*.py do: from my_package import generic



                Now on the project level put a main.py or similar script where you use the functions that are available in my_package.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 15:16









                KarlKarl

                2,40143055




                2,40143055






























                    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%2f53303202%2ffile-importing-on-python-project%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

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues