Repository: Lightning-AI/pytorch-lightning (Python)

Issue:
Recent project name in WandbLogger ignores env var WANDB_PROJECT
### Bug description

This change https://github.com/Lightning-AI/lightning/pull/14145/files#diff-fb7a6ccc19c8344d78090c02d77175ad3f9699a8bc0e4ab26345855023ce3c12R37-R42 didn't take into account Weights and Biases users using the `WANDB_PROJECT` environmental variable to set the project name https://docs.wandb.ai/guides/track/advanced/environment-variables


### How to reproduce the bug

```python
export WANDB_PROJECT=my-project-name
# launch training job
```


### Error messages and logs

Result: `WANDB_PROJECT` gets ignored and overwritten by the new default `project: str = "lightning_logs",` https://github.com/Lightning-AI/lightning/blob/46548337def198216b4e3000ef08b06c9c11816d/src/pytorch_lightning/loggers/wandb.py#L302

### Environment

<details>
  <summary>Current environment</summary>

```
#- Lightning Component (e.g. Trainer, LightningModule, LightningApp, LightningWork, LightningFlow): Trainer
#- PyTorch Lightning Version (e.g., 1.5.0): 1.8.4.post0
#- PyTorch Version (e.g., 1.10): 1.12.1
#- Python version (e.g., 3.9): 3.8.13
#- OS (e.g., Linux): Linux
#- CUDA/cuDNN version: 515.65.01 CUDA Version: 11.7
#- How you installed Lightning(`conda`, `pip`, source): pip
#- Running environment of LightningApp (e.g. local, cloud): local
```

</details>


### More info

_No response_

cc @awaelchli @morganmcg1 @borisdayma @scottire @manangoel99


Discussion:
@elgalu Thanks for reporting. I think we want to keep the default at lightning_logs, but perhaps need to handle the special case of the env var. 
Are you interested in making a contribution? :) 
Maybe someone from WandB with experience in not hard-coding project names https://github.com/wandb/wandb/commit/238d3a6b51397305d60916fac851d2bae5ccc1e4 like @raubitsj
Hi! I can take this one



Patch:
diff --git a/src/lightning/pytorch/CHANGELOG.md b/src/lightning/pytorch/CHANGELOG.md
index e78a5f2ee5e96..e60013da7b248 100644
--- a/src/lightning/pytorch/CHANGELOG.md
+++ b/src/lightning/pytorch/CHANGELOG.md
@@ -123,6 +123,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
 - Fixed a formatting issue when the filename in `ModelCheckpoint` contained metrics that were substrings of each other ([#17610](https://github.com/Lightning-AI/lightning/pull/17610))
 
 
+- Fixed `WandbLogger` ignoring the `WANDB_PROJECT` environment variable ([#16222](https://github.com/Lightning-AI/lightning/pull/16222))
+
+
 ## [2.0.1.post0] - 2023-04-11
 
 ### Fixed
diff --git a/src/lightning/pytorch/loggers/wandb.py b/src/lightning/pytorch/loggers/wandb.py
index 10570bbbc2b0a..d5f928bd8e69d 100644
--- a/src/lightning/pytorch/loggers/wandb.py
+++ b/src/lightning/pytorch/loggers/wandb.py
@@ -260,7 +260,8 @@ def any_lightning_module_function_or_hook(self):
         dir: Same as save_dir.
         id: Same as version.
         anonymous: Enables or explicitly disables anonymous logging.
-        project: The name of the project to which this run will belong.
+        project: The name of the project to which this run will belong. If not set, the environment variable
+            `WANDB_PROJECT` will be used as a fallback. If both are not set, it defaults to ``'lightning_logs'``.
         log_model: Log checkpoints created by :class:`~lightning.pytorch.callbacks.ModelCheckpoint`
             as W&B artifacts. `latest` and `best` aliases are automatically set.
 
@@ -293,7 +294,7 @@ def __init__(
         dir: Optional[_PATH] = None,
         id: Optional[str] = None,
         anonymous: Optional[bool] = None,
-        project: str = "lightning_logs",
+        project: Optional[str] = None,
         log_model: Union[str, bool] = False,
         experiment: Union[Run, RunDisabled, None] = None,
         prefix: str = "",
@@ -334,6 +335,8 @@ def __init__(
         elif dir is not None:
             dir = os.fspath(dir)
 
+        project = project or os.environ.get("WANDB_PROJECT", "lightning_logs")
+
         # set wandb init arguments
         self._wandb_init: Dict[str, Any] = {
             "name": name,


Test patch:
diff --git a/tests/tests_pytorch/loggers/test_wandb.py b/tests/tests_pytorch/loggers/test_wandb.py
index b90581d443523..e2213265475bb 100644
--- a/tests/tests_pytorch/loggers/test_wandb.py
+++ b/tests/tests_pytorch/loggers/test_wandb.py
@@ -29,10 +29,20 @@
 @mock.patch("lightning.pytorch.loggers.wandb.Run", new=mock.Mock)
 @mock.patch("lightning.pytorch.loggers.wandb.wandb")
 def test_wandb_project_name(*_):
-    logger = WandbLogger()
+    with mock.patch.dict(os.environ, {}):
+        logger = WandbLogger()
     assert logger.name == "lightning_logs"
 
-    logger = WandbLogger(project="project")
+    with mock.patch.dict(os.environ, {}):
+        logger = WandbLogger(project="project")
+    assert logger.name == "project"
+
+    with mock.patch.dict(os.environ, {"WANDB_PROJECT": "env_project"}):
+        logger = WandbLogger()
+    assert logger.name == "env_project"
+
+    with mock.patch.dict(os.environ, {"WANDB_PROJECT": "env_project"}):
+        logger = WandbLogger(project="project")
     assert logger.name == "project"