Python
Requirements
Since v2.9, Tyk supports any currently stable Python 3.x version. The main requirement is to have the Python shared libraries installed. These are available as libpython3.x
in most Linux distributions.
- Python3-dev
- Protobuf: provides Protocol Buffers support
- gRPC: provides gRPC support
Install the Python development packages
If you’re using Ubuntu/Debian:
apt install python3 python3-dev python3-pip build-essential
If you’re using Red Hat or CentOS:
yum install python3-devel python3-setuptools
python3 -m ensurepip
Install the Required Python Modules
Make sure that “pip” is now available in your system, it should be typically available as “pip”, “pip3” or “pipX.X” (where X.X represents the Python version):
pip3 install protobuf grpcio
Python versions
Newer Tyk versions provide more flexibility when using Python plugins, allowing the users to set which Python version to use. By default, Tyk will try to use the latest version available.
To see the Python initialization log, run the Tyk gateway in debug mode.
To use a specific Python version, set the python_version
flag under coprocess_options
in the Tyk Gateway configuration file (tyk.conf).
Note
Tyk doesn’t support Python 2.x.
Troubleshooting
To verify that the required Python Protocol Buffers module is available:
python3 -c 'from google import protobuf'
No output is expected from this command on successful setups.
How do I write Python Plugins?
We have created a demo Python plugin repository.
The project implements a simple middleware for header injection, using a Pre hook (see Tyk custom middleware hooks. A single Python script contains the code for it, see middleware.py.