Folder structure and import problems with pytest












2















Imagine a simple folder structure:



my_folder/
__init__.py
funcs.py
tests/
test_funcs.py


funcs.py:



def f():
return 2


__init__.py:



from funcs import f


test_funcs.py:



from funcs import f

def test_f():
assert f() == 2


It's one of the suggested ways in the documentation:
https://pytest.readthedocs.io/en/reorganize-docs/new-docs/user/directory_structure.html



But when I run pytest from my_folder:



tests/test_funcs.py:1: in <module>
from funcs import f
E ModuleNotFoundError: No module named 'funcs'


It's strange because I would have thought that pytest sets the path from where it's been running so those kinds of errors don't come up without dealing with it manually.



The documentation doesn't gave any indication about this either... They're just saying :




Typically you can run tests by pointing to test directories or modules:



pytest tests/test_appmodule.py      # for external test dirs
pytest src/tests/test_appmodule.py # for inlined test dirs
pytest src # run tests in all below test directories
pytest # run all tests below current dir



What am I missing?










share|improve this question

























  • your __init__.py you should have from .funcs import f -- you are making a package-relative import

    – Anthony Sottile
    Oct 11 '18 at 20:36











  • @AnthonySottile I considered that solution, as well. Unfortunately, running pytest with that modification still yields the same error: E ModuleNotFoundError: No module named 'funcs'

    – haxtar
    Oct 11 '18 at 20:49






  • 1





    oh also your test file should have from my_folder.funcs import f -- $ python -m pytest -q my_folder ==> 1 passed in 0.01 seconds

    – Anthony Sottile
    Oct 11 '18 at 21:07













  • That works! (when it is run outside of my_folder). But running pytest within my_folder still fails

    – haxtar
    Oct 11 '18 at 21:19













  • This proves that the documentation is missing something! You're even providing another way to run the tests! The doc just says pytest...

    – Yahya Abou Imran
    Oct 11 '18 at 21:57
















2















Imagine a simple folder structure:



my_folder/
__init__.py
funcs.py
tests/
test_funcs.py


funcs.py:



def f():
return 2


__init__.py:



from funcs import f


test_funcs.py:



from funcs import f

def test_f():
assert f() == 2


It's one of the suggested ways in the documentation:
https://pytest.readthedocs.io/en/reorganize-docs/new-docs/user/directory_structure.html



But when I run pytest from my_folder:



tests/test_funcs.py:1: in <module>
from funcs import f
E ModuleNotFoundError: No module named 'funcs'


It's strange because I would have thought that pytest sets the path from where it's been running so those kinds of errors don't come up without dealing with it manually.



The documentation doesn't gave any indication about this either... They're just saying :




Typically you can run tests by pointing to test directories or modules:



pytest tests/test_appmodule.py      # for external test dirs
pytest src/tests/test_appmodule.py # for inlined test dirs
pytest src # run tests in all below test directories
pytest # run all tests below current dir



What am I missing?










share|improve this question

























  • your __init__.py you should have from .funcs import f -- you are making a package-relative import

    – Anthony Sottile
    Oct 11 '18 at 20:36











  • @AnthonySottile I considered that solution, as well. Unfortunately, running pytest with that modification still yields the same error: E ModuleNotFoundError: No module named 'funcs'

    – haxtar
    Oct 11 '18 at 20:49






  • 1





    oh also your test file should have from my_folder.funcs import f -- $ python -m pytest -q my_folder ==> 1 passed in 0.01 seconds

    – Anthony Sottile
    Oct 11 '18 at 21:07













  • That works! (when it is run outside of my_folder). But running pytest within my_folder still fails

    – haxtar
    Oct 11 '18 at 21:19













  • This proves that the documentation is missing something! You're even providing another way to run the tests! The doc just says pytest...

    – Yahya Abou Imran
    Oct 11 '18 at 21:57














2












2








2








Imagine a simple folder structure:



