Problems with Python 3 Plugin

Plugin: Python 3 Script
Version: 2.0.2

I’m having two issues with the Python 3 Plugin

  1. How do I print to console/log? I followed the wiki but its not working:
logging.getLogger().setLevel(logging.INFO)
logging.info('testing')
  1. How do I install third party modules?

I added it via UI… Third-Party Modules: ["pretty_json"]

Then in my code call

from pretty_json import format_json

But get the following error:

ModuleNotFoundError: No module named ‘pretty_json’

@erick_cheng I think the only problem with pretty_json is the import needs modified just a bit. It should instead be ["pretty-json"] with a hyphen instead of underscore. Once that is updated in your connection the import should work!

def run(params={}):
    from pretty_json import format_json

    return {"output": format_json({'key': 'value'})}

Hi @zyoutz,

I figured out why the 3rd party modules aren’t working - the modules are installing on the wrong plugin version.

Modules are installing on 2.0.1 but workflows are running on 2.0.2. I was able to confirm this by execing into the respective docker containers and running pip list.

Is there a way to choose which plugin runs?

Screen Shot 2020-06-08 at 10.49.47

Screen Shot 2020-06-08 at 10.50.39

When searching and selecting a plugin, you have the ability to select which version will be used:
Screen Shot 2020-06-08 at 8.56.38 AM

Since the Python 3 dependencies are part of the connection configuration, you’ll want to make sure whatever connection is being used by the plugin has the necessary dependencies defined. It sounds like the actions in your workflow are using version 2.0.2 and don’t have a connection with the necessary dependencies defined.

Great, I was able to get a 2.0.2 connection by creating a new one. Is there no way to upgrade an existing connection? It’ll be a hassle if we need to update all of our workflows every time there’s a new plugin version.

Btw, do you know how to log messages? It’ll be useful for debugging.

I’ve followed the docs here but its not working:

To log messages with the Python plugin, import logging and call logging.info() or other logging methods in the run function. print or equivalent statements will not successfully log messages due to how output is handled by the plugin.

https://insightconnect.help.rapid7.com/docs/python-2-or-3-script

Is there no way to upgrade an existing connection?

If the plugin version update is a minor or patch (1.X.0 or 1.0.X) it’s pretty painless. The major patch versions (X.0.0) indicate the input or output changed, so they’re a little more painful to update.

Btw, do you know how to log messages? It’ll be useful for debugging.

To print to the logger, this should work.

def run(params={})
    import logging
    log = logging.getLogger("Test")
    log.info("Hello world")
2 Likes

Following up on what @joey_mcadams added about the logger. Currently when logging with the Python plugin, the message can be found within the container logs (eg. docker logs <CONTAINER_ID>); however, they won’t be returned in the step output within the InsightConnect UI. We’re doing a bit of digging to see how we can best facilitate this.

1 Like

@joey_mcadams @zyoutz thank you both for the help. That answers all my questions :slight_smile:

@joey_mcadams @zyoutz How do I update the the version of a pre-installed plugin?

I’m trying to use Third-Party Modules: ["beautifulsoup4==4.9.1"]

def run(params={}):
    import bs4
    logging.getLogger().setLevel(logging.INFO)
    logging.info(bs4.__version__)

Outputs: 4.6.0

Nvm, deleting the docker image somehow solved it :sweat_smile:

1 Like

you guys should really hae your imports at the top of your scripts so they run first, plus its more of a pythonic way to have them at the top

The way our plugin works you have to use imports inside the run function. So, this is the best way to utilize python with InsightConnect.