Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork23
🐍 Python interpreter bindings for Deno and Bun.
License
denosaurs/deno_python
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This module provides a seamless integration between JavaScript (Deno/Bun) andPython by integrating with thePython/C API. It acts as a bridgebetween the two languages, enabling you to pass data and execute python codefrom within your JS applications. This enables access to the large and wonderfulpython ecosystem while remaining native (unlike a runtimelike the wonderfulpyodide which iscompiled to wasm, sandboxed and may not work with all python packages) andsimply using the existing python installation.
Import any locally installed Python package, for example,matplotlib:
import{python}from"https://deno.land/x/python/mod.ts";constnp=python.import("numpy");constplt=python.import("matplotlib.pyplot");constxpoints=np.array([1,8]);constypoints=np.array([3,10]);plt.plot(xpoints,ypoints);plt.show();
When running, youmust specify--allow-ffi,--allow-env and--unstable-ffi flags. Alternatively, you may also just specify-A instead ofspecific permissions since enabling FFI effectively escapes the permissionssandbox.
deno run -A --unstable-ffi<file>
You can import from thebunpy NPM package to use this module in Bun.
import{python}from"bunpy";constnp=python.import("numpy");constplt=python.import("matplotlib.pyplot");constxpoints=np.array([1,8]);constypoints=np.array([3,10]);plt.plot(xpoints,ypoints);plt.show();
Normally deno_python follows the default python way of resolving imports, goingthroughsys.path resolving them globally, locally or scoped to a virtualenvironment. This isgreat and allows you to manage your python dependenciesfordeno_python projects in the same way you would any other python projectusing your favorite package manager, be itpip,conda orpoetry.
This may not be a good thing though, especially for something like a deno modulewhich may depend on a python package. That is why theext/piputility exists for this project. It allows you to install python dependenciesusing pip, scoped to either the global deno installation or if defined the--location passed to deno without leaking to the global python scope. It usesthe same caching location and algorithm asplug anddeno cache.
To useext/pip for python package management you simply usethe providedimport orinstall methods. The rest is handled automaticallyfor you! Just take a look!
import{pip}from"https://deno.land/x/python/ext/pip.ts";constnp=awaitpip.import("numpy");constplt=awaitpip.import("matplotlib","matplotlib.pyplot");constxpoints=np.array([1,8]);constypoints=np.array([3,10]);plt.plot(xpoints,ypoints);plt.show();
Check out the docshere.
This module uses FFI to interface with the Python interpreter's C API. So youmust have an existing Python installation (with the shared library), which issomething likepython310.dll, etc.
Python installed from Microsoft Store does not work, as it does not containshared library for interfacing with Python interpreter.
If the module fails to find Python, you can add the path to the Python in theDENO_PYTHON_PATH environment variable.
DENO_PYTHON_PATH if set, must point to full path including the file name ofthe Python dynamic library, which is likepython310.dll (Windows),libpython310.dylib (macOS) andlibpython310.so (Linux) depending onplatform.
Usage with docker is easiest done using thedenoland/deno:bin imagealong with theofficialpython image.
ARG DENO_VERSION=1.38.2ARG PYTHON_VERSION=3.12FROM denoland/deno:bin-$DENO_VERSION AS denoFROM python:$PYTHON_VERSION# Copy and configure denoCOPY --from=deno /deno /usr/local/bin/denoENTRYPOINT ["/usr/local/bin/deno"]# Copy your project sourceCOPY . .RUN ["run","-A","--unstable","https://deno.land/x/python@0.4.2/examples/hello_python.ts"]
- DjDeveloper (@DjDeveloperr)
- Elias Sjögreen (@eliassjogreen)
Pull request, issues and feedback are very welcome. Code style is formatted withdeno fmt and commit messages are done following Conventional Commits spec.
Copyright 2021, DjDeveloperr.
Copyright 2023, the Denosaurs team. All rights reserved. MIT license.
About
🐍 Python interpreter bindings for Deno and Bun.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors10
Uh oh!
There was an error while loading.Please reload this page.