1

I have a directory structure like this:

dir/
   frontend.py
   dir1/main.py
   dir2/backend.py
  • How do I import backend in main in Python?
  • How do I import frontend in main in Python?

Have tried all the answers on Stackoverflow. Nothing seems to work.

waqasgard
  • 801
  • 7
  • 25
  • 1
    You need to have __init__.py file in dir1 and dir2 https://stackoverflow.com/questions/448271/what-is-init-py-for – Nabin Nov 28 '17 at 11:19
  • I have done that too. Does not help me in importing modules from the parent directory. – waqasgard Nov 28 '17 at 11:33

2 Answers2

1

In any folder from which you want to import source files you need to have existing init.py file.

I would advise structure like:

dir/
   main.py
   dir1/frontend.py
   dir1/__init__.py
   dir2/backend.py
   dir2/__init__.py

Then you import them in following fashion (in main.py):

import dir1.frontend
import dir2.backend
jo9k
  • 690
  • 6
  • 19
  • Thanks. I was aware of such a structure. I really wanted to know how to import modules from the parent directory and in the sub-directories within the parent directory. It's not possible always to structure the directories in the way that you have mentioned. – waqasgard Nov 28 '17 at 11:32
  • Thanks mate! But that did not help either. Tried adding my parent directory to PYTHONPATH also. Did not work for me. I cannot import modules from the parent directory. – waqasgard Nov 28 '17 at 12:16
  • As long as I run main.py from dir/ (like python ./dir1/main.py) I achieve proper results (both modules imported) with: `import frontend`, `import dir2.backend`. If it has to be run from dir1/ (python ./main.py) then I have no idea. – jo9k Nov 28 '17 at 12:45
  • I've seen a "hacky" solution import all needed modules in the initial __init__.py of each subfolder, then the __init__.py of the main module would import each of those. – BoboDarph Nov 28 '17 at 13:19
  • @jo9k I badly want to figure out the imports in Python. The structure you provided works fine always. I was aware of that. There should be a mechanism to import anything from anywhere and run from anywhere, I believe. – waqasgard Nov 29 '17 at 10:07
  • From where do you run your script? If you run it from parent directory, like "python ./dir1/main.py" in console, then `import frontend`, `import dir2.backend` works in main. You can always have your console run in parent directory so it should work for you. – jo9k Nov 29 '17 at 16:41
  • I realise this directory structure. However, as I mentioned earlier that it is not always possible to maintain that structure. Hence, I was looking for a way to import modules from the parent directory. I want to know how that can be done, if it's possible. – waqasgard Dec 05 '17 at 07:32
1

There is only one rule when it comes to importing files in a Python project.

You have to import the package relative to the directory from where the project is run.

For example in the question main.py should have something like this:

from dir.frontend import *
from dir.dir2.backend import *

But then you'll have to have something like main.py under dir/ which imports dir/dir1/main.py and then run python main.py.

So, try to keep the main.py always in the head directory so you don't have to worry about such a situation as above.

ONLY ONE RULE: Everything has to be imported relatively to the directory from where the project is run.

waqasgard
  • 801
  • 7
  • 25