简体   繁体   中英

Python best practice for when an module is not always available

I have a Python code that runs on cuda. Now I need to support new deployment devices that cannot run cuda because they don't have Nvidia GPUs. Since I have many cupy imports in the code, I am wondering what is the best practice for this situation.

For instance, I might have to import certain classes based on the availability of cuda. This seems nasty. Is there any good programming pattern I can follow?

For instance, I would end up doing something like this:

from my_custom_autoinit import is_cupy_available
if is_cupy_available:
    import my_module_that_uses_cupy

where my_custom_autoinit.py is:

try:
    import cupy as cp
    is_cupy_available = True
except ModuleNotFoundError:
    is_cupy_available = False

This comes with a nasty drawback: every time I want to use my_module_that_uses_cupy I need to check if cupy is available. I don't personally like this and I guess somebody came up with something better than this. Thank you

You could add a module called cupywrapper to your project, containing your try..except

cupywrapper.py

try:
    import cupy as cp
    is_cupy_available = True
except ModuleNotFoundError:
    import numpy as cp
    is_cupy_available = False

I'm assuming you can substitute cupy with numpy because from the website :

CuPy's interface is highly compatible with NumPy; in most cases it can be used as a drop-in replacement. All you need to do is just replace numpy with cupy in your Python code.

Then, in your code, you'd do:

import cupywrapper
cp = cupywrapper.cp

# Now cp is either cupy or numpy
x = [1, 2, 3]
y = [4, 5, 6]
z = cp.dot(x, y)

print(z)
print("cupy? ", cupywrapper.is_cupy_available)

On my computer, I don't have cupy installed and this falls back to numpy.dot , giving an output of

32
cupy? False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM