使用`conda build`时如何设置环境?

时间:2019-09-14 18:00:43

标签: python-3.x windows-10 conda conda-build

试图利用Conda的build功能安装不在conda存储库中的软件包。我已经下载了想要的软件包的源文件。在同一级别上,我为 conda食谱创建了一个文件夹,其中包含meta.yaml设置source: path: ../<package-name>。因此,查找一个目录以查找源文件。这行得通。

当构建尝试测试导入包时,问题最终会发生。它说没有找到 x模块,但是我是从一个conda环境中调用conda build的,在conda环境中安装了缺少的软件包,如conda list所示。

我如何从conda build的环境中进行这些测试?

使用conda build时如何设置环境?

Conda Build文档中充斥着 environment 这个单词,但大部分指的是environmental variables而不是conda environments

(base) C:\Users\myhome\PyTools\bin>conda info

     active environment : base
    active env location : C:\Users\myhome\Miniconda3_64-4.5.4
            shell level : 1
       user config file : C:\Users\myhome\.condarc
 populated config files : C:\Users\myhome\.condarc
          conda version : 4.5.4
    conda-build version : 3.18.9
         python version : 3.6.5.final.0
       base environment : C:\Users\myhome\Miniconda3_64-4.5.4  (writable)
           channel URLs : https://conda.anaconda.org/anaconda/win-64
                          https://conda.anaconda.org/anaconda/noarch
                          https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/win-64
                          https://repo.anaconda.com/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/pro/win-64
                          https://repo.anaconda.com/pkgs/pro/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
                          https://conda.anaconda.org/conda-forge/win-64
                          https://conda.anaconda.org/conda-forge/noarch
          package cache : C:\Users\myhome\Miniconda3_64-4.5.4\pkgs
                          C:\Users\myhome\AppData\Local\conda\conda\pkgs
       envs directories : C:\Users\myhome\Miniconda3_64-4.5.4\envs
                          C:\Users\myhome\AppData\Local\conda\conda\envs
                          C:\Users\myhome\.conda\envs
               platform : win-64
             user-agent : conda/4.5.4 requests/2.18.4 CPython/3.6.5 Windows/10 Windows/10.0.17763
          administrator : False
             netrc file : None
           offline mode : False

(myenv) C:\Users\myhome\PyTools\bin>conda build <module to install>
...
set PREFIX=C:\Users\myhome\Miniconda3_64-4.5.4\conda-bld\swaggerpy_1568484873987\_test_env
set SRC_DIR=C:\Users\myhome\Miniconda3_64-4.5.4\conda-bld\swaggerpy_1568484873987\test_tmp

(myenv) %SRC_DIR%>call "%SRC_DIR%\conda_test_env_vars.bat"

(myenv) %SRC_DIR%>call "C:\Users\myhome\Miniconda3_64-4.5.4\Scripts\activate.bat" "%PREFIX%"

(%PREFIX%) %SRC_DIR%>IF 0 NEQ 0 exit 1

(%PREFIX%) %SRC_DIR%>IF 0 NEQ 0 exit 1

(%PREFIX%) %SRC_DIR%>"%PREFIX%\python.exe" -s "%SRC_DIR%\run_test.py"
Traceback (most recent call last):
import: 'swaggerpy'
  File "C:\Users\myhome\Miniconda3_64-4.5.4\conda-bld\swaggerpy_1568484873987\test_tmp\run_test.py", line 2, in <module>
    import swaggerpy
  File "C:\Users\myhome\Miniconda3_64-4.5.4\conda-bld\swaggerpy_1568484873987\_test_env\lib\site-packages\swaggerpy\__init__.py", line 13, in <module>
    from .swagger_model import load_file, load_json, load_url, Loader
  File "C:\Users\myhome\Miniconda3_64-4.5.4\conda-bld\swaggerpy_1568484873987\_test_env\lib\site-packages\swaggerpy\swagger_model.py", line 15, in <module>
    from swaggerpy.http_client import SynchronousHttpClient
  File "C:\Users\myhome\Miniconda3_64-4.5.4\conda-bld\swaggerpy_1568484873987\_test_env\lib\site-packages\swaggerpy\http_client.py", line 11, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

(%PREFIX%) %SRC_DIR%>IF 1 NEQ 0 exit 1
Tests failed for swaggerpy-0.2.1-py36_0.tar.bz2 - moving package to C:\Users\myhome\Miniconda3_64-4.5.4\conda-bld\broken
WARNING:conda_build.build:Tests failed for swaggerpy-0.2.1-py36_0.tar.bz2 - moving package to C:\Users\myhome\Miniconda3_64-4.5.4\conda-bld\broken
TESTS FAILED: swaggerpy-0.2.1-py36_0.tar.bz2

(myenv) C:\Users\myhome\PyTools\bin>conda list
# packages in environment at C:\Users\myhome\Miniconda3_64-4.5.4\envs\myenv:
#
# Name                    Version                   Build  Channel
...
requests                  2.22.0                   py36_0    anaconda
...

1 个答案:

答案 0 :(得分:0)

我仍在解决类似的问题,但我想我至少可以为您提供帮助。 (或者遇到相同问题的人)。

conda构建末尾的测试步骤使用的是全新环境-仅包含您的软件包及其特定要求(如recipe/meta.yaml中所述。(注意,您未显示{{ 1}},这是解决此问题的关键)

如果您的包裹需要它,请在meta.yaml中放入以下内容:

meta.yaml

如果您的软件包确实不需要它,但是测试代码(例如单元测试)需要运行它,请使用以下部分:

requirements:
  host:
    - python
    - pip
  run:
    - python
    - requests  # here

这些软件包将使用test: requires: - nose # e.g. some test runners - requests # extra deps for test 而非conda安装。

如何从pip获取信息,或以任意方式修改测试环境?我还在想这个...