1

I have directory structures like below..

   |---- folder1
        |---- __init__.py
        |---- python_file1.py
   |---- folder2
        |---- __init__.py
        |---- python_file2.py

while i try to import like this "folder2.python_file2 import some_function".

it give an error: ImportError: cannot import name some_function

and also i had look many answer in stack overflow they said to insert file path if the directory without __init__.py file. But even i have __init__.py I cant able to import function from that directory. Let me clear about this error.

Thanks.

Mohammed Yasin
  • 487
  • 7
  • 12

1 Answers1

2

Python uses a period(.) operator to refer to the directories/modules starting from the current folder and moving upwards in the directory tree with each period.
For example, if you wish to import a file from python_file2.py and the file you are importing is contained in folder2 itself. This can be achieved by-
from . import <filename>
If you wish to import a file from python_file2.py and the file rests in folder1, you can add another period(.) to move one directory up and the statement will look like-
from ..folder1 import <filename>
filename is python_file1.py in your case. Hope this helps.

Ritik Saxena
  • 694
  • 1
  • 11
  • 23