我有一个使用Python 3的虚拟环境,并且目前正在使用pytest进行测试。我已经打包了一个名为“process_expiring_passwords”的程序,并从名为“pep.py”的通用脚本文件中导入此包。我还在我的虚拟环境中使用pip安装了我的process_expiring_passwords包。
然而,当我去运行我的测试时,却出现了以下错误:
ImportError while importing test module '/home/username/projects/process-expiring-passwords/tests/test_process_expiring_passwords.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: tests/test_process_expiring_passwords.py:3: in from process_expiring_passwords import ProcessExpiringPasswords process_expiring_passwords/process_expiring_passwords.py:16: in from process_expiring_passwords.models.ProcessExpiringPasswordsLog ModuleNotFoundError: No module named 'process_expiring_passwords.models'; 'process_expiring_passwords' is not a package 是否有特定原因使得我可以从我的pep.py通用脚本文件中使用我的包,而pytest无法找到该包?
与此问题相关的代码的关键部分如下: pep.py:
然而,当我去运行我的测试时,却出现了以下错误:
ImportError while importing test module '/home/username/projects/process-expiring-passwords/tests/test_process_expiring_passwords.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: tests/test_process_expiring_passwords.py:3: in from process_expiring_passwords import ProcessExpiringPasswords process_expiring_passwords/process_expiring_passwords.py:16: in from process_expiring_passwords.models.ProcessExpiringPasswordsLog ModuleNotFoundError: No module named 'process_expiring_passwords.models'; 'process_expiring_passwords' is not a package 是否有特定原因使得我可以从我的pep.py通用脚本文件中使用我的包,而pytest无法找到该包?
与此问题相关的代码的关键部分如下: pep.py:
from process_expiring_passwords.process_expiring_passwords \
import ProcessExpiringPasswords
pep = ProcessExpiringPasswords()
pep.process()
process_expiring_passwords/process_expiring_passwords.py:
from process_expiring_passwords.models.ProcessExpiringPasswordsLog \
import ProcessExpiringPasswordsLog
class ProcessExpiringPasswords:
def __init__(self):
self.example = 0
def process():
pepl = ProcessExpiringPasswordsLog()
process_expiring_passwords/models/ProcessExpiringPasswordsLog.py:
class ProcessExpiringPasswordsLog():
def __init__(self):
self.example = 0
process_expiring_passwords/setup.py:
# ref: https://github.com/pypa/sampleproject/blob/master/setup.py
# ref: http://python-packaging.readthedocs.io/en/latest/minimal.html
from setuptools import setup
setup(
name='process_expiring_passwords',
version='0.0.1',
packages=find_packages(),
description='Process expiring passwords',
license='MIT',
install_requires=[
'apiclient',
'google-api-python-client',
'httplib2',
'mysqlclient',
'oauth2client',
'requests',
'SQLAlchemy',
],
)
tests/test_process_expiring_passwords.py:
from process_expiring_passwords import ProcessExpiringPasswords
def test_pep():
pep = ProcessExpiringPasswords()
assert pep is not None
项目结构:
process_expiring_passwords
pep.py
process_expiring_passwords
__init__.py
models
__init__.py
ProcessExpiringPasswordsLog.py
process_expiring_passwords.py
setup.py
tests
test_process_expiring_passwords.py
注:
- 根据Matt Messersmith的反馈,更新了代码示例和setup.py文件。
pip -e .
),我能想到的唯一原因是pytest
没有安装在你的虚拟环境中,因此你运行的是“主 Python”变体,它无法在 PATH 中找到你的模块。在你的虚拟环境中安装 pytest,它应该可以工作。 - MrLeehprocess_expiring_passwords/process_expiring_passwords.py
中执行import process_expiring_passwords
时,您不会导入父包。在process_expiring_passwords.py
中添加行import process_expiring_passwords; print(process_expiring_passwords.__file__)
并验证您是否导入了模块。 - hoeflingprocess_expiring_passwords.py
中切换到相对导入应该可以解决这个问题:from .models.UserAccountStatus import UserAccountStatus
,但我现在无法自行测试。 - hoeflingprocess_expiring_passwords
包中,它应该被收集和安装,那么您是如何将该包传递给packages
列表的呢?您是否使用了setuptools.find_packages
来实现?包和模块的命名相同可能只是一种分散注意力的复杂性,与实际错误无关。 - hoefling