Skip to content

Import torchvision from root project folder fails #2239

@fmassa

Description

@fmassa

The problem

This issue concerns users who installed torchvision from source via python setup.py install.

After installation, the location of the .so are moved to the site-package folder of the python distribution.
This means that importing torchvision from the root of the project folder (cd vision) fails with an obscure error message

RuntimeError: object has no attribute nms

This error usually appears when the library was either not compiled properly, or there was a version mismatch between PyTorch and torchvision, which makes the library loading not work. But in this case, compilation went well, so what could be the reason?

The reason

This error happens because of python's import rules. It first picks the torchvision from the vision/torchvision folder first, and not the installed package in site-packages, and as such will be missing the compiled .so file, leading to the error above.

This error has been aggravated with the addition of @torch.jit.script annotations to nms, which means that now the missing .so library raises a runtime error on torchvision, which wasn't the case before (we used to guard the library import on a try-except block).

A few solutions

There are some possible solutions to this issue:

  • ask the users to install via python setup.py build develop (and thus setuptools don't copy the .so to site-packages). This doesn't fit all use-cases and is suboptimal
  • remove the @torch.jit.script annotations from the codebase, and replace with @torch.jit._script_if_tracing. This will fix the import error, but when the user try to use the C++ ops they will still face errors, so it's not a real solution
  • follow an approach similar to numpy and detect if we are importing the library from the root folder, raising a nice error message. This I believe would be the preferred solution.

Related to #2228

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions