Import python library with n8nio/n8n:PR-4295-python-code-node : Not found

Hello,

I have created from n8nio/n8n:PR-4295-python-code-node a new image for using Python Node and some customs library…

FROM n8nio/n8n:PR-4295-python-code-node
WORKDIR /data

COPY requirements.txt requirements.txt

USER root
RUN apk add --update  py-pip  automake gcc g++ subversion python3-dev
RUN pip3 install -r requirements.txt

requirements.txt : Contains “pdfkit”

I build the image, named n8n-python :
docker build -t n8n-python -f DockerFile .

After that, I run it, and open a terminal :
python -c "import pdfkit" => OK

At the end, I try to create a Python Node with the “pdfkit”…

import pyodide_js
await pyodide_js.loadPackage('pdfkit')

ERROR: JsException: Error: No known package with name ‘pdfkit’

import pdfkit

ERROR: ModuleNotFoundError: No module named ‘pdfkit’

I have also used the field defining the imported libraries :

ERROR: Traceback (most recent call last): File "/lib/python3.10/asyncio/futures.py", line 201, in result raise self._exception File "/lib/python3.10/asyncio/tasks.py", line 234, in __step result = coro.throw(exc) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 573, in install await transaction.gather_requirements(requirements) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 333, in gather_requirements await gather(*requirement_promises) File "/lib/python3.10/asyncio/futures.py", line 284, in __await__ yield self # This tells Task to wait for completion. File "/lib/python3.10/asyncio/tasks.py", line 304, in __wakeup future.result() File "/lib/python3.10/asyncio/futures.py", line 201, in result raise self._exception File "/lib/python3.10/asyncio/tasks.py", line 232, in __step result = coro.send(None) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 340, in add_requirement return await self.add_requirement_inner(Requirement(req)) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 432, in add_requirement_inner metadata = await _get_pypi_json(req.name, self.fetch_kwargs) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 48, in _get_pypi_json metadata = await fetch_string(url, fetch_kwargs) File "/lib/python3.10/site-packages/micropip/_compat_in_pyodide.py", line 35, in fetch_string return await (await pyfetch(url, **kwargs)).string() File "/lib/python3.10/pyodide/http.py", line 251, in pyfetch url, await _jsfetch(url, to_js(kwargs, dict_converter=Object.fromEntries)) NameError: name '_jsfetch' is not defined

what did I forget ?
Thanks :wink:

Hi @home, that pull request isn’t finalized yet I am afraid. Meaning I am not sure about the current status, and I am also not familiar with this Python package specifically.

Seeing you are using a custom image anyway you could simply consider installing a standard python binary and then run your Python scripts via the Execute Command node here.

@home Could the problem be that the module gets installed for user root, but n8n is actually running as user node?

Regarding

import pyodide_js
await pyodide_js.loadPackage('pdfkit')

That is nothing you actually have to run yourself. n8n will run that automatically when you write pdfkit in the “Python Modules” parameter.

And there is also a new Docker image which you should start to use moving forward. It is the v1 release cancidate one: docker.n8n.io/n8nio/n8n:1.0.0-rc

1 Like

Hi, is there any update on this?

I want to use the ‘cadquery’ libary in a Code node with Python.

I have created a Dockerfile to extend the existing image.

FROM n8nio/n8n

WORKDIR /data

COPY requirements.txt requirements.txt

USER root

RUN apk add --update py-pip automake gcc g++ subversion python3-dev

RUN pip3 install -r requirements.txt

As far as I understand it now, the code node does not use python but Pyodide, so I can not add external packages to that?

What would be the right way to do that today? We run 1.0.5

I can run my custom script using the Execute Command node. How would I hand over my binary data in this case?

thank you!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

@JanMrlth1 You would have to build a Python wheel for that package. You can find information about it here:
https://pyodide.org/en/stable/development/new-packages.html#building-python-wheels-out-of-tree