Repository: dmlc/dgl

Issue:
Cannot use Tensorflow Backend on MacOS even configuring Environment Variable - No module named 'torch'
## 🐛 Bug

I'm trying to run DGL on Tensorflow 2.6 Backend, having installing DGL 0.8.1 on Python 3.7 on a MacOS Monterey 12.3.1 on an i7 processor using conda 4.11.

I changed the environment variable DGLBACKEND to "tensorflow", as well as changing the configuration JSON file to default to "tensorflow".

On trying to run a simple import dgl, an exception is thrown as it still expects to find torch installed in the environment:
"No module named 'torch'"

Before upgrading to Monterey everything seemed fine, but now I can't seem to get it to work anymore.

## To Reproduce

Steps to reproduce the behavior:

1. Install Miniconda 4.11
2. Create conda environment with the following environment.yml:

```
name: myEnv
channels:
  - conda-forge
  - defaults
  - dglteam
  - lambouj
  - plotly
dependencies:
  - python=3.7
  - occwl
  - dgl
  - matplotlib
  - scikit-learn
  - tqdm
  - trimesh
  - jupyter
  - pythreejs
  - tensorflow=2.6.0
  - tensorflow-estimator==2.6.0
  - networkx
  - pandas
  - pydot
  - graphviz
  - seaborn
  - pip
  - opencv
  - plotly
  - alphashape
  - python-igraph
```
  
3. Open python console and run:
`import os; os.environ["DGLBACKEND"]="tensorflow"; import dgl`

The following error occurs 

> Traceback (most recent call last):
>   File "/opt/miniconda3/envs/myEnv/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3552, in run_code
>     exec(code_obj, self.user_global_ns, self.user_ns)
>   File "<ipython-input-2-3372fc2f5385>", line 1, in <module>
>     import os; os.environ["DGLBACKEND"]="tensorflow"; import dgl
>   File "/Users/logged_user/Library/Application Support/JetBrains/Toolbox/apps/DataSpell/ch-0/213.7172.29/DataSpell.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
>     module = self._system_import(name, *args, **kwargs)
>   File "/opt/miniconda3/envs/myEnv/lib/python3.7/site-packages/dgl/__init__.py", line 21, in <module>
>     from . import distributed
>   File "/Users/logged_user/Library/Application Support/JetBrains/Toolbox/apps/DataSpell/ch-0/213.7172.29/DataSpell.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
>     module = self._system_import(name, *args, **kwargs)
>   File "/opt/miniconda3/envs/myEnv/lib/python3.7/site-packages/dgl/distributed/__init__.py", line 17, in <module>
>     from .dist_graph import DistGraphServer, DistGraph, node_split, edge_split
>   File "/Users/logged_user/Library/Application Support/JetBrains/Toolbox/apps/DataSpell/ch-0/213.7172.29/DataSpell.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
>     module = self._system_import(name, *args, **kwargs)
>   File "/opt/miniconda3/envs/myEnv/lib/python3.7/site-packages/dgl/distributed/dist_graph.py", line 12, in <module>
>     from ..transforms import compact_graphs
>   File "/Users/logged_user/Library/Application Support/JetBrains/Toolbox/apps/DataSpell/ch-0/213.7172.29/DataSpell.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
>     module = self._system_import(name, *args, **kwargs)
>   File "/opt/miniconda3/envs/myEnv/lib/python3.7/site-packages/dgl/transforms/__init__.py", line 2, in <module>
>     from .functional import *
>   File "/Users/logged_user/Library/Application Support/JetBrains/Toolbox/apps/DataSpell/ch-0/213.7172.29/DataSpell.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
>     module = self._system_import(name, *args, **kwargs)
>   File "/opt/miniconda3/envs/myEnv/lib/python3.7/site-packages/dgl/transforms/functional.py", line 23, in <module>
>     import torch as th
>   File "/Users/logged_user/Library/Application Support/JetBrains/Toolbox/apps/DataSpell/ch-0/213.7172.29/DataSpell.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
>     module = self._system_import(name, *args, **kwargs)
> ModuleNotFoundError: No module named 'torch'


## Expected behavior

Import should be successfull with tensorflow as Backend

## Environment

 - DGL Version: 0.8.1
 - Backend Library & Version: Tensorflow 2.8
 - OS (e.g., Linux): MacOS Monterey 12.3.1 on Macbook i7 Processor
 - How you installed DGL: conda 4.11
 - Python version: 3.7
 - CUDA/cuDNN version (if applicable): None
 - GPU models and configuration (e.g. V100): None
 - Any other relevant information: 

## Additional context

On Reinstall, i cannot find ~/.dgl anymore


Thank you.



Discussion:

Resolving patch:
diff --git a/python/dgl/transforms/functional.py b/python/dgl/transforms/functional.py
--- a/python/dgl/transforms/functional.py
+++ b/python/dgl/transforms/functional.py
@@ -21,7 +21,11 @@
 import numpy as np
 import scipy.sparse as sparse
 import scipy.sparse.linalg
-import torch as th
+
+try:
+    import torch as th
+except ImportError:
+    pass
 
 from .._ffi.function import _init_api
 from ..base import dgl_warning, DGLError, NID, EID


Test patch: