1

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?

jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
Drachenfels
  • 2,629
  • 2
  • 27
  • 38
  • 1
    I think you shouldn't bother at all. – vaultah Feb 19 '15 at 14:57
  • 2
    You can use `__init__.py` files and the `__all__` attribute to define what a package exposes externally; see e.g. http://stackoverflow.com/q/44834/3001761, http://stackoverflow.com/q/448271/3001761 – jonrsharpe Feb 19 '15 at 15:07

1 Answers1

4

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')
Vasily Ryabov
  • 8,502
  • 6
  • 23
  • 67