daniilidis-group / neural_renderer

A PyTorch port of the Neural 3D Mesh Renderer
Other
1.12k stars 248 forks source link

The name shadowing issue for the package 'neural_renderer' #17

Closed mengyuest closed 5 years ago

mengyuest commented 5 years ago

Hi, I tried the code and could not execute python3 ./examples/example1.py successfully. It raised the error

Traceback (most recent call last):
  File "examples/example1.py", line 12, in <module>
    import neural_renderer as nr
  File "/foo/bar/neural_renderer/neural_renderer/__init__.py", line 3, in <module>
    from .load_obj import load_obj
  File "/foo/bar/neural_renderer/neural_renderer/load_obj.py", line 8, in <module>
    import neural_renderer.cuda.load_textures as load_textures_cuda
ImportError: No module named 'neural_renderer.cuda.load_textures'

It seems like to be a "name shadowing" problem as the local package name conflicts with the installed packages. After altering the inner (second) "neural_renderer" filename to others, it worked as expected. Could it be better to rename the local dirname or just mention that in the README.md? I am not sure whether this is a common issue. I am using python3. Thanks.

PS: Referenced from The name shadowing trap

A simple case for showing "name shadowing" is (assuming you have numpy)

  1. mkdir numpy
  2. touch numpy/__init__.py
  3. python3 -c "import numpy.random" and you will get "No module named..." error
nkolot commented 5 years ago

Yes I am aware that this can happen if you try to run the examples from the root folder of the repo. The local directory is named neural_renderer because this is the standard practice when distributing python packages (you need to supply a setup.py and place your package named foo under the foo/ directory). One problem here is that neural_renderer.cuda.load_textures is an external module written in C++ and CUDA and not a Python module, and is compiled when you run setup.py So when you try to import anything that starts with neural renderer and you are in the root folder it may shadow a globally installed package.

Check for example this package:

DavisDDD commented 4 years ago

I had the same problem, I'm using pycharm. The problem is beacause the import package can't be in the same path with the demo.py. My solution is copy the /neural_renderer to another path, then add it as an external lib.