2

My project space is something like this:

Project
   Functions_Folder/
                   __init__.py
                   Folder1/
                          __init__.py
                          Function1.py
                          Function2.py
                          Function3.py
                   Folder2/
                              __init__.py
                              Function4.py
                              Function5.py
                              Function6.py
   Codes_Folder/
                  script1.py
                  script2.py

I need to import in script1.pysome functions of the Folder1 and Folder2 but that functions also have to import functions on the same directory, i mean, for example Function4.py has to import Function6.py and Function3.py

Please help.

DsWqas
  • 29
  • 2
  • 1
    From the directory containing the `Project` you can run files using `python -m Project.Codes_Folder.script1` and then in files you can just `from Project.Functions_Folder.Folder1 import functions1`. – Imanpal Singh Oct 08 '20 at 03:24
  • Put your scripts on level higher in the folder structure. Usually the folder of the script that is being run becomes the base for imports. – Klaus D. Oct 08 '20 at 03:25
  • Look into relative imports. I think that's the way you want to go here. – CryptoFool Oct 08 '20 at 03:28
  • this solution can help you https://stackoverflow.com/questions/6323860/sibling-package-imports – qaiser Oct 08 '20 at 03:36

2 Answers2

0

You can use relative import

├── test
│   ├── __init__.py
│   ├── a
│   │   ├── one.py
│   │   └── two.py
│   └── b
│       ├── __init__.py
│       └── one.py
└── test.py

in which the one.py file in module a is simply

from .two import a_two

def a_one(i):
    return 10*a_two(i)

the two.py file in module a is

def a_two(i):
    return 5*i

the one.py file in module b is

from ..a.one import a_one

def b_one(i):
    return a_one(i)

The lines from ..a.one import a_one and from .two import a_two use relative import.

The whole module can be tested by running the test.py file, which is

from test.b.one import b_one

print(b_one(1))

As for the problem of importing the module test in a script, in macOS and linux, you need to set PYTHONPATH in .bashrc for the module test with

export PYTHONPATH=the_path_to_module_test:$PYTHONPATH

meTchaikovsky
  • 7,478
  • 2
  • 15
  • 34
  • But this doesn't quite answer his question. What if he's down a level, in a directory that's a peer to test? – CryptoFool Oct 08 '20 at 03:38
  • @Steve I don't understand, you mean another module that is peer to module `test`, and `test` has to import methods from that module? – meTchaikovsky Oct 08 '20 at 03:40
  • I did that but i recieve an error: KeyError: 'test' – DsWqas Oct 08 '20 at 03:43
  • @DsWqas It works fine on my computer, since I didn't set `PYTHONPATH` for the module `test`, the script `test.py` and `test` must be in the same folder, otherwise `test.py` won't find `test`. – meTchaikovsky Oct 08 '20 at 03:45
  • yes but on my real project space test,py is not in the same folder.... `othefolder/test.py` – DsWqas Oct 08 '20 at 03:49
0

So you want to import some functions from Folder1 and Folder2 in script1.py.

First create __init__.py for the Project folder

script1.py

import sys
sys.path.append("..")

from Functions_folder.Folder1 import Function1, Function2
from Functions_folder.Folder2 import Function4, Function5

Function4.py

import sys
sys.path.append("..")

from Folder2 import Function5, Function6
from Folder1 import Function3
Carlos
  • 452
  • 2
  • 18