'How to compile a library for a multi-layered Python class structure

The Challenge

I am working on a Python project that will act as a translation layer for the SCPI command line interface on scientific instruments.

It is similar to the concept described in Multi layer package in python however my situation is slightly more complex and I can't seem to figure out how to make it work.

The following is a representation of my project structure (I am happy to change it if it is required):

Project Structure Representation

Some things to keep in mind

  • The names of the files in translator_lib.instruments.supplierX.moduleX are only class1.py, class2.py and class3.py, the rest of the filename is for reference to describe where they are derived from.
  • Many of the modules and classes inside of supplier1 and supplier2 have the same names (as in the example).
  • Every directory contains a __init__.py file

init.py files in each directory (not all expanded)

The __init__.py files (based on the example layout) look as follows (Note I only have one module for testing)

translator_lib\__init__.py

from .instruments import supplier1

__all__ = ['supplier1']

translator_lib\instruments\__init__.py

from .supplier1 import module1

__all__ = ['module1']

What I'm trying to do

Compile three libraries called my_translator_lib, supplier1_translator_lib and supplier2_translator_lib.

Reason

The development team would import my_translator_lib to do what they need to, but if we want to send sample code to supplier1, we want to send them the supplier1_translator_lib and they should only be able to import supplier1_translator_lib

Example 1 : Developer

from translator_lib.instruments import supplier1
from translator_lib.instruments import supplier2


class DoStuff:

  __init__(self):
    self.sup1_class1 = supplier1.module1.class1.class1()
    self.sup2_class1 = supplier2.module1.class1.class1()

Example 2 : Supplier 1

from supplier1_translator_lib import module1
from supplier1_translator_lib import module2


class DoStuff:

  __init__(self):
    self.class1 = module1.class1.class1()
    self.class2 = module1.class2.class2()

I've tried multiple combinations and sections from How to create a Python library and Deep dive: Create and publish your first Python library. I manage to create a library and install it, but ultimately I can only import my_translator_lib and nothing else is visible or available.

Any help in this regard would be truly appreciated.



Solution 1:[1]

have you created the __init__.py files? you need to have then in every subfolder to your module.

Files named __init__.py are used to mark directories on disk as Python package directories. Try to follow this:

mydir/spam/__init__.py
mydir/spam/module.py

If this not solve your problem, as a last resource you can try to sys.path.append('path_to_other_modules')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Guinther Kovalski