my_folder/
__init__.py
funcs.py
tests/
test_funcs.py


funcs.py:



def f():
return 2


__init__.py:



from funcs import f


test_funcs.py:



from funcs import f

def test_f():
assert f() == 2


It's one of the suggested ways in the documentation:
https://pytest.readthedocs.io/en/reorganize-docs/new-docs/user/directory_structure.html



But when I run pytest from my_folder:



tests/test_funcs.py:1: in <module>
from funcs import f
E ModuleNotFoundError: No module named 'funcs'


It's strange because I would have thought that pytest sets the path from where it's been running so those kinds of errors don't come up without dealing with it manually.



The documentation doesn't gave any indication about this either... They're just saying :




Typically you can run tests by pointing to test directories or modules:



pytest tests/test_appmodule.py      # for external test dirs
pytest src/tests/test_appmodule.py # for inlined test dirs
pytest src # run tests in all below test directories
pytest # run all tests below current dir



What am I missing?










share|improve this question
















Imagine a simple folder structure:



my_folder/
__init__.py
funcs.py
tests/
test_funcs.py


funcs.py:



def f():
return 2


__init__.py:



from funcs import f


test_funcs.py:



from funcs import f

def test_f():
assert f() == 2


It's one of the suggested ways in the documentation:
https://pytest.readthedocs.io/en/reorganize-docs/new-docs/user/directory_structure.html



But when I run pytest from my_folder:



tests/test_funcs.py:1: in <module>
from funcs import f
E ModuleNotFoundError: No module named 'funcs'


It's strange because I would have thought that pytest sets the path from where it's been running so those kinds of errors don't come up without dealing with it manually.



The documentation doesn't gave any indication about this either... They're just saying :




Typically you can run tests by pointing to test directories or modules:



pytest tests/test_appmodule.py      # for external test dirs
pytest src/tests/test_appmodule.py # for inlined test dirs
pytest src # run tests in all below test directories
pytest # run all tests below current dir



What am I missing?







python python-import pytest






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 11 '18 at 0:49







Yahya Abou Imran

















asked Oct 10 '18 at 19:50









Yahya Abou ImranYahya Abou Imran

849




849













  • your __init__.py you should have from .funcs import f -- you are making a package-relative import

    – Anthony Sottile
    Oct 11 '18 at 20:36











  • @AnthonySottile I considered that solution, as well. Unfortunately, running pytest with that modification still yields the same error: E ModuleNotFoundError: No module named 'funcs'

    – haxtar
    Oct 11 '18 at 20:49






  • 1





    oh also your test file should have from my_folder.funcs import f -- $ python -m pytest -q my_folder ==> 1 passed in 0.01 seconds

    – Anthony Sottile
    Oct 11 '18 at 21:07













  • That works! (when it is run outside of my_folder). But running pytest within my_folder still fails

    – haxtar
    Oct 11 '18 at 21:19













  • This proves that the documentation is missing something! You're even providing another way to run the tests! The doc just says pytest...

    – Yahya Abou Imran
    Oct 11 '18 at 21:57



















  • your __init__.py you should have from .funcs import f -- you are making a package-relative import

    – Anthony Sottile
    Oct 11 '18 at 20:36











  • @AnthonySottile I considered that solution, as well. Unfortunately, running pytest with that modification still yields the same error: E ModuleNotFoundError: No module named 'funcs'

    – haxtar
    Oct 11 '18 at 20:49






  • 1





    oh also your test file should have from my_folder.funcs import f -- $ python -m pytest -q my_folder ==> 1 passed in 0.01 seconds

    – Anthony Sottile
    Oct 11 '18 at 21:07













  • That works! (when it is run outside of my_folder). But running pytest within my_folder still fails

    – haxtar
    Oct 11 '18 at 21:19













  • This proves that the documentation is missing something! You're even providing another way to run the tests! The doc just says pytest...

    – Yahya Abou Imran
    Oct 11 '18 at 21:57

















