I'm writing a library in python 3.7. I'd like it to have as few dependencies as possible. For example, tqdm is nice to have but ideally I'd like my library to work without it if it's not there. Therefore, wherever a script in my library has, for example:
from tqdm import tqdmdef a_function():for x in tqdm([1, 2, 3]):...
I would want it to use the real tqdm if it's installed but otherwise just act as if it wasn't there. So I created a script inside the library called tqdm.py, containing a pass-through function, which defaults to being a dummy function if it fails to import tqdm:
try:from tqdm import tqdm as tqdmExtdef tqdm(function, **kwargs):return tqdmExt(function, **kwargs)except ModuleNotFoundError:def tqdm(function, **kwargs):return function(**kwargs)
So then, my first step when using the library is to add the path to the library itself at the front of the system path, so:
import sys
sys.path.insert(0, 'path_to_library')
Then I hoped to import the library function, which would then pick up the local tqdm function. Unfortunately this doesn't work, because now the script in my library can't be imported because it, too, sees its own library at the front of the path, and fails when trying to import the local tqdm (it would really need to pick up the installed tqdm):
from tqdm import tqdm as tqdmExtImportError: cannot import name 'tqdm' from 'tqdm'
Any ideas how to get this working, or another way to achieve this optional dependency?