0

My current working files are in the following file structure:

--Trick_Folder (root folder)
   --try.py
  --LTransform (sub-folder)
     --encoder.py
     --decoder.py
     --sampler.py
     --__init__.py

in my try.py I try to write import one of the module from the sub-folder LTransform as follows:

**Python-3.4.3
from LTransform import encoder

(Note: In the encoder.py there is an import sampler line in it)

this is the error I am getting:

import sampler
ImportError: No module named 'sampler'

I tried understanding it but I can't wrap my head around it. The__init__.py is already in the sub-folder and my PYTHONPATH is set on the root-folder Trick_Folder as /home/user/Trick_Folder

Why can Python3 interpreter call the sampler.py when importing the encoder.py from the root folder(Trick_Folder)?

Shan-Desai
  • 3,101
  • 3
  • 46
  • 89

2 Answers2

1

To tell python to import from the current directory, use:

from . import sampler

in your encoder.py

ahmed
  • 5,430
  • 1
  • 20
  • 36
1

Found the correct way for importing the module.

in my encoder.py where the line import sampler was previously before, as suggested by this Query I rewrote the line as suggested by @ahmed but with some changes since

import .sampler

gives an invalid syntax error

Solution

from . import sampler

now from LTransform import encoder works perfectly.

Community
  • 1
  • 1
Shan-Desai
  • 3,101
  • 3
  • 46
  • 89