0

I have created a package in python that I want to use in many other modules. The package contains a lot of classes; some large and some small. I have decided to keep each class in its own module as this makes sense in the context of the package and is, I think, what users would want.

I would like to know the most pythonic way to organise the package.

At present at present it is structured as shown here, in a top level directory called 'org':

enter image description here

(remember I have many more modules than the three show here and the list of modules is very long).

I can import any of the classes using into different packages using:

import sys
sys.path.append('../org')
from org.a import A
A()

I would like to organise it like this and still use the same import statements (if possible):

enter image description here

unfortunately, if I do this, I cannot import any of the classes using the code shown above.

Can someone please show me how they would do it?

Psionman
  • 3,084
  • 1
  • 32
  • 65
  • I think you should do `from org.a import A` from the same folder where `org` folder is located –  Jul 04 '19 at 10:32
  • Where are you trying to do this import? – absolutelydevastated Jul 04 '19 at 10:34
  • 1
    i suppose in org if apart from source there are otheer package include which have different functionality then the a,b,c then it is good way, all you need to defined the __init__.py to expose packages and also , if you want more reference then just check the github of any big project let say flask and see how they organise there package – sahasrara62 Jul 04 '19 at 10:34
  • @reportgunner I'm not sure what you mean. I'm trying to import classes from this package into modules in other packages – Psionman Jul 04 '19 at 10:35
  • do what prashant said, it helped me the most. Check a package you like (or more than one) and get inspired on how they did it –  Jul 04 '19 at 10:36
  • @prashantrana you need to defined the init.py to expose packages - how? – Psionman Jul 04 '19 at 10:37
  • 1
    @Psionman read about `__all__` – sahasrara62 Jul 04 '19 at 10:42
  • Thanks __all__. This question covers it [link](https://stackoverflow.com/questions/44834/can-someone-explain-all-in-python). See the answer by **Aaron Hall** – Psionman Jul 04 '19 at 11:21

0 Answers0