For general information on how the Python agent API calls relate to each other, see the API guide.

newrelic.agent.initialize(config_file=None, environment=None, ignore_errors=None, log_file=None, log_level=None)
Initializes the Python agent with a specified config file during a manual integration process.


This is called to initialize the New Relic Python agent with a specified configuration file when the agent is being manually integrated with a Python application.

For best instrumentation results, make this call as early as possible in your app code. Unlike typical Python functionality, the order of import matters. For WSGI and application script files, place the initialize call before all imports, with the exception of the sys import and updates to the sys.path. If you call initialize multiple times, the agent ignores calls after the first if the configuration file and environment options are the same. If the options differ, an exception is raised.

If you call initialize with no arguments, you must have already specified your license key with the NEW_RELIC_LICENSE_KEY environment variable. Alternatively, you can set NEW_RELIC_CONFIG_FILE and NEW_RELIC_ENVIRONMENT. The agent will then read those values in place of the missing arguments.


Parameter Description



Optional. The path to the New Relic config file. If you supply a configuration file, you can override elements of that config file with the optional environment parameter.



Optional. Indicates the name of the environment. If you are using a configuration file, you can override the settings in that file with environment.



Optional. Indicates whether to ignore startup errors. Default is True.



Optional. Path to the agent log file. Also accepts stdout and stderr (same as the log_file option in the config file).


int or applicable logging parameter type

Optional. Sets the logging level. New Relic uses Python's logging module. Options are the same as for the log_level option in config file.

Return value(s)



Initializing with config file and environment

Here's an example of initializing the Python agent with a config file and an environment string:

newrelic.agent.initialize('/etc/newrelic/newrelic.ini', 'production')

Using all parameter values

Calling with all parameter values:

newrelic.agent.initialize('/etc/newrelic/newrelic.ini', 'production', ignore_errors=False, log_file='/var/log/newrelic.log', log_level=logging.DEBUG)

For more help

Recommendations for learning more: