GIS-Jonathan
GIS-Jonathan

Reputation: 4647

Creating a package with several sub-packages in Python

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
    |    |    |-...

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?

Upvotes: 5

Views: 12292

Answers (3)

bluemoon
bluemoon

Reputation: 363

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

Upvotes: 1

Łukasz Lalik
Łukasz Lalik

Reputation: 61

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()

Upvotes: 4

GIRISH RAMNANI
GIRISH RAMNANI

Reputation: 624

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.

Upvotes: 3

Related Questions