Package Imports in Python












3















I am pretty new to Python, but I encountered this problem when someone tried recommending me to use packages. My directory (not actual names but just for example's sake) are as follows:



Main_Folder
- First_folder
__init__.py
first_file.py
- Second_folder
__init__.py
second_file.py
__init__.py
third_file.py


I want to use some functions I created on first_file inside second_file so I wrote both (on different times, not same):



from .first_folder import first_file
from Main_Folder.first_folder import first_file


And I get errors like:



<from first import>
ModuleNotFoundError: No module named '__main__.first_file'; '__main__' is not a package
<from second import>
ModuleNotFoundError: No module named 'Main_Folder'


However, when I do an import for third_file to any of the files inside using, it WORKS:



from First_folder.first_file import some_function


So I was just wondering if i was doing something wrong.
I know there are lots of questions like this existing and I already looked but i cannot get anything to work.. And I am new to Python too...



Update:
I ran both codes using their full absolute path










share|improve this question




















  • 1





    It probably depends on the directory you are in while executing the code

    – Khalil Al Hooti
    Nov 24 '18 at 23:55











  • What exactly were you running and from which directory? Which version of Python are you using? PEP 328 gives you a hint about the problem, it seems that __main__ is set as the top level module (see also this question). However I don't see how you get this with .. relative import since you should get ValueError: attempted relative import beyond top-level package instead.

    – a_guest
    Nov 25 '18 at 0:26











  • If you are sitting in Main_Folder though and using from .first_folder import first_file (note the single .) in third_file.py and running python third_file.py then this perfectly explains the reported errors (as documented by PEP 328).

    – a_guest
    Nov 25 '18 at 0:28













  • Thanks for the answers, I was running second_file.py. And also, you are right, it's only a single dot (.) and I get that error from using two dots.

    – ashiii
    Nov 25 '18 at 6:10













  • Also I ran my code on both Main_Folder and also at Second_Folder but still getting the same error..

    – ashiii
    Nov 25 '18 at 6:12
















3















I am pretty new to Python, but I encountered this problem when someone tried recommending me to use packages. My directory (not actual names but just for example's sake) are as follows:



Main_Folder
- First_folder
__init__.py
first_file.py
- Second_folder
__init__.py
second_file.py
__init__.py
third_file.py


I want to use some functions I created on first_file inside second_file so I wrote both (on different times, not same):



from .first_folder import first_file
from Main_Folder.first_folder import first_file


And I get errors like:



<from first import>
ModuleNotFoundError: No module named '__main__.first_file'; '__main__' is not a package
<from second import>
ModuleNotFoundError: No module named 'Main_Folder'


However, when I do an import for third_file to any of the files inside using, it WORKS:



from First_folder.first_file import some_function


So I was just wondering if i was doing something wrong.
I know there are lots of questions like this existing and I already looked but i cannot get anything to work.. And I am new to Python too...



Update:
I ran both codes using their full absolute path










share|improve this question




















  • 1





    It probably depends on the directory you are in while executing the code

    – Khalil Al Hooti
    Nov 24 '18 at 23:55











  • What exactly were you running and from which directory? Which version of Python are you using? PEP 328 gives you a hint about the problem, it seems that __main__ is set as the top level module (see also this question). However I don't see how you get this with .. relative import since you should get ValueError: attempted relative import beyond top-level package instead.

    – a_guest
    Nov 25 '18 at 0:26











  • If you are sitting in Main_Folder though and using from .first_folder import first_file (note the single .) in third_file.py and running python third_file.py then this perfectly explains the reported errors (as documented by PEP 328).

    – a_guest
    Nov 25 '18 at 0:28













  • Thanks for the answers, I was running second_file.py. And also, you are right, it's only a single dot (.) and I get that error from using two dots.

    – ashiii
    Nov 25 '18 at 6:10













  • Also I ran my code on both Main_Folder and also at Second_Folder but still getting the same error..

    – ashiii
    Nov 25 '18 at 6:12














3












3








3


1






I am pretty new to Python, but I encountered this problem when someone tried recommending me to use packages. My directory (not actual names but just for example's sake) are as follows:



Main_Folder
- First_folder
__init__.py
first_file.py
- Second_folder
__init__.py
second_file.py
__init__.py
third_file.py


I want to use some functions I created on first_file inside second_file so I wrote both (on different times, not same):



from .first_folder import first_file
from Main_Folder.first_folder import first_file


And I get errors like:



<from first import>
ModuleNotFoundError: No module named '__main__.first_file'; '__main__' is not a package
<from second import>
ModuleNotFoundError: No module named 'Main_Folder'


However, when I do an import for third_file to any of the files inside using, it WORKS:



from First_folder.first_file import some_function


So I was just wondering if i was doing something wrong.
I know there are lots of questions like this existing and I already looked but i cannot get anything to work.. And I am new to Python too...



Update:
I ran both codes using their full absolute path










share|improve this question
















I am pretty new to Python, but I encountered this problem when someone tried recommending me to use packages. My directory (not actual names but just for example's sake) are as follows:



Main_Folder
- First_folder
__init__.py
first_file.py
- Second_folder
__init__.py
second_file.py
__init__.py
third_file.py


I want to use some functions I created on first_file inside second_file so I wrote both (on different times, not same):



from .first_folder import first_file
from Main_Folder.first_folder import first_file


And I get errors like:



<from first import>
ModuleNotFoundError: No module named '__main__.first_file'; '__main__' is not a package
<from second import>
ModuleNotFoundError: No module named 'Main_Folder'


However, when I do an import for third_file to any of the files inside using, it WORKS:



from First_folder.first_file import some_function


So I was just wondering if i was doing something wrong.
I know there are lots of questions like this existing and I already looked but i cannot get anything to work.. And I am new to Python too...



Update:
I ran both codes using their full absolute path







python python-import






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 0:09







ashiii

















asked Nov 24 '18 at 23:41









ashiiiashiii

162




162








  • 1





    It probably depends on the directory you are in while executing the code

    – Khalil Al Hooti
    Nov 24 '18 at 23:55











  • What exactly were you running and from which directory? Which version of Python are you using? PEP 328 gives you a hint about the problem, it seems that __main__ is set as the top level module (see also this question). However I don't see how you get this with .. relative import since you should get ValueError: attempted relative import beyond top-level package instead.

    – a_guest
    Nov 25 '18 at 0:26











  • If you are sitting in Main_Folder though and using from .first_folder import first_file (note the single .) in third_file.py and running python third_file.py then this perfectly explains the reported errors (as documented by PEP 328).

    – a_guest
    Nov 25 '18 at 0:28













  • Thanks for the answers, I was running second_file.py. And also, you are right, it's only a single dot (.) and I get that error from using two dots.

    – ashiii
    Nov 25 '18 at 6:10













  • Also I ran my code on both Main_Folder and also at Second_Folder but still getting the same error..

    – ashiii
    Nov 25 '18 at 6:12














  • 1





    It probably depends on the directory you are in while executing the code

    – Khalil Al Hooti
    Nov 24 '18 at 23:55











  • What exactly were you running and from which directory? Which version of Python are you using? PEP 328 gives you a hint about the problem, it seems that __main__ is set as the top level module (see also this question). However I don't see how you get this with .. relative import since you should get ValueError: attempted relative import beyond top-level package instead.

    – a_guest
    Nov 25 '18 at 0:26











  • If you are sitting in Main_Folder though and using from .first_folder import first_file (note the single .) in third_file.py and running python third_file.py then this perfectly explains the reported errors (as documented by PEP 328).

    – a_guest
    Nov 25 '18 at 0:28













  • Thanks for the answers, I was running second_file.py. And also, you are right, it's only a single dot (.) and I get that error from using two dots.

    – ashiii
    Nov 25 '18 at 6:10













  • Also I ran my code on both Main_Folder and also at Second_Folder but still getting the same error..

    – ashiii
    Nov 25 '18 at 6:12








1




1





It probably depends on the directory you are in while executing the code

– Khalil Al Hooti
Nov 24 '18 at 23:55





It probably depends on the directory you are in while executing the code

– Khalil Al Hooti
Nov 24 '18 at 23:55













What exactly were you running and from which directory? Which version of Python are you using? PEP 328 gives you a hint about the problem, it seems that __main__ is set as the top level module (see also this question). However I don't see how you get this with .. relative import since you should get ValueError: attempted relative import beyond top-level package instead.

– a_guest
Nov 25 '18 at 0:26





What exactly were you running and from which directory? Which version of Python are you using? PEP 328 gives you a hint about the problem, it seems that __main__ is set as the top level module (see also this question). However I don't see how you get this with .. relative import since you should get ValueError: attempted relative import beyond top-level package instead.

– a_guest
Nov 25 '18 at 0:26













If you are sitting in Main_Folder though and using from .first_folder import first_file (note the single .) in third_file.py and running python third_file.py then this perfectly explains the reported errors (as documented by PEP 328).

– a_guest
Nov 25 '18 at 0:28







If you are sitting in Main_Folder though and using from .first_folder import first_file (note the single .) in third_file.py and running python third_file.py then this perfectly explains the reported errors (as documented by PEP 328).

– a_guest
Nov 25 '18 at 0:28















Thanks for the answers, I was running second_file.py. And also, you are right, it's only a single dot (.) and I get that error from using two dots.

– ashiii
Nov 25 '18 at 6:10







Thanks for the answers, I was running second_file.py. And also, you are right, it's only a single dot (.) and I get that error from using two dots.

– ashiii
Nov 25 '18 at 6:10















Also I ran my code on both Main_Folder and also at Second_Folder but still getting the same error..

– ashiii
Nov 25 '18 at 6:12





Also I ran my code on both Main_Folder and also at Second_Folder but still getting the same error..

– ashiii
Nov 25 '18 at 6:12












1 Answer
1






active

oldest

votes


















0














One way to make this work is to add the parent path to the python path as follows:



import sys
sys.path
sys.path.append('..')


then you should be able to import normally






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%2f53463381%2fpackage-imports-in-python%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









    0














    One way to make this work is to add the parent path to the python path as follows:



    import sys
    sys.path
    sys.path.append('..')


    then you should be able to import normally






    share|improve this answer




























      0














      One way to make this work is to add the parent path to the python path as follows:



      import sys
      sys.path
      sys.path.append('..')


      then you should be able to import normally






      share|improve this answer


























        0












        0








        0







        One way to make this work is to add the parent path to the python path as follows:



        import sys
        sys.path
        sys.path.append('..')


        then you should be able to import normally






        share|improve this answer













        One way to make this work is to add the parent path to the python path as follows:



        import sys
        sys.path
        sys.path.append('..')


        then you should be able to import normally







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 0:22









        Pedro TorresPedro Torres

        703413




        703413
































            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%2f53463381%2fpackage-imports-in-python%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python