PDi*_*lta 5 unit-testing cython pytest pycharm python-3.x
目标是将pytest单元测试框架用于使用 Cython 的 Python3 项目。这不是即插即用的事情,因为pytest默认情况下无法导入 Cython 模块。
一种不成功的解决方案是使用该pytest-cython插件,但它对我来说根本不起作用:
> py.test --doctest-cython
usage: py.test [options] [file_or_dir] [file_or_dir] [...]
py.test: error: unrecognized arguments: --doctest-cython
inifile: None
rootdir: /censored/path/to/my/project/dir
Run Code Online (Sandbox Code Playgroud)
要验证我是否已安装该软件包:
> pip freeze | grep pytest-cython
pytest-cython==0.1.0
Run Code Online (Sandbox Code Playgroud)
更新:我正在使用 PyCharm,它似乎没有使用我的 pip 安装的软件包,而是使用我的项目使用的软件包的自定义(?)pycharm 存储库。一旦我添加pytest-cython到该存储库,该命令就会运行,但奇怪的是它无论如何都无法识别 Cython 模块,尽管包/附加组件是专门为此目的而设计的:
> pytest --doctest-cython
Traceback:
tests/test_prism.py:2: in <module>
from cpc_naive.prism import readSequence, processInput
cpc_naive/prism.py:5: in <module>
from calculateScore import calculateScore, filterSortAlphas,
calculateAlphaMatrix_c#, incrementOverlapRanges # cython code
E ImportError: No module named 'calculateScore'
Run Code Online (Sandbox Code Playgroud)
我在这里得到的另一个不成功的解决方案是使用pytest-runner,但这会产生:
> python3 setup.py pytest
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: setup.py --help [cmd1 cmd2 ...]
or: setup.py --help-commands
or: setup.py cmd --help
error: invalid command 'pytest'
Run Code Online (Sandbox Code Playgroud)
更新:
我首先忘记将setup_requires=['pytest-runner', ...]和添加tests_require=['pytest', ...]到设置脚本中。一旦我这样做了,我又收到了另一个错误:
> python3 setup.py pytest
Traceback (most recent call last):
File "setup.py", line 42, in <module>
tests_require=['pytest']
(...)
AttributeError: type object 'test' has no attribute 'install_dists'
Run Code Online (Sandbox Code Playgroud)
更新2(setup.py):
from distutils.core import setup
from distutils.extension import Extension
from setuptools import find_packages
from Cython.Build import cythonize
import numpy
try: # try to build the .c file
from Cython.Distutils import build_ext
except ImportError: # if the end-user doesn't have Cython that's OK; you should have shipped the .c files anyway.
use_cython = False
else:
use_cython = True
cmdclass = {}
ext_modules = []
if use_cython:
ext_modules += [
Extension("cpc_naive.calculateScore", ["cpc_naive/calculateScore.pyx"],
extra_compile_args=['-g'], # -g for debugging
define_macros=[('CYTHON_TRACE', '1')]),
]
cmdclass.update({'build_ext': build_ext})
else:
ext_modules += [
Extension("cpc_naive.calculateScore", ["cpc_naive/calculateScore.c"],
define_macros=[('CYTHON_TRACE', '1')]), # compiled C files are stored in /home/pdiracdelta/.pyxbld/
]
setup(
name='cpc_naive',
author=censored,
author_email=censored,
license=censored,
packages=find_packages(),
cmdclass=cmdclass,
ext_modules=ext_modules,
install_requires=['Cython', 'numpy'],
include_dirs=[numpy.get_include()],
setup_requires=['pytest-runner'],
tests_require=['pytest']
)
Run Code Online (Sandbox Code Playgroud)
更新3(部分修复):按照@hoefling的建议,我降级pytest-runner到版本<4(实际上是3.0.1),这解决了更新1中的错误,但现在我得到了与解决方案相同的异常pytest-cython:
E ImportError: No module named 'calculateScore'
Run Code Online (Sandbox Code Playgroud)
它似乎无法识别该模块。也许这是由于我不理解的一些绝对/相对导入魔力。
如何pytest与 Cython 一起使用?我怎样才能发现这些方法不起作用的原因,然后修复它?
最终更新:在考虑了原始问题和问题更新后(感谢@hoefling解决了这些问题!),这个问题现在简化为以下问题:
为什么 pytest 不能导入 Cython 模块calculateScore,即使仅使用 python (无 pytest)运行代码就可以正常工作?
正如 @hoefling 建议的那样,应该使用pytest-runner<0.4 版本以避免
AttributeError: type object 'test' has no attribute 'install_dists'
Run Code Online (Sandbox Code Playgroud)
然后回答实际和最终的问题(除了添加到问题帖子本身的部分、偏离主题、特定于用户的修复之外)为什么pytest不能导入 Cython 模块calculateScore,即使仅使用 python(无 pytest)运行代码也是如此很好:剩下的问题在这里解决了。
| 归档时间: |
|
| 查看次数: |
2704 次 |
| 最近记录: |