"""Imports pynvml with sanity checks and custom patches."""

import textwrap


pynvml = None

try:
    # Check pynvml version: we require 11.450.129 or newer.
    # https://github.com/wookayin/gpustat/pull/107
    import pynvml
    if not (
        # Requires nvidia-ml-py >= 11.460.79
        hasattr(pynvml, 'NVML_BRAND_NVIDIA_RTX') or
        # Requires nvidia-ml-py >= 11.450.129, < 11.510.69
        hasattr(pynvml, 'nvmlDeviceGetComputeRunningProcesses_v2')
    ):
        raise RuntimeError("pynvml library is outdated.")
except (ImportError, SyntaxError, RuntimeError) as e:
    raise ImportError(textwrap.dedent(
        """\
        pynvml is missing or an outdated version is installed.

        We require nvidia-ml-py>=11.450.129; see GH-107 for more details.
        Your pynvml installation: """ + repr(pynvml) +
        """

        -----------------------------------------------------------
        Please reinstall `gpustat`:

        $ pip install --force-reinstall gpustat

        if it still does not fix the problem, manually fix nvidia-ml-py installation:

        $ pip uninstall nvidia-ml-py3
        $ pip install --force-reinstall 'nvidia-ml-py<=11.495.46'
        """)) from e


__all__ = ['pynvml']

@@ -70,16 +70,26 @@
 extwrap%0A
+import os%0A
 %0A%0Apynvml
@@ -97,16 +97,439 @@
 = None%0A%0A
+# If this environment variable is set, we will bypass pynvml version validation%0A# so that legacy pynvml (nvidia-ml-py3) can be used. This would be useful%0A# in a case where there are conflicts on pynvml dependencies.%0A# However, beware that pynvml might produce wrong results (see #107).%0AALLOW_LEGACY_PYNVML = os.getenv(%22ALLOW_LEGACY_PYNVML%22, %22%22)%0AALLOW_LEGACY_PYNVML = ALLOW_LEGACY_PYNVML.lower() not in ('false', '0', '')%0A%0A%0A
 try:%0A   
@@ -895,16 +895,44 @@
 ')%0A    )
+ and not ALLOW_LEGACY_PYNVML
 :%0A      
@@ -1457,17 +1457,17 @@
         
-i
+I
 f it sti
@@ -1494,16 +1494,23 @@
 problem,
+ please
  manuall

Introduce a temporary way to bypass pynvml valdiation