Custom packages in my Python middleware

Hello! I need to use redis package in my middleware written in Python.

I’m aware about get_data(key) API available for Python middleware. Unfortunately I need an access to HGET Redis operation which does not seem to be supported in API.

Question: how can I install Python packages of interest in Tyk?

Currently I managed to create my own Docker image of Tyk that includes package installation commands. Is there an officially recommended way for this?

Alternatively, is it posssible to add a support of HGET operation to middleware API?

Hi,

To install packages via the bundle you need to vendor them for python and add them to the bundile zip file. This involves creating a requirements.txt and using pip to put them into a vendor subdirectory. This works for me, but I’m no python expert. pip3 install -r requirements.txt --prefix vendor

The contents of the vendor directory can be added to the bundle zip file with something like zip -ur bundle.zip vendor/.

Lastly the python plugin needs to load modules from the vendor directory.

This works for me, but I’m using python 3.7 so it’s best to look in your vendor directory to get the path right

import os
import sys
bundle_dir = os.path.abspath(os.path.dirname(__file__))
for lib_dir in [ 'vendor/lib/python3.7/site-packages/' ]:
  vendor_dir = os.path.join(bundle_dir, lib_dir)
  sys.path.append(vendor_dir)

import my_vendored_module_name

However this won’t give you an HGET that piggy backs on Tyk’s redis connection like get_data() does. Your plugin code would need to initialise a redis connection from within the plugin to use HGET. It does free you to use another redis instance or even another DB if you want.

Cheers,
Pete

1 Like

Hi @Pete!

Thank you for the helpful comment, it works for me.

I just ran pip3 install ... within a Docker image inherited from a Tyk’s official one. This make me sure I’m using a proper version of python and its packages.