Skip to content

How to use Neptune with argparse#

You can use Neptune to log parameters defined with argparse .

Before you start#

  • Sign up at
  • Create a project for storing your metadata.
  • Have argparse and Neptune installed:

    pip install -U argparse neptune
    conda install -c conda-forge argparse neptune
Upgrading with neptune-client already installed

Important: To smoothly upgrade to the 1.0 version of the Neptune client library, first uninstall the neptune-client library and then install neptune.

pip uninstall neptune-client
pip install neptune

Logging parameters with argparse#

  1. Import Neptune and start a run:

    import neptune
    run = neptune.init_run()  # (1)!
    1. If you haven't set up your credentials, you can log anonymously:

  2. Specify and parse the arguments.

    import argparse
    argparser = argparse.ArgumentParser()
    argparser.add_argument("--lr", default=0.01)
    argparser.add_argument("--batch", default=32)
    argparser.add_argument("--activation", default="ReLU")
    args = argparser.parse_args()
  3. Log the parsed arguments to a namespace in the run:

    run["parameters"] = args

    Inside the namespace, Neptune creates a field for each argument.

  4. To stop the connection to Neptune and sync all data, call the stop() method:

  5. Run your script as you normally would.

Sample output

The general format is<workspace>/<project> followed by the Neptune ID of the initialized object.

If Neptune can't find your project name or API token

As a best practice, you should save your Neptune API token and project name as environment variables:

export NEPTUNE_API_TOKEN="h0dHBzOi8aHR0cHM6Lkc78ghs74kl0jvYh3Kb8"
export NEPTUNE_PROJECT="ml-team/classification"

You can, however, also pass them as arguments when initializing Neptune:

run = neptune.init_run(
    api_token="h0dHBzOi8aHR0cHM6Lkc78ghs74kl0jvYh3Kb8",  # your token here
    project="ml-team/classification",  # your full project name here
  • API token: In the bottom-left corner, expand the user menu and select Get my API token.
  • Project name: in the top-right menu: Edit project details.

If you haven't registered, you can also log anonymously to a public project (make sure not to publish sensitive data through your code!):

run = neptune.init_run(

Follow the run link and explore the logged parameters in the All metadata dashboard.

Logging with Namespace()#

You can also use Namespace() to define the args:

from argparse import Namespace

args = Namespace(

args = argparser.parse_args()
run["parameters"] = args