dPol
dPol

Reputation: 475

Python subpackage in arbitrary folder. How to write the __init__.py?

If I have this hierarchy

A
|- __init__.py
|-B
  |- __init__.py
  |- funcitons.py 
     (which contains def my_function(): pass)

and I have package A installed, I can do the following

from A.B import functions
functions.my_function()

or

from A.B.functions import my_function
my_function()

How to accomplish the same result if B is encapsulated in several subfolders (which are not packages)?

Upvotes: 0

Views: 143

Answers (1)

dPol
dPol

Reputation: 475

As suggested by Antti Haapala, in Python 2 this is what __path__ is for. The following A/__init__.py did the job.

import os
__path__.append( os.path.join(os.path.dirname(__file__), 'res/path'))

see also What is __path__ useful for?

Upvotes: 1

Related Questions