Unexpected import behavior: non-existing module imported
I have a torch extension (but I think the error I get is independent of torch), let's call it foo, that is built with setuptools. However, it depends on whether there is a GPU available in the system, in which case some CUDA code is compiled:

from torch.utils.cpp_extension import CppExtension, BuildExtension, CUDAExtension

if cuda_available:
    ext = CUDAExtension(
            'foo_gpu',
            prefixed(prefix, ['foo.cpp', 'foo_kernel.cu']),
            define_macros=[('COMPILE_CUDA', '1')])
else:
    ext = CppExtension(
            'foo_cpu',
            prefixed(prefix, ['foo.cpp']))

setup(name=ext.name,
      version='1.0.0',
      ext_modules=[ext_module],
      extra_compile_args=['-mmacosx-version-min=10.9'],
      cmdclass={'build_ext': BuildExtension})


Notice how the module is compiled to be either foo_gpu or foo_cpu.

Later, I import it as follows:

try:
    import foo_gpu as foo_backend
    CUDA_SUPPORTED = True
except ImportError:
    CUDA_SUPPORTED = False
    # Try importing the cpu version
    try:
        import foo_cpu as foo_backend
    except ImportError:
        ...


Let's say I compile it with CUDA support, so foo_gpu goes into PIP (actually goes into PIP as foo-gpu, not sure why?)

Now, I uninstalled it, pip uninstall foo-gpu, and compile it with CPU support only, and pip shows foo-cpu 1.0.0.

BUT, now, when I run the import code above, it still finds foo-gpu, i.e., the first import statement succeeds! Even though it does not show up in pip.



EDIT. I checked sys.path and found there is one folder that contains something with gpu in my conda env:

$ ls ~/miniconda3/envs/cpu_env/lib/python3.7/site-packages/foo_cpu-1.0.0-py3.7-linux-x86_64.egg/ 
 EGG-INFO
 __pycache__
 foo_cpu.cpython-37m-x86_64-linux-gnu.so
 foo_cpu.py
 foo_gpu.cpython-37m-x86_64-linux-gnu.so


But how should it get there? In this env (cpu_env), I never compiled with GPU support. Are there some caches that get invoked?

Turns out the GPU build in was cached by setuptools (build/, dist/, etc.) and also installed!
