Моя структура каталогов такова:
├── 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"
После установки моего пакета:
$ pip install -e .
Obtaining file:///Users/raphattack/PyCharm/person
Installing collected packages: person
Running setup.py develop for person
Successfully installed person
Я нажимаю эту ошибку:
$ 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 ======================================================================
Если я изменю импорт на from person.person import Person
, он запустится.Как я могу сделать так, чтобы мне нужно было только from person import Person
?
Редактировать: @ hoefling Предложение работает хорошо, но я нажимаю ModuleNotFoundError: No module named 'team'
ошибка, если мой person.py
импортирует team
.как я могу изменить __init__.py
так, чтобы pytest подхватил другой модуль?
team.py
class Team:
def __init__(self, name):
self.name = name
Это работает:
$ python3 person/person.py
hello
Это не так:
$ 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 =====================================================================