Using external modules in Code Node with Python - ModuleNotFoundError

Hello Everyone!

I’m a new user with n8n, and I was testing this tool with a simple workflow.

in my case I receive erros when I use node Code with python language and import module.

I defined the variable that permit using external modules and I was installed the modules in my Dockerfile.

So how can I solve this problem? I searched a lot but nothing solved. I saw about using execute command node, however using a code node is better in my vision.

image

Here’s my setup:

  • n8n is running in Docker in wsl Linux - Ubuntu.
  • Docker port mapping: 5678:5678
  • Image: latest
  • N8N_VERSION=1.89.2 I tried with latest before

Error detailed:

Stack trace

Error: ModuleNotFoundError: No module named 'boto3'

    at PythonSandbox.getPrettyError (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/PythonSandbox.js:91:14)
    at PythonSandbox.runCodeInPython (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/PythonSandbox.js:78:18)
    at PythonSandbox.runCodeAllItems (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/PythonSandbox.js:49:29)
    at ExecuteContext.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/Code.node.js:155:17)
    at WorkflowExecute.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/execution-engine/workflow-execute.js:681:27)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/execution-engine/workflow-execute.js:915:51
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/execution-engine/workflow-execute.js:1246:20

I just saw that python code supports n8n provides Python support using Pyodide, however there is no boto3 here

@Marcos_Daniel_Santos Welcome to the community! :blush:

You need to manually install the required npm or Python packages inside the Docker container, as n8n does not handle this automatically. or Build a Custom Dockerfile with the N8N Image

After installation, you can allow these packages by specifying them in the NODE_FUNCTION_ALLOW_EXTERNAL environment variable.

Hi Marcos,

I ran into the same confusion when I first tested Python in n8n as well — especially with external packages like boto3.

Just to clarify: even if you define NODE_FUNCTION_ALLOW_EXTERNAL=boto3,requests,pandas in your environment, n8n won’t install those packages automatically inside the Docker container. That variable only allows them once they’re installed.

To get boto3 working, here’s what I did:

  1. Extend the n8n Docker image by creating a custom Dockerfile where I install the Python packages I need using pip:

FROM n8nio/n8n:latest

RUN apt-get update && apt-get install -y python3-pip
RUN pip3 install boto3 requests pandas

  1. Then I rebuilt the image and used that custom image when starting n8n:

docker build -t n8n-custom .
docker run -it --rm
-e NODE_FUNCTION_ALLOW_EXTERNAL=boto3,requests,pandas
-p 5678:5678
n8n-custom

  1. After that, I was able to use Python code in the Code node with import boto3 without issues.

Alternatively, if you’re using Pyodide, keep in mind that it doesn’t support all native packages like boto3, because it’s a WebAssembly-based Python interpreter running in the browser context.

Let me know if you want help setting up the custom Docker image — happy to share a sample repo.

Hi Buddies!

I’m used a custom Dockerfile to build image, the packages was installed but isn’t recognized in n8n code node.

This is my Dockerfile, my problem is to understand the reason that n8n don’t recognize these modules.

FROM n8nio/n8n:latest

USER root

# Install Python and pip using apk
RUN apk update && apk add --no-cache python3 py3-pip

# Create the virtual environment
RUN python3 -m venv /opt/venv

# Activate the virtual environment 
RUN . /opt/venv/bin/activate && \
pip install --no-cache-dir boto3 requests pandas

# Add venv to path
ENV PATH="/opt/venv/bin:$PATH"

# Switch back to node user
USER node

I will check the documentation again

Hi Eric, how are you?

This image don’t use the apt to manage packages.

I’m using apk to addpy3-pip and pip to install the boto3, requests and pandas.

i think this should help you: How to install python libraries with Docker image to use them in n8n?