dmlc / dmlc/dgl

Isolate and Provide Help Information for Implicit Requirements of Some Modules

Open
#6,463 0 comments 0 reactions 0 assignees View on GitHub
Work Item
Dominant language
Python
Stars
14.3k
Forks
3.1k
PR merge metrics
No merged PRs in 30d

Description

## 🔨Work Item

In DGL, certain modules or classes may rely on implicit external dependencies that are not included in the standard DGL installation. It's crucial to isolate them to ensure a seamless experience for all users and prevent potential crashes for those who do not utilize these modules. In such cases, we should also offer informative guidance to help users address these requirements if they intend to utilize the associated modules.

Project tracker: https://github.com/orgs/dmlc/projects/2

## Description

### Objective
We take `dgl.data.PeptidesStructuralDataset` as an example to describe the scenario. This class requires *ogb* and *pandas* to process raw data, but these dependencies are not included in the standard DGL installation.

When such dependencies are missing in users' environment:
1. (scenario 1) For users who do not use such dataset, we hope code like
```python
import dgl.data as data
from dgl.data import YelpDataset
```
can work normally.
2. (scenario 2) For users who want to use such dataset but without those dependencies properly installed, we hope the code below
```python
from dgl.data import PeptidesStructuralDataset
dataset = PeptidesStructuralDataset()
```
raises an error including guidance to help users address corresponding requirements.

### Current Solution
Currently, we include the following try-except block in `python/dgl/data/__init__.py`
```python
try:
from .lrgb import PeptidesStructuralDataset
except ImportError:
pass
```
If required dependencies are not installed, this solution ignores the class `PeptidesStructuralDataset` and does not include it under the name space `dgl.data`. It handles scenario 1 perfectly but provides no extra information for users in scenario 2. It just raises an `ImportError` indicating that the class does not exist.

### Proposal
Put the error handling in the specific function body. It will not be executed until the function is called, so will not affect the behavior under scenario 1. When the function is invoked without the necessary dependencies met, this approach will provide guidance on how to install the required packages.

```python
class PeptidesStructuralDataset:
...
def foo(self):
# requires a method in bar to execute
# check the existence and version of bar
# (the block below can be abstracted as a separate utility to reuse)
package_exists = importlib.util.find_spec("bar") is not None
package_version = "N/A"
if package_exists:
try:
package_version = importlib.metadata.version("bar")
package_exists = True
except importlib.metadata.PackageNotFoundError:
package_exists = False
# raise an error including guidance according to the existence
# and potential version information of bar
...
```

@frozenbugs @paoxiaode for awareness

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.