4

My directory structure is like so:

├── person
     └── __init__.py
     └── person.py
└── tests
     └── test_person.py
├── setup.py

setup.py:

from setuptools import setup, find_packages

setup(name="person", packages=find_packages())

person.py:

import team


class Person:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname


print("hello")

test_person.py:

from person import Person


def test_person():
    p = Person("john", "smith")
    assert p.fname == "john"

After installing my package:

$ pip install -e .
Obtaining file:///Users/raphattack/PyCharm/person
Installing collected packages: person
  Running setup.py develop for person
Successfully installed person

I am hitting this error:

$ pytest
======================================================================= test session starts ========================================================================
platform darwin -- Python 3.6.5, pytest-3.8.1, py-1.6.0, pluggy-0.7.1
rootdir: /Users/raphattack/PyCharm/person, inifile:
collected 0 items / 1 errors

============================================================================== ERRORS ==============================================================================
______________________________________________________________ ERROR collecting tests/test_person.py _______________________________________________________________
ImportError while importing test module '/Users/raphattack/PyCharm/person/tests/test_person.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_person.py:1: in <module>
    from person import Person
E   ImportError: cannot import name 'Person'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
===================================================================== 1 error in 0.10 seconds ======================================================================

If I modify the import to from person.person import Person then it runs. How can I make it so that I only need from person import Person?

Edit: @hoefling's suggestion works well, but I am hitting a ModuleNotFoundError: No module named 'team' error if my person.py imports team. how can I modify __init__.py so that pytest will pick up the other module?

team.py

class Team:
    def __init__(self, name):
        self.name = name

This works:

$ python3 person/person.py
hello

This doesn't:

$ pytest
======================================================================= test session starts =======================================================================
platform darwin -- Python 3.6.5, pytest-3.8.1, py-1.6.0, pluggy-0.7.1
rootdir: /Users/raphattack/PyCharm/person, inifile:
collected 0 items / 1 errors

============================================================================= ERRORS ==============================================================================
______________________________________________________________ ERROR collecting tests/test_person.py ______________________________________________________________
ImportError while importing test module '/Users/raphattack/PyCharm/person/tests/test_person.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_person.py:1: in <module>
    from person import Person
person/__init__.py:1: in <module>
    from .person import Person
person/person.py:1: in <module>
    import team
E   ModuleNotFoundError: No module named 'team'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
===================================================================== 1 error in 0.11 seconds =====================================================================
raphattack
  • 179
  • 4
  • 12
  • 1
    Add `from .person import Person` to `person/__init__.py` file. – hoefling Oct 03 '18 at 19:23
  • Thanks! Is this the recommended directory structure and import convention or would you modify anything? – raphattack Oct 03 '18 at 19:49
  • It's a common one and there's nothing wrong with it. Still, you may be interested in reading [Good Integration Practices](https://docs.pytest.org/en/latest/goodpractices.html) for examples of another layouts that may or may not suit you better. – hoefling Oct 03 '18 at 20:00
  • Thanks! This is working well for `person.py`, but when I extend the above to `import team`, it is not picking up the module. Can you give an example of how I might do this? – raphattack Oct 03 '18 at 21:38

0 Answers0