5

I am trying to create a package in Python that has a number of sub-packages (I'm not sure if that's the right term for them) which need to interoperate.

I have a (simplified) structure like this:

/package
    |-script1.py
    |-script2.py
    |-subpackage1
    |   |-__init__.py
    |   |-src
    |   |   |-__init__.py
    |   |   |-my_program.py
    |   |   |-functions.py
    |   |   |-...
    |
    |-tests
    |    |-a_tests.py
    |-subpackage2
    |    |-web-server.py
    |    |-API
    |    |    |-__init__.py
    |    |    |-REST.py
    |    |    |-...
  • package/subpackage2 needs to be able to call package/subpackage1/src/functions.py
  • package/tests calls both subpackages (using pytests).
  • package/subpackage1/src/functions.py needs to be able to call other modules within subpackage1

I've seen this answer: https://stackoverflow.com/a/33195094 - which explains what I need to do (create a package), but it doesn't explain how to do it.

I can readily get the two scripts to call their component sub-packages using:

import subpackage1.src.my_program.py

(i.e. similar to the suggestions here) but then my_program.py fails with an ImportError: No module named 'functions'

So, what glue do I need to set this structure up?

Community
  • 1
  • 1
GIS-Jonathan
  • 4,347
  • 11
  • 31
  • 45
  • 1
    the `subpackage1` has to have a `__init__.py` be to detected as module. you added `__init__.py` in `src` but didnt in the `subpackage1`. – GIRISH RAMNANI Mar 28 '16 at 08:20
  • @GIRISHRAMNANI - Thanks. I've intermittently had one in there but wasn't sure if it was necessary. Having one in there doesn't seem to change anything; so some other stuff is needed to... – GIS-Jonathan Mar 28 '16 at 08:23

3 Answers3

4

If you want to import something from functions.py into my_program.py then in my_program.py you have to specify the absolute import path.

Let's say that functions.py contains following function:

def function1():
  print('foo bar')

Then, to import function1 from functions.py into my_program.py its contents should look like:

from subpackage1.src.functions import function1

function1()
  • But can I tried `import subpackage1.src.functions` and do `functions.function1()`? It doesn't work. Why? – theX Aug 10 '20 at 02:28
3

So to solve this issue i Created a similar folder structure

/package
    |-script1.py
    |-subpackage1
    |   |-__init__.py
    |   |-src
    |   |   |-__init__.py
    |   |   |-functions.py

my script1.py file has

import subpackage1
import subpackage1.src
import subpackage1.src.functions as f


print(f.hello())

my functions.py file has

def hello():
    return "from the functions"

now from the package folder

i did

$ python script1.py

the script ran and output

from the functions

showed.

I am using python3

So am i missing something because its working on my system.

Note: i added three different imports to check for import errors there.

GIRISH RAMNANI
  • 614
  • 6
  • 18
1

Add import subpackage1.src.functions as f in your my_program.py

When you run the module, stand in package folder and run as below:

python -m subpackage1.src.my_program

bluemoon
  • 353
  • 3
  • 7