Giter Club home page Giter Club logo

nimpy's Introduction

nimpy Build Status

Native language integration with Python has never been easier!

Implementing a Python Module in Nim

# mymodule.nim - file name should match the module name you're going to import from python
import nimpy

proc greet(name: string): string {.exportpy.} =
  return "Hello, " & name & "!"
# Compile on Windows:
nim c --threads:on --app:lib --out:mymodule.pyd mymodule
# Compile on everything else:
nim c --threads:on --app:lib --out:mymodule.so mymodule
# test.py
import mymodule
assert mymodule.greet("world") == "Hello, world!"
assert mymodule.greet(name="world") == "Hello, world!"

Calling Python From Nim

import nimpy
let os = pyImport("os")
echo "Current dir is: ", os.getcwd().to(string)

# sum(range(1, 5))
let py = pyBuiltinsModule()
let s = py.sum(py.range(0, 5)).to(int)
assert s == 10

Note: here nimpy relies on your local python installation.

Importing Nim Extensions Directly

For a convenient way to import your Nim extension modules directly, you can use Nimporter.

Misc

The library is designed with ABI compatibility in mind. That is the compiled module doesn't depend on particular Python version, it should properly work with any. The C API symbols are loaded in runtime from whichever process has launched your module.

Troubleshooting, Q&A

Question:

Importing the compiled module from Python fails with ImportError: dynamic module does not define module export function ...

Make sure that the module you're importing from Python has exactly the same name as the nim file which the module is implemented in.

Question:

Nim strings are converted to Python bytes instead of string

nimpy converts Nim strings to Python strings usually, but since Nim strings are encoding agnostic and may contain invalid utf8 sequences, nimpy will fallback to Python bytes in such cases.

Question:

Is there any numpy compatibility?

nimpy allows manipulating numpy objects just how you would do it in Python, however it not much more efficient. To get the maximum performance nimpy exposes Buffer protocol, see raw_buffers.nim. tpyfromnim.nim contains a very basic test for this (grep numpy). Higher level API might be considered in the future, PRs are welcome.

Question:

Does nim default garbage collector (GC) work?

nimpy internally does everything needed to run the GC properly (keeps the stack bottom actual, and appropriate nim references alive), and doesn't introduce any special rules on top. So the GC question boils down to proper GC usage in nim shared libraries, you'd better lookup elsewhere. The following guidelines are by no means comprehensive, but should be enough for the quick start:

  • If it's known there will be only one nimpy module in the process, you should be fine.
  • If there is more than one nimpy module, it is recommended to move nim runtime out to a separate shared library. However it might not be needed if nim references are known to never travel between nim shared libraries.
  • If you hit any GC problems with nimpy, whether you followed these guidelines or not, please report them to nimpy tracker :)

Future directions

  • exporting Nim types/functions as Python classes/methods
  • High level buffer API

Stargazers over time

Stargazers over time

nimpy's People

Contributors

bontavlad avatar chrisheller avatar clyybber avatar erhlee-bird avatar jonasrauber avatar juancarlospaco avatar kdheepak avatar lemonboy avatar pebaz avatar vindaar avatar yglukhov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.