Running a python script that's in a virtualenv


I have a situation where I need to run a Python script that is sitting in a python virtualenv. Is it possible to do this through the EXECUTE node? Our server is a Ubuntu 20.04 VM running in an AWS EC2.

Thanks in advance!

Sorry my Python days lie sadly a long time behind. So can you please elaborate.

But in the end if you are able to run a python script via the command line you can also execute it the same way via the Execute Command node as long as n8n has access to it.

We have the n8n instance in a docker environment. I guess we have to “break” out of the docker environment? (Sorry I’m a complete docker noob) to access the virtual python instance?

We have a python script that does a bunch of calculations and pulls in data from other sources returning a JSON package as output. Ideally, I’ll like to execute that python script and capture the JSON results.

In this case you can mount that folder into the docker instance with -v. You can find more information here:

Basically will you add something like -v /external:/internal. You can then access the data of /external in your docker instance via the folder /internal.

Thanks that did the trick!

Hi @jan,

I have run into bit of a glitch with this approach.

I have my n8n instance running in a docker container. A folder in the file system has some python code I’d like to execute. I have mounted the /external dir on the /internal via - v. The execute node is able to call the script.

The issue is - the execution fails at importing the packages and modules despite having those installed via pip3. I’m guessing it’s happening because the docker container isn’t seeing those packages and modules in its env, but not sure how to fix it.

Could you or any one in the community assist please?
By the way, this is a setup running on a raspberry pi with a Debian os.

Thank you.

Any pointers to resolve this?

@chris did something similar and might be able to help you :slight_smile:

1 Like

Hi Rony,
you may need to add your venv directory e.g. via #! /internal/myenv/bin/python - I’m not sure exactly how n8n calls the python scripts (still haven’t done that), but I’d expect it to wrap them and execute them via ./

You can print the path you are in:

import sys

and the pip list like this from a script:

from pip import _internal

Either drop me a DM, always happy to connect with the Python folks here.


1 Like

Hey @chris

When the docker container didn’t detect the imported packages I mounted the host’s package dir like “-v /usr/local/lib/python3.9/dist-packages:/root/.n8n/workflows”, but that didn’t work either :slight_smile:

I’ll try what you recommended and reach out via DM.

Thank you for your response.

Great, I’d:

  • create the venv in the folder you are going to mount
  • add #! line to the script to point the python executable in the bin folder of the venv
  • it should load the packages from /lib folder automatically.
  • debug with scripts above to see if you get docker python or the venv python.