Skip to main content

Nython

Build Python Extension Modules for Nim libraries.

Synopsis

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.

System Reqs

Install

Using your favorite Python package manager, this library lives on pypi

pip install nython

Usage

See the example folder of a working project that uses nython (and runs all the tests).

Poetry

  • Add nython as package dependency
  • In the [tool.poetry] section of the pyproject.toml, add `build = "build.py"
  • Create the file 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.
  • Add your Nim modules, nythonize them, and pass them back
  • Note: you must pass in nimbase.h
  • Note: your Nim code must live in a directory that is included in your package build process. It can live side by side with your python.
# 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"}]
            ),
        }
    )
  • Please see the example project for one method of merging your namespaces.
    • The tests directory shows how the functions can be run (nothing special)

Setuptools

Todo - but basically just add ext_modules = nythonize(nimbase, [{"name": "adder", "path": "ponim/adder.nim"}]) to your setup call

Acknowledgements

  • nimpy: this is an amazing project that 'just works' and makes working with Nim and Python easy. nython is just the last 5% in getting the packaging to work. nimpy is the fir 95% of the work.
  • faster-than-requests, I looked a lot at how the build system for this package was set up. Essentially, nython is just a streamlined version of the setup used in faster-than-requests.

Development

Goals

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.

Tests

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.

TODOs

  • Support Nimble / full Nim projects with dependancies
  • Allow for fine-grained compiler option tuning
  • Remove the spurious .so file that ends up in your project root dir.
  • Remove the dep fo passing in nimbase.h and find it on the system somehow
  • Possibly create a Nim install in your local virtualenv somehow, with nimpy
  • Generate some performance tests, although that is more on nimpy than this package

Ponim

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

Metadata

Release files for nython 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nython 0.1.1
File Size Uploaded Provenance Actions
nython-0.1.1.tar.gz 4.9 kB Without provenance Details for nython-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nython 0.1.1
File Interpreter ABI Platform Actions
nython-0.1.1-py3-none-any.whl Python 3 none any Details for nython-0.1.1-py3-none-any.whl

Total release size: 9.8 kB

Release files / nython-0.1.1.tar.gz

Download URL nython-0.1.1.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
af891253538959f6e4d30f5dbc8463119a70f336d5b3778db21184c21b5038fd
BLAKE2b-256 checksum
How to use checksums
902054ac212899cb67699b80c170ebdad7e198ac1ff4454e10dd9b079770f3a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.2 CPython/3.7.5 Darwin/18.7.0

Release files / nython-0.1.1-py3-none-any.whl

Download URL nython-0.1.1-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d868ef5769e979f9533c20ec6eb57515e3d7e3862979a531c5637b429c852077
BLAKE2b-256 checksum
How to use checksums
7f2159b813b453db07b1ab74549ef5f09bbc0978b5f86df4993535df9c5bef8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.2 CPython/3.7.5 Darwin/18.7.0

Security for nython 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page