your __init__.py you should have from .funcs import f -- you are making a package-relative import

– Anthony Sottile
Oct 11 '18 at 20:36





your __init__.py you should have from .funcs import f -- you are making a package-relative import

– Anthony Sottile
Oct 11 '18 at 20:36













@AnthonySottile I considered that solution, as well. Unfortunately, running pytest with that modification still yields the same error: E ModuleNotFoundError: No module named 'funcs'

– haxtar
Oct 11 '18 at 20:49





@AnthonySottile I considered that solution, as well. Unfortunately, running pytest with that modification still yields the same error: E ModuleNotFoundError: No module named 'funcs'

– haxtar
Oct 11 '18 at 20:49




1




1





oh also your test file should have from my_folder.funcs import f -- $ python -m pytest -q my_folder ==> 1 passed in 0.01 seconds

– Anthony Sottile
Oct 11 '18 at 21:07







oh also your test file should have from my_folder.funcs import f -- $ python -m pytest -q my_folder ==> 1 passed in 0.01 seconds

– Anthony Sottile
Oct 11 '18 at 21:07















That works! (when it is run outside of my_folder). But running pytest within my_folder still fails

– haxtar
Oct 11 '18 at 21:19







That works! (when it is run outside of my_folder). But running pytest within my_folder still fails

– haxtar
Oct 11 '18 at 21:19















This proves that the documentation is missing something! You're even providing another way to run the tests! The doc just says pytest...

– Yahya Abou Imran
Oct 11 '18 at 21:57





This proves that the documentation is missing something! You're even providing another way to run the tests! The doc just says pytest...

– Yahya Abou Imran
Oct 11 '18 at 21:57












2 Answers
2






active

oldest

votes


















1














Here is a very simple way:




  1. Empty out __init__.py

  2. From a level above my_folder run python -m pytest or python -m pytest tests (but not pytest)


EXPLANATION: Running a module with the -m option will include it in the PYTHONPATH, so everything related to the import statements will be solved smoothly.






