python - python安装文件中的numpy依赖项
问题描述
这是一个出现的问题,但我还没有找到一个好的解决方案。所以,我有一个 python 项目,我希望用户能够将它安装在一个最小的 anaconda 虚拟环境中。
我的代码还使用 Cython 来加速一些计算密集型函数,并要求 numpy 标头能够对它们进行 cythonize。所以,我有一个设置python脚本如下:
from distutils.extension import Extension
from setuptools import setup, Extension
def create_extension():
import numpy as np # No luck even though I keep it at function scope
return Extension("speed",
sources=["app/perf/booster.pyx"],
include_dirs=[np.get_include()], # problematic
language="c++",
libraries=[],
extra_link_args=[])
setup(
setup_requires=[
'setuptools>=18.0',
'numpy==1.13.3',
'cython==0.28.2'
],
ext_modules=[
create_extension(),
],
......
install_requires=[
'numpy==1.13.3',
'Cython==0.28.2',
'nibabel==2.2.1',
'scipy==1.0.0'
],
)
但是,当我运行build
orinstall
时,numpy inport 失败。虽然我已经将它添加到setup_requires
组和install_requires
组中,但它似乎没有任何效果。
当然,我可以要求用户预先安装 numpy,但我宁愿让它一步到位,我想知道是否有办法实现这一点。
发表评论后,我尝试了该线程中的解决方案,如下所示:
F
rom distutils.extension import Extension
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools import setup, Extension
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())
def create_extension():
#import numpy as np
return Extension("speed",
sources=["app/perf/booster.pyx"],
#include_dirs=[np.get_include()],
language="c++",
libraries=[],
extra_link_args=[])
setup(
cmdclass={'build_ext': build_ext},
.....
现在这失败并出现错误:
error: unknown file type '.pyx' (from 'app/perf/booster.pyx')
解决方案
推荐阅读
- scrapy - Scrapy 抓取 div 下的所有文本
- python - 如何扫描字符串中的字符以标记匹配是否正确
- python - python:瑞利拟合(直方图)
- r - 使用 lapply() 在 R 中生成 igraph 属性
- sql - 在Oracle中将一列的数据显示为一行
- templates - 如何将调度程序和触发器添加到 Azure devops 管道中的嵌套模板?
- javascript - 如何在 VueJS 中来回传递数据
- javascript - 拒绝显示...在一个框架中,因为它在 React 和 oAuth 中将“X-Frame-Options”设置为“同源”
- powershell - 可重用的自定义排序
- nginx - Nginx 配置将任何 url 作为根 url 提供服务