Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Cython with Poetry?

I have a file in my project which I would like to compile for performance reasons:

mylibrary/myfile.py

How to achieve this with Poetry?

like image 204
kolypto Avatar asked Aug 31 '20 23:08

kolypto


People also ask

How do you use Cython?

To use Cython two things are needed. The Cython package itself, which contains the cython source-to-source compiler and Cython interfaces to several C and Python libraries (for example numpy). To compile the C code generated by the cython compiler, a C compiler is needed.

Why should I use Cython?

Cython will get you good speedups on almost any raw Python code, without too much extra effort at all. The key thing to note is that the more loops you're going through, and the more data you're crunching, the more Cython can help.

What is poetry in Python?

With Poetry, Python finally has a graceful way to manage virtual environments and dependencies for development projects. Here’s how to get started “There should be one — and preferably only one — obvious way to do it.” While that line comes from Tim Peters’s Zen of Python, Python doesn’t always adhere to that principle.

How do I install poetry in Python?

Please note that you can install poetry using the traditional pip command but poetry will be limited to create virtual environments for the python version for which it has been installed. To study poetry, we will create a simple project poet using the new command. The tree structure of the created project looks like the following one.

What is the use of Cython?

Cython is a tool most commonly used to speed up the execution of Python programs through a secondary compilation step. It works by transpiling Python code into C code, and then compiling that into machine code binaries, which can then be imported in other Python scripts as if they were normal Python.

How to compile Cython files with poetry build?

Now let's move to the Cython part. First of all, next to the pymod folder we create cymod folder with similar contents, but written in Cython. To compile Cython files during the installation with poetry build.py should be created. from setuptools import Extension from Cython.


Video Answer


1 Answers

There is an undocumented feature in Poetry. Add this to your pyproject.toml:

[tool.poetry]
...
build = 'build.py'

[build-system]
requires = ["poetry>=0.12", "cython"]
build-backend = "poetry.masonry.api"

What this does is runs the build.py:build() function inside the implicitly generated setup.py. This is where we build.

So, create a build.py that provides the build() function:

import os

# See if Cython is installed
try:
    from Cython.Build import cythonize
# Do nothing if Cython is not available
except ImportError:
    # Got to provide this function. Otherwise, poetry will fail
    def build(setup_kwargs):
        pass
# Cython is installed. Compile
else:
    from setuptools import Extension
    from setuptools.dist import Distribution
    from distutils.command.build_ext import build_ext

    # This function will be executed in setup.py:
    def build(setup_kwargs):
        # The file you want to compile
        extensions = [
            "mylibrary/myfile.py"
        ]

        # gcc arguments hack: enable optimizations
        os.environ['CFLAGS'] = '-O3'

        # Build
        setup_kwargs.update({
            'ext_modules': cythonize(
                extensions,
                language_level=3,
                compiler_directives={'linetrace': True},
            ),
            'cmdclass': {'build_ext': build_ext}
        })

Now, when you do poetry build, nothing happens. But if you install this package elsewhere, it gets compiled.

You can also build it manually with:

$ cythonize -X language_level=3 -a -i mylibrary/myfile.py

Finally, it seems that you can't publish binary packages to PyPi. The solution is to limit your build to "sdist":

$ poetry build -f sdist
$ poetry publish
like image 174
kolypto Avatar answered Oct 30 '22 21:10

kolypto