3

我已经使用 conda build 成功构建了 fftw 和 pyfftw 的包。我现在正在尝试编写一个 run_test.py 例程来执行 pyfftw 中的测试,作为构建过程的一部分。我写了一个 run_test.py 来运行 pyfftw 中的一个测试文件:

import os
path=os.environ.get('SRC_DIR')
execfile(path+'/test/test_pyfftw_real_backward.py')

在构建结束时,我收到以下错误:

Traceback (most recent call last):
  File "/data/miniconda/conda-bld/test-tmp_dir/run_test.py", line 38, in <module>
    execfile(path+'/test/test_pyfftw_real_backward.py')
  File "/data/miniconda/conda-bld/work/pyFFTW-0.9.2/test/test_pyfftw_real_backward.py", line 24, in <module>
    from .test_pyfftw_base import run_test_suites
ValueError: Attempted relative import in non-package
TESTS FAILED: pyfftw-0.9.2-np18py27_0

我是否将文件添加到包中?或者也许将文件复制到测试环境?我实际上不知道如何进行。任何帮助,将不胜感激。

4

1 回答 1

0

这里有几件事(很久以前就问过这个问题)。

上面的问题是您不能通过 - 进行相对导入from .something import ...- 不是因为该文件不存在,而是因为您将其作为文件运行,类似于:

python test/my_test.py

如果您执行以下操作,它将起作用:

python -m test.my_test

但是,execfile不是那样的,而且 - 它已经成为过去:Python 3 中 execfile 的替代方案是什么?

接下来的事情,对于不小心来到这里的人 - 你不应该那样使用SRC_DIR,否则你会得到:

ValueError: In conda-build 2.1+, the work dir is removed by default before 
the test scripts run.  You are using the SRC_DIR variable in your test script,
but these files have been deleted.  Please see the  documentation regarding
the test/source_files meta.yaml section, or pass the --no-remove-work-dir flag.

有关配置 conda 包测试的更多信息,请参见此处:https ://docs.conda.io/projects/conda-build/en/latest/resources/define-metadata.html#test-section

所以,你应该让你的测试独立,或者至少他们应该做一些类似的事情:

import os
import sys

sys.path.append(os.path.dirname(__file__))
import test_common
# etc.

test/要在源中设置子目录的工作设置,只需使用:

test:
  requires:
   - nose
  source_files:
   - test/*.py
  commands:
   - nosetests

不要担心这种配置的不足——更少的代码就是更少的维护。

于 2020-03-02T16:18:20.770 回答