【问题标题】:scipy ImportError on travis-citravis-ci 上的 scipy ImportError
【发布时间】:2015-07-28 22:10:25
【问题描述】:

我是第一次设置 Travis-CI。我以我认为是标准的方式安装 scipy:

language: python
python:
  - "2.7"
# command to install dependencies
before_install:
  - sudo apt-get -qq update
  - sudo apt-get -qq install python-numpy python-scipy python-opencv
  - sudo apt-get -qq install libhdf5-serial-dev hdf5-tools
install:
   - "pip install numexpr"
   - "pip install cython"
   - "pip install -r requirements.txt --use-mirrors"
# command to run tests
script: nosetests

一切都建立起来。但是当鼻子测试开始时,我得到了

ImportError: No module named scipy.ndimage

更新:这里有一个更直接的问题演示。

$ sudo apt-get install python-numpy python-scipy python-opencv
$ python -c 'import scipy'
Traceback (most recent call last):
  File "<string>", line 1, in <module>

ImportError: No module named scipy

The command "python -c 'import scipy'" failed and exited with 1 during install.

我也尝试使用 pip 安装 scipy。我尝试先安装 gfortran。 Here is one example of a failed build。有什么建议吗?

另一个更新:Travis 已经添加了关于在 Travis 中使用 conda 的官方文档。请参阅 ostrokach 的回答。

【问题讨论】:

  • 当您从sudo apt-get install python-scipy 中删除-qq 选项时,您是否看到任何有趣的消息?如果你打开一个终端,你能import scipy.ndimage吗?
  • 我删除了-qq (see latest build here),我没有看到有问题的消息。如果我在我的电脑上打开一个终端,是的,我可以import scipy.ndimage。但也许我误解了你:有什么方法可以在 Travis 中打开终端吗?
  • 对不起,丹,我不知道。只是 scipy 引发了导入错误,还是 numpy、cv 等也引发了ImportErrors
  • 好建议。 Numpy 很好,但 cv 和 cv2 也会引发 ImportErrors。我不确定我们在这里学到了什么,但这不仅仅是 scipy 的错。
  • 如果您查看 pip 命令的失败日志,您将看到 Travis CI 正在虚拟环境中安装 numexpr/cypthon/etc。所以我的猜测是sudo apt-get install命令正在为系统的python安装scipy和friends,但是Travis CI环境正在使用virtualenv的python进行nosetest,它无法访问系统的包。我知道您已经尝试过使用 pip 安装 scipy,但也许可以使用 this 作为指导再试一次。

标签: python scipy travis-ci


【解决方案1】:

我找到了解决这个困难的两种方法:

  1. 按照@unutbu 的建议,构建您自己的虚拟环境并在该环境中使用 pip 安装所有内容。我通过了构建,但是以这种方式从源代码安装 scipy 非常慢。

  2. 按照 pandas 项目在this .travis.yml file and the shell scripts that it calls 中使用的方法,强制 travis 使用系统范围的站点包,并使用 apt-get 安装 numpy 和 scipy。这要快得多。关键线是

    virtualenv:
        system_site_packages: true
    

    在 travis.yml 中 before_install 组之前,后面是这些 shell 命令

    SITE_PKG_DIR=$VIRTUAL_ENV/lib/python$TRAVIS_PYTHON_VERSION/site-packages
    rm -f $VIRTUAL_ENV/lib/python$TRAVIS_PYTHON_VERSION/no-global-site-packages.txt  
    

    然后是最后

    apt-get install python-numpy
    apt-get install python-scipy
    

    当鼻子测试尝试导入它们时会找到。

更新

我现在更喜欢基于 conda 的构建,它比上述任何一种策略都快。这是我维护的一个项目的one example

【讨论】:

  • 如果 Travis 上的 pip install scipy 超时,则可以按照此处所述增加超时:stackoverflow.com/questions/28746046/…
  • 使用 sudo 也意味着不使用更新的、基于容器的 travis 基础架构。此外,系统提供的numpyscipy已经相当过时了。
【解决方案2】:

官方 conda 文档中对此进行了介绍:Using conda with Travis CI


.travis.yml 文件

下面展示了如何修改.travis.yml 文件以将Miniconda 用于支持Python 2.6、2.7、3.3 和3.4 的项目。

注意:有关basic configuration for Travis 的信息,请参阅 Travis CI 网站。

language: python
python:
  # We don't actually use the Travis Python, but this keeps it organized.
  - "2.6"
  - "2.7"
  - "3.3"
  - "3.4"
install:
  - sudo apt-get update
  # We do this conditionally because it saves us some downloading if the
  # version is the same.
  - if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then
      wget https://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh;
    else
      wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh;
    fi
  - bash miniconda.sh -b -p $HOME/miniconda
  - export PATH="$HOME/miniconda/bin:$PATH"
  - hash -r
  - conda config --set always_yes yes --set changeps1 no
  - conda update -q conda
  # Useful for debugging any issues with conda
  - conda info -a

  # Replace dep1 dep2 ... with your dependencies
  - conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION dep1 dep2 ...
  - source activate test-environment
  - python setup.py install

script:
  # Your test script goes here

【讨论】:

  • 效果很好,速度也很快。
【解决方案3】:

我发现这种方法可行:

http://danielnouri.org/notes/2012/11/23/use-apt-get-to-install-python-dependencies-for-travis-ci/

将这些行添加到您的 Travis 配置中以使用 virtualenv--system-site-packages

virtualenv:
  system_site_packages: true

因此,您可以通过before_install 部分中的apt-get 安装Python 包,并在您的virtualenv 中使用它们:

before_install:
 - sudo apt-get install -qq python-numpy python-scipy

nolearn 中可以找到这种方法的实际使用。

【讨论】:

【解决方案4】:

正如 Dan Allan 在他的更新中指出的那样,他现在更喜欢基于 conda 的构建。 Here is a gist 礼貌 Dan Blanchard 提供了完整的 .travis.yml 文件示例,它将在测试机器上预安装 scipy:

language: python
python:
  - 2.7
  - 3.3
notifications:
  email: false

# Setup anaconda
before_install:
  - wget http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh
  - chmod +x miniconda.sh
  - ./miniconda.sh -b
  - export PATH=/home/travis/miniconda/bin:$PATH
  - conda update --yes conda
  # The next couple lines fix a crash with multiprocessing on Travis and are not specific to using Miniconda
  - sudo rm -rf /dev/shm
  - sudo ln -s /run/shm /dev/shm
# Install packages
install:
  - conda install --yes python=$TRAVIS_PYTHON_VERSION atlas numpy scipy matplotlib nose dateutil pandas statsmodels
  # Coverage packages are on my binstar channel
  - conda install --yes -c dan_blanchard python-coveralls nose-cov
  - python setup.py install

# Run test
script:
  - nosetests --with-cov --cov YOUR_PACKAGE_NAME_HERE --cov-config .coveragerc --logging-level=INFO

# Calculate coverage
after_success:
  - coveralls --config_file .coveragerc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-11
    • 2021-02-16
    • 2023-04-07
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 2016-05-02
    • 2018-07-15
    相关资源
    最近更新 更多