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 =====================================================================