FAQ
I'm converting some tests written earlier for Unittest and am having
trouble with import statements when running them with Pytest.


As an example, in the older code the module create.py in directory lib
might be imported as


import lib.create
>


but this syntax usually raises errors when I run the code with Pytest.


I'd appreciate any advice. Details and minimal examples below.


1. I am in the directory temp:


temp/
     lib/
         __init__.py
         create.py
     tests/
         test_caller.py






2. File __init__.py is empty. The other two files are as follows:


# test_caller.py
import lib.create

def test_show():
assert True

and


# create.py
def create():
pass





3. Running Pytest from within temp typically generates an ImportError:


$ py.test tests
============================= test session starts
==============================
platform darwin -- Python 2.7.6 -- py-1.4.26 -- pytest-2.7.0
rootdir: .../temp/tests, inifile:
collected 0 items / 1 errors

==================================== ERRORS
====================================
_______________________ ERROR collecting test_caller.py
________________________
tests/test_caller.py:3: in <module>
import lib.create
E ImportError: No module named lib.create
=========================== 1 error in 0.01 seconds
============================





4. I have tried updating the sys.path list in test_caller.py:


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

or


sys.path.extend(['..', 'lib'])
>


but the result is the same.


Thanks for any suggestions as to how to deal with this.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20150724/dcd34cbf/attachment.html>

Search Discussions

  • Dpb dpb at Jul 24, 2015 at 6:20 pm
    I've posted a MWE to https://github.com/dpbmm/pytest_import_question for
    ease of study.


    Curiously, the example works with nose. What should I do to make it work
    with Pytest?


    - dpb


    On Fri, Jul 24, 2015 at 1:16 PM, dpb dpb wrote:

    I'm converting some tests written earlier for Unittest and am having
    trouble with import statements when running them with Pytest.

    As an example, in the older code the module create.py in directory lib
    might be imported as

    import lib.create
    but this syntax usually raises errors when I run the code with Pytest.

    I'd appreciate any advice. Details and minimal examples below.

    1. I am in the directory temp:

    temp/
    lib/
    __init__.py
    create.py
    tests/
    test_caller.py



    2. File __init__.py is empty. The other two files are as follows:

    # test_caller.py
    import lib.create

    def test_show():
    assert True
    and

    # create.py
    def create():
    pass


    3. Running Pytest from within temp typically generates an ImportError:

    $ py.test tests
    ============================= test session starts
    ==============================
    platform darwin -- Python 2.7.6 -- py-1.4.26 -- pytest-2.7.0
    rootdir: .../temp/tests, inifile:
    collected 0 items / 1 errors

    ==================================== ERRORS
    ====================================
    _______________________ ERROR collecting test_caller.py
    ________________________
    tests/test_caller.py:3: in <module>
    import lib.create
    E ImportError: No module named lib.create
    =========================== 1 error in 0.01 seconds
    ============================


    4. I have tried updating the sys.path list in test_caller.py:

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

    sys.path.extend(['..', 'lib'])
    but the result is the same.

    Thanks for any suggestions as to how to deal with this.
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20150724/b5e45708/attachment-0001.html>
  • Dpb dpb at Jul 24, 2015 at 6:54 pm
    Ah, the solution is to add a setup.py file and then install as "editable"
    with pip. Details added to the README. - dpb
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20150724/9fd3e8d4/attachment.html>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppytest-dev @
categoriespython
postedJul 24, '15 at 5:16p
activeJul 24, '15 at 6:54p
posts3
users1
websitepython.org

1 user in discussion

Dpb dpb: 3 posts

People

Translate

site design / logo © 2018 Grokbase