Skip to content

Install the Lightrun Python agent🔗

The Lightrun agent is at the core of the Lightrun platform. It's the component that communicates requests for runtime observability to your running code, and gathers and relays the requested data back to the Lightrun Server, and eventually the developer's IDE.

Before running the agent, it must be installed on the system where your code to be monitored is running, and its credentials must be declared either inside the application code, or in an agent.config file.

Before you begin🔗

  • Version support

    Lightrun supports Python 3.10 through 3.14 on x86-64 and ARM64, including the AWS Lambda runtime. Windows support is x86-64. Python 3.12 requires Lightrun Python agent version 1.96 or later, Python 3.13 requires version 1.97 or later, and Python 3.14 requires version 1.98 or later. For Python 3.13 and Python 3.14 limitations and operating system and architecture compatibility, see the Python agent system requirements.

  • Review the system requirements Check the Python agent system requirements here.

Important

Make sure no debugger is connected when using Lightrun.

The Lightrun Python agent cannot run alongside other debuggers, such as pdb, debugpy, or IDE-integrated debuggers. If another debugger is attached to the Python process, Lightrun operations may not be triggered.

Lightrun can be installed both directly and inside of a Docker container.

Direct installation🔗

  1. Install the python agent by running python -m pip install lightrun.

  2. Import Lightrun inside your main function, by adding the following code at the beginning of the function.

    try:
         import lightrun
         lightrun.enable(company_key="<COMPANY_SECRET>")
    except ImportError as e:
         print("Error importing Lightrun: ", e)
    

    Getting Company Details

    You can get your <COMPANY_SECRET> and <server_url> key by logging into the Management Portal and inspecting the Set up an agent section.

    If Hide agent setup from WebUI is enabled for your organization, get the agent pool API key from Settings > Identity and Access Management > Agent Pools instead. For more information, see Service Configuration and Manage Agent Pools.

    Note

    Other configuration parameters can also be passed here. For example, add the server_url parameter to the lightrun.enable command when using Lightrun on-prem.

    try:
        import lightrun
        lightrun.enable(company_key="<COMPANY_SECRET>", com_lightrun_server="<server_url>")
    except ImportError as e:
        print("Error importing Lightrun: ", e)
    
  3. Run the application as you normally would; for example, python app.py.

Important

  • Lightrun actions in Python must only be applied to a function before the function is called. Actions inserted into a Python function while the function is currently executing will only take effect once the function is exited and called again. This also means that you can not use Lightrun actions in a Python program's __main__ method as it is already running when the Lightrun agent initializes.
  • If your Python program doesn’t have a main() method and all the code is in one scope, you will not be able to use Lightrun effectively. It is necessary to introduce a main() method that triggers the other methods that you want to debug with Lightrun.

    For example, you won't be able to debug with Lightrun in the following code:

    def main():
        // code which does not include calls to other methods -- Lightrun actions won't work
    
        // ONLY code in methods which are called anew after the Lightrun action has been placed will have a chance to be observed
    
    if __name__ == '__main__':
        main()
    

    To use Lightrun, you need to have other methods that are triggered by the main() method.

    def echo():
        //  some logic/algorithm
        // Lightrun actions will work in this method, for invocations of this method that happen AFTER the Lightrun action is created
    
    def main():
        // Lightrun actions won't work in this method
        echo()
    
    if __name__ == '__main__':
        main()
    

Run as command line argument🔗

You can also install a Lightrun agent to your application by specifying your Lightrun credentials as environment variables when you start your Python application from your command-line interface (CLI).

Note

This option does not work for Gunicorn applications.

  1. ​Open a terminal and change to the working directory where your project is located.
  2. Install Lightrun in your application’s folder.

    python -m pip install lightrun
    
  3. Run your application with your Lightrun credentials.

    python -m lightrun --com_lightrun_server=<server_url> --company_key=<COMPANY_SECRET> -- app.py
    

Change <server_url> to your Lightrun server URL, app.py to the name of your python file, and <COMPANY_SECRET> to your Lightrun company key.

Getting Company Details

You can get your <COMPANY_SECRET> and <server_url> key by logging into the Management Portal and inspecting the Set up an agent section.

If Hide agent setup from WebUI is enabled for your organization, get the agent pool API key from Settings > Identity and Access Management > Agent Pools instead. For more information, see Service Configuration and Manage Agent Pools.

Docker installation🔗

Docker containers are ephemeral.

If you were to create a shell into a Docker container (by docker exec -it <container-id>, for example) and add the Lightrun files there, they would disappear the next time the container would spin up due to the ephemeral nature of that container.

Instead, we suggest you install Lightrun by adding it to the underlying Docker image directly, i.e. by "baking" the agent into the image.

This is an example Dockerfile that installs Lightrun with a simple dummy application, assuming the credentials were added to the source code of your application as shown above:

FROM python:3.14
RUN pip install lightrun
COPY prime_main.py /app/prime_main.py
CMD ["python", "/app/prime_main.py"]

If you prefer passing the credentials via the command line, here's how to do it inside a Dockerfile:

FROM python:3.14
RUN pip install lightrun
COPY prime_main.py /app/prime_main.py
CMD ["python", "-m", "lightrun", "--company_secret=<COMPANY_SECRET>", "--", "app/prime_main.py"]

Note: Add the server_url parameter when using Lightrun on-prem.

Important

Installing Lightrun in this fashion does not absolve you of the need to import Lightrun to your application. Please make sure to add Lightrun as an import to your application file.

Lightrun in Alpine Linux🔗

Because google-re2, a dependency of Lightrun, does not publish a musllinux wheel, Alpine images must build it from source. The following multi-stage build keeps the required compiler and development packages out of the final image.

The wheel-builder stage compiles a google-re2 wheel (google-re2==1.1.20251105). The final image installs the runtime libraries libstdc++ and re2, then installs Lightrun with --find-links=/wheels so pip uses that wheel.

FROM python:3.14-alpine AS wheel-builder

# google-re2 does not publish a musllinux wheel, so build one for Alpine.
# This stage keeps the compiler and development packages out of the final image.
RUN apk add --no-cache \
    g++ \
    py3-pybind11-dev \
    re2-dev

RUN pip wheel \
    --no-deps \
    --wheel-dir /wheels \
    google-re2==1.1.20251105


FROM python:3.14-alpine

# Shared libraries required at runtime by Lightrun and google-re2.
RUN apk add --no-cache \
    libstdc++ \
    re2

COPY --from=wheel-builder /wheels /wheels

# Install Lightrun using the locally built google-re2 wheel.
RUN pip install \
    --no-cache-dir \
    --find-links=/wheels \
    lightrun \
 && rm -rf /wheels

COPY prime_main.py /app/prime_main.py

CMD ["python", "/app/prime_main.py"]

The sample uses the python:3.14-alpine base image. To run another supported Python version, change both FROM lines to the matching python:<version>-alpine tag. Lightrun supports Python 3.10 through 3.14. For operating system requirements, including supported Alpine versions, see the Python agent system requirements.


Last update: December 2, 2026