3

I have a python package (python 2.7) that includes some optimized functions written in cython. My setup.py handles the pure-python files and the installed package uses the un-optimized pure python functions.

I'm trying to understand what is the best way to setup the project for distributing the C extension.

Right now I have a specific setup.py in the extension folder that can be used to compile the extension with:

python setup.py build_ext --inplace

But I want the package installation to also handle the C extension. Possibly with a single:

python setup.py

I was thinking of merging the specific setup.py in the project-wide setup.py. However, to compile the extension, I need to import numpy. But I know that importing anything outside the standard lib in setup.py is discouraged.

What are the best practices for python packages including a simple cython extensions that only uses numpy and no other external libs?

5
  • But I know that importing anything outside the standard lib in setup.py is discouraged. Where did you find that? Commented Nov 12, 2014 at 20:45
  • It will break installing the package as a dependence in a new virtualenv. See for example: github.com/lmfit/lmfit-py/issues/149 Commented Nov 12, 2014 at 21:04
  • I would recommend seeing how other projects solve this: e.g. github.com/statsmodels/statsmodels/blob/master/setup.py I would not state that importing third party modules is discouraged. However it's true that you cannot just write import numpy without getting into trouble. A mix of defensive importing with the try statement and using setuptools install_requires probably solves the problem for you. Commented Nov 12, 2014 at 21:31
  • 1
    Do you know the difference between requires and install_requires? Commented Nov 12, 2014 at 23:24
  • see: stackoverflow.com/questions/17727398/… Commented Nov 13, 2014 at 10:54

1 Answer 1

2

For now I settled on the following solution that requires importing numpy. It does not cause problems so far.

setup.py

from setuptools import setup
from setuptools.extension import Extension
import numpy as np
import versioneer

## Metadata
project_name = 'foobar'
long_description = """
Long description in RST. Used by PyPI.
"""

## Configure versioneer
versioneer.VCS = 'git'
versioneer.versionfile_source = project_name + '/_version.py'
versioneer.versionfile_build = project_name + '/_version.py'
versioneer.tag_prefix = '' # tags are like 1.2.0
versioneer.parentdir_prefix = project_name + '-'

## Configuration to build Cython extensions
try:
    from Cython.Distutils import build_ext
except ImportError:
    # cython is not installed, use the .c file
    has_cython = False
    ext_extention = '.c'
else:
    # cython is installed, use .pyx file
    has_cython = True
    ext_extention = '.pyx'
ext_modules = [Extension("corecalculation_c",
                         [project_name + \
                         "/calculation/corecalculation_c" + ext_extention])]

## Configure setup.py commands
cmdclass = versioneer.get_cmdclass()
if has_cython:
    cmdclass.update(build_ext=build_ext)


setup(name = project_name,
      version = versioneer.get_version(),
      cmdclass = cmdclass,
      include_dirs = [np.get_include()],
      ext_modules = ext_modules,
      author = 'Author Name',
      author_email = 'email@address',
      url          = 'http://github.com/USER/PROJECT/',
      download_url = 'http://github.com/USER/PROJECT/',
      install_requires = ['numpy', 'scipy', 'matplotlib', 'ipython'],
      license = 'GPLv2',
      description = ("Oneline description"),
      long_description = long_description,
      platforms = ('Windows', 'Linux', 'Mac OS X'),
      classifiers=['Intended Audience :: Science/Research',
                   'Operating System :: OS Independent',
                   'Programming Language :: Python',
                   'Programming Language :: Python :: 2.7',
                   'Topic :: Scientific/Engineering',
                   ],
      packages = [project_name, project_name+'.utils'],
      keywords = 'keyword1 keyword2',
      )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.