2014-11-19 17 views
14

Tôi đang cố gắng tạo một setup.py cho một dự án phụ thuộc vào SciPy. sau setup.py tái tạo này:Cách xử lý sự phụ thuộc vào scipy trong setup.py

setup(
    name='test', 
    version='0.1', 
    install_requires=['scipy'] 
) 

Khi cài đặt này sử dụng python setup.py develop nó tạo ra các lỗi sau:

ImportError: No module named numpy.distutils.core 

Tuy nhiên, khi tôi cài đặt scipy sử dụng pip, nó được cài đặt nó từ một bánh xe, và nó hoạt động bình thường.

Vì vậy, câu hỏi của tôi là, làm cách nào để tạo một setup.py phụ thuộc vào SciPy? Tại sao không setuptools cài đặt các phụ thuộc từ bánh xe? Điều này sẽ làm việc tốt hơn khi sử dụng Python 3 (chúng tôi dự định di chuyển anyway, vì vậy nếu nó hoạt động ở đó, tôi sẽ chỉ đợi cho đến khi quá trình di chuyển hoàn tất).

Tôi đang sử dụng Python 2.7.8 trên Mac OS X 10.10.1 với setuptools 3.6 và pip 1.5.6.

+1

'install_requires' luôn làm phiền tôi; Tôi đã phải làm việc xung quanh nó ở lần, nhưng tôi không có một giải pháp ở đây. Có 'install_requires = ['numpy', 'scipy']' giúp đỡ không? – Evert

+1

Và có lẽ [SO câu hỏi & trả lời] này (http://stackoverflow.com/questions/2087148/can-i-use-pip-instead-of-easy-install-for-python-setup-py-install-dependen) help: điều đó có thể cho phép 'pip' xử lý các phụ thuộc, trong khi bạn về cơ bản có cùng hành vi như' python setup.py develop'. – Evert

+0

Không. Rõ ràng thứ tự mà trong đó 'setuptools' cài đặt phụ thuộc không được chỉ định, do đó, nó cố gắng để cài đặt SciPy đầu tiên và không thành công. Kỳ lạ thay, khi tôi chạy thử nghiệm sử dụng [tox] (http://tox.readthedocs.org) (không có 'tox.ini' cơ bản nhất), nó sẽ hoạt động tốt. –

Trả lời

2

Cuối cùng, điều này đã làm việc cho tôi:

#!/usr/bin/env python 

from setuptools import setup, Extension 
from setuptools.command.build_ext import build_ext as _build_ext 

# 
# This cludge is necessary for horrible reasons: see comment below and 
# http://stackoverflow.com/q/19919905/447288 
# 
class build_ext(_build_ext): 
    def finalize_options(self): 
     _build_ext.finalize_options(self) 
     # Prevent numpy from thinking it is still in its setup process: 
     __builtins__.__NUMPY_SETUP__ = False 
     import numpy 
     self.include_dirs.append(numpy.get_include()) 

setup(
    # 
    # Amazingly, `pip install scipy` fails if `numpy` is not already installed. 
    # Since we cannot control the order that dependencies are installed via 
    # `install_requires`, use `setup_requires` to ensure that `numpy` is available 
    # before `scipy` is installed. 
    # 
    # Unfortunately, this is *still* not sufficient: `numpy` has a guard to 
    # check when it is in its setup process that we must circumvent with 
    # the `cmdclass`. 
    # 
    setup_requires=['numpy'], 
    cmdclass={'build_ext':build_ext}, 
    install_requires=[ 
     'numpy', 
     'scipy', 
    ], 
    ... 
) 
2

Sử dụng setup_requires tham số để cài đặt numpy trước scipy:

setup(
    name='test', 
    version='0.1', 
    setup_requires=['numpy'], 
    install_requires=['scipy'] 
) 

Note: Cũng biên dịch Fortran là cần thiết để xây dựng scipy. Bạn có thể cài đặt nó qua brew:

brew install gcc 

p.s. Nếu bạn có AttributeError: 'module' object has no attribute 'get_include' hãy xem Why doesn't setup_requires work properly for numpy? SO câu hỏi, nó giả sử để khắc phục vấn đề đó.

+0

Tính năng này hoạt động như thế nào? Đặc biệt, khi nào phương thức 'run' được thực thi? –

+0

@ AlexReece cảm ơn vì đã chỉ ra, phương pháp và móc đó là tối nghĩa ... Tôi đã xóa nó. –

Các vấn đề liên quan