Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🐍 Python interpreter bindings for Deno and Bun.

License

NotificationsYou must be signed in to change notification settings

denosaurs/deno_python

Repository files navigation

Tagsdeno docchecksLicense

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.

Example

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>

Usage in Bun

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();

Dependencies

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();

Documentation

Check out the docshere.

Python Installation

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

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"]

Maintainers

Other

Contribution

Pull request, issues and feedback are very welcome. Code style is formatted withdeno fmt and commit messages are done following Conventional Commits spec.

Licence

Copyright 2021, DjDeveloperr.

Copyright 2023, the Denosaurs team. All rights reserved. MIT license.


[8]ページ先頭

©2009-2025 Movatter.jp