Build Python Extension Modules for Nim libraries.
Build Python Extension Modules for Nim libraries.
This is using Nim's compileToC command to generate C code that Python can then package as an extension module and compile natively when your package is installed elsewhere. Note that this does not solve the fact that Python wheels with C code are specific the platform they are built on. So a wheel built on a mac will run on another mac. Check out the manylinux project for ideas on how to build wheels for more platforms.
Using your favorite Python package manager, this library lives on pypi
pip install nython
See the example folder of a working project that uses nython (and runs all the tests).
nython
as package dependency[tool.poetry]
section of the pyproject.toml
, add `build = "build.py"build.py
in top level of your project. This will be called by poetry when creating the package, essentially it just needs to have a build
function that takes a dict of setup kwargs and adds to it.# Example build.py script
from nython import nythonize
from os.path import expanduser
def build(setup_kwargs):
"""Called by poetry, the args are added to the kwargs for setup."""
nimbase = expanduser("~") + "/.choosenim/toolchains/nim-1.0.4/lib/nimbase.h"
setup_kwargs.update(
{
"ext_modules": nythonize(
nimbase, [{"name": "adder", "path": "ponim/adder.nim"}]
),
}
)
Todo - but basically just add ext_modules = nythonize(nimbase, [{"name": "adder", "path": "ponim/adder.nim"}])
to your setup call
A list of projects using nython to reference as how-tos
Create a seamless development experience for integrating Nim code with Python. Nim should be so easy to use that eventually you just end up writing Nim-only modules for Python, and then realize you don't really need Python and migrate to just Nim. This package should enable Nim in places and companies where it can't be selected as the primary language for a project, but it can be reached for when performance is needed. This should be easier to use than Cython.
Currently this is tested by running the code in the example project. I would like to find a better way to do this, so if you have a good idea, feel free to contribute!
Currently:
cd example
poetry shell
poetry install
poetry run py_test
And that is it.
If you are interested in this, and want to see a nice local dev way of doing things, check out this example I put together: https://github.com/sstadick/ponim