Drachenfels
Drachenfels

Reputation: 3286

Python - creating subpackage without exposing internal imports

I have following structure:

/api
    /v0
        api_1.py
        api_2.py
    /v1
        api_1.py
        api_2.py

I would like to use it like that:

import api

api.v0.api_1.notify_user('1337')

However when I do for example dir(api.v0.api_1), I will get not only the API methods but all the imports that are performed inside. I feel other developers should not be bothered of what I use internally.

One of the solutions I am considering is to change all internal imports in api_1 to something like that:

import collections as _collections

to clearly indicate it's not a part of a public API. However, this seems to be very strange.

How should I solve this puzzle? Or maybe I should not bother at all and what I am trying to achieve is an overkill?

Upvotes: 1

Views: 161

Answers (1)

Vasily Ryabov
Vasily Ryabov

Reputation: 9991

Probably you need to import public stuff in __init__.py. File structure should look so:

/api
    /v0
       __init__.py # import public stuff here
       api_1.py
       api_2.py
    /v1
       __init__.py
       api_1.py
       api_2.py

In the code:

api.v0.notify_user('1337')

Upvotes: 4

Related Questions