我有一个使用Python 3的virtualenv,目前正在使用pytest进行测试。我已经打包了一个名为"process_expiring_passwords“的程序,并从名为"pep.py”的普通脚本文件中导入了这个包。我还在虚拟环境中使用了pip来安装我的process_expiring_passwords包。
然而,当我去运行我的测试时,我会得到以下错误:
导入测试模块'/home/username/projects/process-expiring-passwords/tests/test_process_expiring_passwords.py'.时的ImportError 提示:确保您的测试模块/包具有有效的Python名称。 回溯:test/test_process_届满_passwords.py:3: in从process_expiring_passwords.models.ProcessExpiringPasswordsLog导入ProcessExpiringPasswords ProcessExpiringPasswords \e ModuleNotFoundError:没有一个模块名为‘process_expiring_passwords_process_passwords.model’;'process_expiring_passwords‘不是一个包
当pytest无法找到所述包时,是否有特殊的原因可以使用我的pep.py通用脚本文件中的包?
与这一问题有关的守则的基本部分如下:
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进程_届满_密码/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',
],
)测试/测试过程_到期_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备注:
发布于 2018-10-26 20:48:04
下面是关于这个问题的an extended conversation之后,下面的步骤特别有助于基于@ here的评论和基于@Messersmith的答复将packages=find_packages(),添加到setup.py之后:
setup.py移动到根目录项目目录__pycache__文件from .models.ProcessExpiringPasswordsLog \ import ProcessExpiringPasswordsLogpython -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"easy-install.pth中删除旧的包行(应该使用上一步生成的路径找到它)pip install -e .process_expiring_passwords添加到测试文件的导入部分:
from process_expiring_passwords.process_expiring_passwords \ import ProcessExpiringPasswords发布于 2018-10-26 16:57:38
您的代码不太像发布的那样工作,我对其进行了修改,使其在构造函数和成员函数中具有self关键字。在那之后,我可以让pep.py运行。
当pytest无法找到所述包时,是否有特殊的原因可以使用我的pep.py通用脚本文件中的包?
是的,当您处于顶级process_expiring_passwords目录中时,您可以运行pep.py,因为您将得到包的“本地”导入。
测试没有运行这一事实与pytest没有多大关系,更多的是与导入语句的外观以及您没有构建包这一事实有关:
from process_expiring_passwords import ProcessExpiringPasswords您还没有创建一个名为process_expiring_passwords的包。您可以通过执行python setup.py bdist_wheel (或只执行sdist并解压缩tarball)、解压方向盘并注意没有任何包来验证这一点。
通常,您需要安装包python setup.py install (这是站点包中的可分发包),您还必须在setup.py中命名这些包(find_packages()是您的朋友),然后您的测试就会像预期的那样工作(或多或少)。
HTH。
https://stackoverflow.com/questions/53011469
复制相似问题