Multiple Related Projects With Same Package Structure In Python

I started Python programming 4-5 years ago. However, I usually still use Java to write more complicated projects such as data processing platforms for many reasons. One reason is the that writing complicated projects where you have to separate one project into many related and dependent projects much easier.

Lately, I started writing a RPC platform using JSON-RPC that would enable me and possibly my team to write Machine Learning code in various languages and execute these functions from using RPC from other languages. There are many reasons why you would need such a system including efficiency, flexibility and etc.

However, as more experienced Java developer some perks that are really obvious and easy to use in java are not as clear and obvious in Python. A particular case that I experienced in my project was importing packages and modules from different Python projects with same/similar package structure. This is a common practice for example you want to separate the common libraries, classes and codes for a project into a minimal project that will be imported by the other parts. In my case where I’m writing a RPC platform and Name Server I have the following structure in Java and Python:

However,  when I’m trying to import module1 in module3.py file it fails run by throwing the following error:

The problem here is that the overlap of packages prevents the interpreter to find the right package name and module. In order to solve the problem we need to allow Python’s __path__ variable to be extended or overwritten. This can be done by adding the following code to the __init__.py of packages that may cause a conflict (This will include pack1, pack2 in my example above).

Try running your code again. Your imports should work now.

 

Leave a reply:

Your email address will not be published.

Site Footer