# How do I print the wandb sweep url in python?

**URL:** <https://community.wandb.ai/t/how-do-i-print-the-wandb-sweep-url-in-python/4133>\
**Category:** W&B Help\
**Tags:** sweeps\
**Created:** [March 27, 2023, 4:42am UTC](https://community.wandb.ai/t/how-do-i-print-the-wandb-sweep-url-in-python/4133 "2023-03-27T04:42:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![brando](https://sea2.discourse-cdn.com/flex020/user_avatar/community.wandb.ai/brando/32/199_2.png) [@brando](https://community.wandb.ai/u/brando)\
**Post date:** [March 27, 2023, 4:42am UTC](https://community.wandb.ai/t/how-do-i-print-the-wandb-sweep-url-in-python/4133/1 "2023-03-27T04:42:56Z")

</div>

For runs I do:

```auto
wandb.run.get_url()

```

how do I do the same but for sweeps given the `sweep_id`?

* * *

fulls sample run:

```auto
"""
Main Idea:
- create sweep with a sweep config & get sweep_id for the agents (note, this creates a sweep in wandb's website)
- create agent to run a setting of hps by giving it the sweep_id (that mataches the sweep in the wandb website)
- keep running agents with sweep_id until you're done

note:
    - Each individual training session with a specific set of hyperparameters in a sweep is considered a wandb run.

ref:
    - read: https://docs.wandb.ai/guides/sweeps
"""

import wandb
from pprint import pprint
import math
import torch

sweep_config: dict = {
    "project": "playground",
    "entity": "your_wanbd_username",
    "name": "my-ultimate-sweep",
    "metric":
        {"name": "train_loss",
         "goal": "minimize"}
    ,
    "method": "random",
    "parameters": None, # not set yet
}

parameters = {
    'optimizer': {
        'values': ['adam', 'adafactor']}
    ,
    'scheduler': {
        'values': ['cosine', 'none']} # todo, think how to do
    ,
    'lr': {
        "distribution": "log_uniform_values",
        "min": 1e-6,
        "max": 0.2}
    ,
    'batch_size': {
        # integers between 32 and 256
        # with evenly-distributed logarithms
        'distribution': 'q_log_uniform_values',
        'q': 8,
        'min': 32,
        'max': 256,
    }
    ,
    # it's often the case that some hps we don't want to vary in the run e.g. num_its
    'num_its': {'value': 5}
}
sweep_config['parameters'] = parameters
pprint(sweep_config)

# create sweep in wandb's website & get sweep_id to create agents that run a single agent with a set of hps
sweep_id = wandb.sweep(sweep_config)
print(f'{sweep_id=}')

def my_train_func():
    # read the current value of parameter "a" from wandb.config
    # I don't think we need the group since the sweep name is already the group
    run = wandb.init(config=sweep_config)
    print(f'{run=}')
    pprint(f'{wandb.config=}')
    lr = wandb.config.lr
    num_its = wandb.config.num_its

    train_loss: float = 8.0 + torch.rand(1).item()
    for i in range(num_its):
        # get a random update step from the range [0.0, 1.0] using torch
        update_step: float = lr * torch.rand(1).item()
        wandb.log({"lr": lr, "train_loss": train_loss - update_step})
    run.finish()

# run the sweep, The cell below will launch an agent that runs train 5 times, usingly the randomly-generated hyperparameter values returned by the Sweep Controller.
wandb.agent(sweep_id, function=my_train_func, count=5)

```

cross: [machine learning - How do I print the wandb sweep url in python? - Stack Overflow](https://stackoverflow.com/questions/75852199/how-do-i-print-the-wandb-sweep-url-in-python)

---

<div class="post-metadata">

**Author:** ![corey-strausman](https://sea2.discourse-cdn.com/flex020/user_avatar/community.wandb.ai/corey-strausman/32/1561_2.png) [@corey-strausman](https://community.wandb.ai/u/corey-strausman)\
**Post date:** [March 27, 2023, 1:26pm UTC](https://community.wandb.ai/t/how-do-i-print-the-wandb-sweep-url-in-python/4133/2 "2023-03-27T13:26:08Z")

</div>

Hi @brando thank you for your question. Here is how you can get\_sweep\_url:

```auto
get_sweep_url() -> Optional[str]

```

You can find more [in our docs](https://docs.wandb.ai/ref/python/run#get_sweep_url). I hope this helps!

---

<div class="post-metadata">

**Author:** ![brando](https://sea2.discourse-cdn.com/flex020/user_avatar/community.wandb.ai/brando/32/199_2.png) [@brando](https://community.wandb.ai/u/brando)\
**Post date:** [March 27, 2023, 4:27pm UTC](https://community.wandb.ai/t/how-do-i-print-the-wandb-sweep-url-in-python/4133/3 "2023-03-27T16:27:29Z")

</div>

like `wandb.get_sweep_url()`? Thanks!

[Run | Weights & Biases Documentation](https://docs.wandb.ai/ref/python/run?_gl=1*uk130d*_ga*MTYwMTE3MDYzNS4xNjUyMjI2MTE1*_ga_JH1SJHJQXJ*MTY4MDAxNTk2Ny4yNjguMS4xNjgwMDE2MTMyLjQ3LjAuMA)…#get\_sweep\_url

---

<div class="post-metadata">

**Author:** ![brando](https://sea2.discourse-cdn.com/flex020/user_avatar/community.wandb.ai/brando/32/199_2.png) [@brando](https://community.wandb.ai/u/brando)\
**Post date:** [March 31, 2023, 12:29am UTC](https://community.wandb.ai/t/how-do-i-print-the-wandb-sweep-url-in-python/4133/4 "2023-03-31T00:29:01Z")

</div>

@corey-strausman

```auto
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1496, in _exec
    pydev_imports.execfile(file, globals, locals) # execute the script
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/brandomiranda/ultimate-utils/tutorials_for_myself/my_wandb_uu/my_wandb_sweeps_uu/sweep_everything_in_python_even_config/sweep_everything_in_python.py", line 64, in <module>
    wandb.get_sweep_url()
AttributeError: module 'wandb' has no attribute 'get_sweep_url'

```

---

<div class="post-metadata">

**Author:** ![brando](https://sea2.discourse-cdn.com/flex020/user_avatar/community.wandb.ai/brando/32/199_2.png) [@brando](https://community.wandb.ai/u/brando)\
**Post date:** [March 31, 2023, 12:33am UTC](https://community.wandb.ai/t/how-do-i-print-the-wandb-sweep-url-in-python/4133/5 "2023-03-31T00:33:20Z")

</div>

didn’t work even after pip install --upgrade wandb

```auto
(pycoq-ejgallego) brandomiranda~/pycoq-ejgallego ❯ pip install --upgrade wandb

Requirement already satisfied: wandb in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (0.13.10)
Collecting wandb
  Using cached wandb-0.14.0-py3-none-any.whl (2.0 MB)
Requirement already satisfied: typing-extensions in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (4.5.0)
Requirement already satisfied: protobuf!=4.21.0,<5,>=3.19.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (4.22.0)
Requirement already satisfied: docker-pycreds>=0.4.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (0.4.0)
Requirement already satisfied: GitPython!=3.1.29,>=1.0.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (3.1.31)
Requirement already satisfied: sentry-sdk>=1.0.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (1.15.0)
Requirement already satisfied: Click!=8.0.0,>=7.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (8.1.3)
Requirement already satisfied: setuptools in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (65.6.3)
Requirement already satisfied: PyYAML in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (6.0)
Requirement already satisfied: pathtools in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (0.1.2)
Requirement already satisfied: setproctitle in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (1.3.2)
Requirement already satisfied: requests<3,>=2.0.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (2.28.2)
Requirement already satisfied: psutil>=5.0.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (5.9.4)
Requirement already satisfied: appdirs>=1.4.3 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (1.4.4)
Requirement already satisfied: six>=1.4.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from docker-pycreds>=0.4.0->wandb) (1.16.0)
Requirement already satisfied: gitdb<5,>=4.0.1 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from GitPython!=3.1.29,>=1.0.0->wandb) (4.0.10)
Requirement already satisfied: idna<4,>=2.5 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from requests<3,>=2.0.0->wandb) (3.4)
Requirement already satisfied: charset-normalizer<4,>=2 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from requests<3,>=2.0.0->wandb) (3.0.1)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from requests<3,>=2.0.0->wandb) (1.26.14)
Requirement already satisfied: certifi>=2017.4.17 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from requests<3,>=2.0.0->wandb) (2022.12.7)
Requirement already satisfied: smmap<6,>=3.0.1 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from gitdb<5,>=4.0.1->GitPython!=3.1.29,>=1.0.0->wandb) (5.0.0)
Installing collected packages: wandb
  Attempting uninstall: wandb
    Found existing installation: wandb 0.13.10
    Uninstalling wandb-0.13.10:
      Successfully uninstalled wandb-0.13.10
Successfully installed wandb-0.14.0

```

---

<div class="post-metadata">

**Author:** ![luis\_bergua](https://avatars.discourse-cdn.com/v4/letter/l/dbc845/32.png) [@luis\_bergua](https://community.wandb.ai/u/luis_bergua)\
**Post date:** [April 1, 2023, 9:49am UTC](https://community.wandb.ai/t/how-do-i-print-the-wandb-sweep-url-in-python/4133/6 "2023-04-01T09:49:22Z")

</div>

Hi @brando, you can get the sweep id by using our [public API](https://docs.wandb.ai/ref/python/public-api/sweep) like:

```auto
import wandb
api = wandb.Api()
sweep = api.sweep('entity/project/sweep_id')
sweep.url

```

Please let me know if this works!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/wandb/original/1X/366b649231631dbab896843020da0056074ac79d.png) [@system](https://community.wandb.ai/u/system)\
**Post date:** [May 31, 2023, 9:50am UTC](https://community.wandb.ai/t/how-do-i-print-the-wandb-sweep-url-in-python/4133/7 "2023-05-31T09:50:09Z")

</div>

Closed on May 31, 2023, 9:50am UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
