python: module not found (despite module included in sys.path)

200 Views Asked by At

I have a python script that is calling a module from another python project, whose path is correctly included in sys.path, however the script returns error "module not found".

More in detail, this is the following structure:

/home/lbri/sdfstudio/script.py --> the main script that I am running

/home/lbri/omnidata/omnidata_tools/torch/data/transforms.py --> the module that I am trying to call from script.py

These are the relevant lines inside script.py:

sys.path.append(args.omnidata_path)
print(sys.path)
from data.transforms import get_transform

which is run with

python script.py --omnidata-path /home/lbri/omnidata/omnidata_tools/torch/

This is the error that I get:

['/home/lbri/sdfstudio/scripts/datasets', '/home/lbri/miniconda3/envs/sdfstudio/lib/python38.zip', '/home/lbri/miniconda3/envs/sdfstudio/lib/python3.8', '/home/lbri/miniconda3/envs/sdfstudio/lib/python3.8/lib-dynload', '/home/lbri/miniconda3/envs/sdfstudio/lib/python3.8/site-packages', '__editable__.nerfstudio-0.1.12.finder.__path_hook__', '/home/lbri/omnidata/omnidata_tools/torch/']
Traceback (most recent call last):
  File "script.py", line 41, in <module>
    from data.transforms import get_transform
ModuleNotFoundError: No module named 'data.transforms'

I don't understand: the path of the module ('/home/lbri/omnidata/omnidata_tools/torch/') is included correctly in sys.path, yet the script does not see the module. Am I missing something obvious?

I also tried to put some init.py files inside the folders containing the module, but nothing changes.

Thank you!

EDIT: I also tried to include /home/lbri/omnidata/omnidata_tools/torch/data/ into the sys.path as well, but I still get the same error.

3

There are 3 best solutions below

0
Damian On BEST ANSWER

ok, posting the answer to my question, with a bit of delay.

Basically the main problem is that 'data' and 'torch' are too generic names - which are already used by the script to indicate different modules.

Therefore, the solution was:

  • add /home/lbri/omnidata/omnidata_tools in the syspath
  • change the include to: from omnidata_tools.torch.data.transforms import get_transform

Then everything works fine.

1
Syed Shafiyullah On

To fix this error, you need to make sure that the module data.transforms is actually in the directory /home/lbri/omnidata/omnidata_tools/torch/ and that it is a Python module. You can check this by opening the directory in a file explorer and looking for the file data/transforms.py. If the file is not there, or if it is not a Python module, then you will need to create it or install the module

1
nisakova On

its trying to import from one folder up

sys.path.append(args.omnidata_path)
print(sys.path)
from transforms import get_transform

python script.py --omnidata-path /home/lbri/omnidata/omnidata_tools/torch/data