share|improve this answer

































    -1














    The test file test_funcs.py must be run from the directory where the module funcs is located in order for the import to be successful.



    As a workaround, you can modify sys.path, which determines the interpeter's search path for modules.



    test_funcs.py:



    import sys
    sys.path.append('/Users/Yahya/Desktop/my_folder')

    from funcs import f

    def test_f():
    assert f() == 2





    share|improve this answer


























    • Just want to note: the path "/Users/Yahya/Desktop/my_folder" is just an example. If you would like to determine the absolute path of my_folder, you can open a terminal, cd my_folder, and pwd.

      – haxtar
      Oct 10 '18 at 20:44













    • I know about this workaround, but are you saying that the documentation is wrong, or at the very least obviously incomplete? (see the added quote)

      – Yahya Abou Imran
      Oct 11 '18 at 0:52











    • monkeypatching sys.path is almost never the right way

      – Anthony Sottile
      Oct 11 '18 at 20:34











    • The only other way to be able to use funcs in tests/test_funcs.py is to move __init__.py (as is) from my_folder to tests. Running pytest from my_folder after this transfer will result in a passing test. Unfortunately, though, the documentation specifically advises against it: "You shouldn’t have init.py files in your test directories"

      – haxtar
      Oct 11 '18 at 20:44











    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%2f52747763%2ffolder-structure-and-import-problems-with-pytest%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Here is a very simple way:




    1. Empty out __init__.py

    2. From a level above my_folder run python -m pytest or python -m pytest tests (but not pytest)


    EXPLANATION: Running a module with the -m option will include it in the PYTHONPATH, so everything related to the import statements will be solved smoothly.






    share|improve this answer






























      1














      Here is a very simple way:




      1. Empty out __init__.py

      2. From a level above my_folder run python -m pytest or python -m pytest tests (but not pytest)


      EXPLANATION: Running a module with the -m option will include it in the PYTHONPATH, so everything related to the import statements will be solved smoothly.






      share|improve this answer




























        1












        1








        1







        Here is a very simple way:




        1. Empty out __init__.py

        2. From a level above my_folder run python -m pytest or python -m pytest tests (but not pytest)


        EXPLANATION: Running a module with the -m option will include it in the PYTHONPATH, so everything related to the import statements will be solved smoothly.






        share|improve this answer















        Here is a very simple way:




        1. Empty out __init__.py

        2. From a level above my_folder run python -m pytest or python -m pytest tests (but not pytest)


        EXPLANATION: Running a module with the -m option will include it in the PYTHONPATH, so everything related to the import statements will be solved smoothly.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 10:21

























        answered Oct 11 '18 at 23:08









        Yahya Abou ImranYahya Abou Imran

        849




        849

























            -1














            The test file test_funcs.py must be run from the directory where the module funcs is located in order for the import to be successful.



            As a workaround, you can modify sys.path, which determines the interpeter's search path for modules.



            test_funcs.py:



            import sys
            sys.path.append('/Users/Yahya/Desktop/my_folder')

            from funcs import f

            def test_f():
            assert f() == 2





            share|improve this answer


























            • Just want to note: the path "/Users/Yahya/Desktop/my_folder" is just an example. If you would like to determine the absolute path of my_folder, you can open a terminal, cd my_folder, and pwd.

              – haxtar
              Oct 10 '18 at 20:44













            • I know about this workaround, but are you saying that the documentation is wrong, or at the very least obviously incomplete? (see the added quote)

              – Yahya Abou Imran
              Oct 11 '18 at 0:52











            • monkeypatching sys.path is almost never the right way

              – Anthony Sottile
              Oct 11 '18 at 20:34











            • The only other way to be able to use funcs in tests/test_funcs.py is to move __init__.py (as is) from my_folder to tests. Running pytest from my_folder after this transfer will result in a passing test. Unfortunately, though, the documentation specifically advises against it: "You shouldn’t have init.py files in your test directories"

              – haxtar
              Oct 11 '18 at 20:44
















            -1














            The test file test_funcs.py must be run from the directory where the module funcs is located in order for the import to be successful.



            As a workaround, you can modify sys.path, which determines the interpeter's search path for modules.



            test_funcs.py:



            import sys
            sys.path.append('/Users/Yahya/Desktop/my_folder')

            from funcs import f

            def test_f():
            assert f() == 2





            share|improve this answer


























            • Just want to note: the path "/Users/Yahya/Desktop/my_folder" is just an example. If you would like to determine the absolute path of my_folder, you can open a terminal, cd my_folder, and pwd.

              – haxtar
              Oct 10 '18 at 20:44













            • I know about this workaround, but are you saying that the documentation is wrong, or at the very least obviously incomplete? (see the added quote)

              – Yahya Abou Imran
              Oct 11 '18 at 0:52











            • monkeypatching sys.path is almost never the right way

              – Anthony Sottile
              Oct 11 '18 at 20:34











            • The only other way to be able to use funcs in tests/test_funcs.py is to move __init__.py (as is) from my_folder to tests. Running pytest from my_folder after this transfer will result in a passing test. Unfortunately, though, the documentation specifically advises against it: "You shouldn’t have init.py files in your test directories"

              – haxtar
              Oct 11 '18 at 20:44














            -1












            -1








            -1







            The test file test_funcs.py must be run from the directory where the module funcs is located in order for the import to be successful.



            As a workaround, you can modify sys.path, which determines the interpeter's search path for modules.



            test_funcs.py:



            import sys
            sys.path.append('/Users/Yahya/Desktop/my_folder')

            from funcs import f

            def test_f():
            assert f() == 2





            share|improve this answer















            The test file test_funcs.py must be run from the directory where the module funcs is located in order for the import to be successful.



            As a workaround, you can modify sys.path, which determines the interpeter's search path for modules.



            test_funcs.py:



            import sys
            sys.path.append('/Users/Yahya/Desktop/my_folder')

            from funcs import f

            def test_f():
            assert f() == 2






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 10 '18 at 20:42

























            answered Oct 10 '18 at 20:37









            haxtarhaxtar

            632827




            632827













            • Just want to note: the path "/Users/Yahya/Desktop/my_folder" is just an example. If you would like to determine the absolute path of my_folder, you can open a terminal, cd my_folder, and pwd.

              – haxtar
              Oct 10 '18 at 20:44













            • I know about this workaround, but are you saying that the documentation is wrong, or at the very least obviously incomplete? (see the added quote)

              – Yahya Abou Imran
              Oct 11 '18 at 0:52











            • monkeypatching sys.path is almost never the right way

              – Anthony Sottile
              Oct 11 '18 at 20:34











            • The only other way to be able to use funcs in tests/test_funcs.py is to move __init__.py (as is) from my_folder to tests. Running pytest from my_folder after this transfer will result in a passing test. Unfortunately, though, the documentation specifically advises against it: "You shouldn’t have init.py files in your test directories"

              – haxtar
              Oct 11 '18 at 20:44



















            • Just want to note: the path "/Users/Yahya/Desktop/my_folder" is just an example. If you would like to determine the absolute path of my_folder, you can open a terminal, cd my_folder, and pwd.

              – haxtar
              Oct 10 '18 at 20:44













            • I know about this workaround, but are you saying that the documentation is wrong, or at the very least obviously incomplete? (see the added quote)

              – Yahya Abou Imran
              Oct 11 '18 at 0:52











            • monkeypatching sys.path is almost never the right way

              – Anthony Sottile
              Oct 11 '18 at 20:34











            • The only other way to be able to use funcs in tests/test_funcs.py is to move __init__.py (as is) from my_folder to tests. Running pytest from my_folder after this transfer will result in a passing test. Unfortunately, though, the documentation specifically advises against it: "You shouldn’t have init.py files in your test directories"

              – haxtar
              Oct 11 '18 at 20:44

















            Just want to note: the path "/Users/Yahya/Desktop/my_folder" is just an example. If you would like to determine the absolute path of my_folder, you can open a terminal, cd my_folder, and pwd.

            – haxtar
            Oct 10 '18 at 20:44







            Just want to note: the path "/Users/Yahya/Desktop/my_folder" is just an example. If you would like to determine the absolute path of my_folder, you can open a terminal, cd my_folder, and pwd.

            – haxtar
            Oct 10 '18 at 20:44















            I know about this workaround, but are you saying that the documentation is wrong, or at the very least obviously incomplete? (see the added quote)

            – Yahya Abou Imran
            Oct 11 '18 at 0:52





            I know about this workaround, but are you saying that the documentation is wrong, or at the very least obviously incomplete? (see the added quote)

            – Yahya Abou Imran
            Oct 11 '18 at 0:52













            monkeypatching sys.path is almost never the right way

            – Anthony Sottile
            Oct 11 '18 at 20:34





            monkeypatching sys.path is almost never the right way

            – Anthony Sottile
            Oct 11 '18 at 20:34













            The only other way to be able to use funcs in tests/test_funcs.py is to move __init__.py (as is) from my_folder to tests. Running pytest from my_folder after this transfer will result in a passing test. Unfortunately, though, the documentation specifically advises against it: "You shouldn’t have init.py files in your test directories"

            – haxtar
            Oct 11 '18 at 20:44





            The only other way to be able to use funcs in tests/test_funcs.py is to move __init__.py (as is) from my_folder to tests. Running pytest from my_folder after this transfer will result in a passing test. Unfortunately, though, the documentation specifically advises against it: "You shouldn’t have init.py files in your test directories"

            – haxtar
            Oct 11 '18 at 20:44


















            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%2f52747763%2ffolder-structure-and-import-problems-with-pytest%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

            Costa Masnaga

            Fotorealismo

            Sidney Franklin