kuda.ai

Thoughts on programming and music theory.

dark mode

Generate an SBOM of your Python code from your Python shell

Created on November 11, 2026

python

You can run pip install cyclonedx-bom and cyclonedx-py environment from your Python shell.

In a Python shell:

Check your working directory:

import os

os.getcwd()

os.listdir()

Run pip install cyclonedx-bom:

import subprocess

subprocess.run(["pip", "install", "cyclonedx-bom"], check=True)

Generate SBOM and write to file:

f = open("sbom.json", "w")

subprocess.run(
    ["cyclonedx-py", "environment"],
    stdout=f,
    stderr=subprocess.STDOUT,
    check=True,
)

f.close()

Note: you could use a context manager, but in the shell, I prefer to open and close directly on the file object.

with open("sbom.json", "w") as f:
    subprocess.run ...

Links:

To upload to dependency track as a new project, curl it:

curl --fail-with-body --request POST \
    "${DT_URL}/api/v1/bom" \
    --header "X-Api-Key: ${DT_API_KEY}" \
    --form "autoCreate=true" \
    --form "projectName=Project.Backend.Code" \
    --form "projectVersion=main" \
    --form "parentName=Project" \
    --form "parentVersion=main" \
    --form "isLatest=true" \
    --form "bom=@sbom.json"

or upload it as existing project in dependency track:

curl --fail-with-body --request POST \
    "${DT_URL}/api/v1/bom" \
    --header "X-Api-Key: ${DT_API_KEY}" \
    --form "project=${PROJECT_ID}" \
    --form "bom=@application-sbom.json"