Skip to content

How to use Neptune in Google Colab#

Open in Colab

Google Colaboratory is a temporary runtime environment. By running your model training in a Colab notebook and keeping track of it with Neptune, you can log and access your metadata and artifacts even after the Colab kernel stops.

To use Neptune in your Colab notebook:

  1. Before your code cells, install the Neptune client library:

    %pip install -U neptune
  2. Import the Neptune client library:

    import neptune
  3. Set your Neptune API token:

    from getpass import getpass
    my_api_token = getpass("Enter your Neptune API token: ") # (1)!
    1. If you haven't registered, you can set it to neptune.ANONYMOUS_API_TOKEN to enable anonymous logging.
    How do I find my API token?

    In the bottom-left corner of the Neptune app, open the user menu and select Get your API token.

    You can copy your token from the dialog that opens. It's very long – make sure to copy and paste it in full!

  4. Set your project name:

    my_project = "workspace-name/project-name"
    How do I find my project name?

    Your full project name has the form workspace-name/project-name.

    For example, if your workspace name (shown in the top-left corner) is "ml-team" and your project is named "classification", your project string is: "ml-team/classification".

    To copy the name, click the menu in the top-right corner and select Edit project details.

  5. Initialize a Neptune object, such as a Run.

    Pass your API token and project variables as arguments:

    run = neptune.init_run(api_token=my_api_token, project=my_project)


    If you want to store your model metadata in the model registry, you can also work with Model objects. For instructions, see Model registry overview.

  6. Execute the cell and you should see a Neptune link printed to the console output.

    Click the link to see the metadata appear as it gets logged.

    Sample output

    [neptune] [info ] Neptune initialized. Open in the app:

    In the above example, the run ID is RUN-1.

  7. You can now use the run object to log values and metrics, upload files, and track artifacts.

    Try it out in the Colab example .

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


Jupyter Notebook example 

Setting your Neptune credentials as environment variables#

You can set your credentials with the os and getpass libraries:

import os
from getpass import getpass

os.environ["NEPTUNE_API_TOKEN"] = getpass("Enter your Neptune API token: ")
os.environ["NEPTUNE_PROJECT"] = "workspace-name/project-name"

Note that these won't persist when the notebook kernel is terminated. You'll need to declare the variables again if you restart the notebook.

Snapshotting source code#

In Colab, Neptune cannot access the notebook code from within the notebook itself.

You can use Neptune to snapshot the executed code in the following ways:

  • Run your notebook locally and pass its path to the source_files argument when initializing Neptune.
  • Work with Neptune in a Python script that you execute locally.