我的目录结构如下:
├── 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 =====================================================================