我一直在努力创建一个 python 包,其中包含一些我想在 numpy 中使用 f2py 合并的 fortran 代码。目标是将其上传到 PyPI,以便用户可以使用 pip 进行安装。我做了一些研究,发现 setuptools 和 numpy.distutils.core 具有我想要的功能(我认为)。包的结构是这样的,
rabacus
--setup.py
--README.txt
--MANIFEST.in
--rabacus
----f2py
----python_mod1
----python_mod2
----python_mod3
我在 f2py 目录中有 fortran 代码。我的包需要 numpy 和另一个名为 quantity 的 python 包,它们都在 PyPI 中。我已经用 install_requires 关键字在 setup 函数中指出了这一点。我的测试周期是这样的,
注册并上传包到 PyPI
python setup.py register sdist upload
创建一个没有系统包的虚拟环境
virtualenv --no-site-packages venv
进入虚拟环境并尝试使用 pip 安装
cd venv/bin
./pip install rabacus
我希望它检测未满足的要求(numpy 和数量),下载它们,然后继续我的包安装。我对应该调用哪个设置函数(来自 setuptools 的函数或来自 numpy.distutils.core 的函数)感到困惑。似乎一个有 install_requires,一个有 ext_modules。到目前为止,当我在虚拟环境中执行 pip 命令时,我收到一个错误,抱怨找不到 numpy
Downloading/unpacking rabacus
Downloading rabacus-0.9.0.tar.gz (1.7MB): 1.7MB downloaded
Running setup.py egg_info for package rabacus
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "/home/galtay/bitbucket/rabacus/venv/build/rabacus/setup.py", line 3, in <module>
import numpy.distutils.core
ImportError: No module named numpy.distutils.core
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "/home/galtay/bitbucket/rabacus/venv/build/rabacus/setup.py", line 3, in <module>
import numpy.distutils.core
ImportError: No module named numpy.distutils.core
----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /home/galtay/bitbucket/rabacus/venv/build/rabacus
Storing complete log in /home/galtay/.pip/pip.log
是否可以将 numpy 作为要求并在 setup.py 脚本中使用 numpy?
我的 setup.py 看起来像这样,
import os
import setuptools
import numpy.distutils.core
# setup fortran 90 extension
#---------------------------------------------------------------------------
f90_fnames = ['types.f90', 'utils.f90', 'physical_constants.f90',
'hui_gnedin_97.f90', 'hm12.f90', 'chem_cool_rates.f90',
'ion_solver.f90', 'verner_96.f90', 'photo_xsections.f90',
'spectra.f90', 'source_point.f90', 'source_plane.f90',
'source_background.f90', 'iliev_sphere.f90', 'slab_plane.f90']
f90_paths = []
for fname in f90_fnames:
f90_paths.append( 'rabacus/f2py/' + fname )
ext1 = numpy.distutils.core.Extension(
name = 'rabacus_fc',
sources = f90_paths
)
# write short description
#--------------------------------------------------------------------------
description = 'Calculates analytic cosmological radiative transfer ' + \
'solutions in simplified geometries.'
# puts the contents of the README file in the variable long_description
#--------------------------------------------------------------------------
with open('README.txt') as file:
long_description = '\n\n ' + file.read()
# call setup
#--------------------------------------------------------------------------
#setuptools.setup(
numpy.distutils.core.setup(
install_requires = ['quantities', 'numpy'],
name = 'rabacus',
version = '0.9.0',
description = description,
long_description = long_description,
url = 'https://bitbucket.org/galtay/rabacus',
download_url = 'https://pypi.python.org/pypi/rabacus',
license = 'GPL v3',
platforms = 'linux',
author = 'Gabriel Altay',
author_email = 'gabriel.altay@gmail.com',
classifiers = [
"Programming Language :: Python",
"Programming Language :: Fortran",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: POSIX :: Linux",
"Topic :: Scientific/Engineering :: Astronomy",
"Topic :: Scientific/Engineering :: Physics",
"Intended Audience :: Science/Research",
"Development Status :: 4 - Beta",
"Topic :: Education",
"Natural Language :: English",
],
packages = setuptools.find_packages(),
package_data = {'': ['*.f90']},
include_package_data = True,
ext_modules = [ext1],
)