Include a custom python module in multiple modules separately

805 Views Asked by At

I have made a custom python module (say awesome-lib.py), which is to be imported and used by the multiple other python modules(module1.py, module2.py etc). The problem is that all the modules need to be in separate folders and each of them should have a copy of awesome-lib.py for them to import it. I thought of two options for doing this:

  1. Each module folder will have a copy of awesome-lib.py in it. That way I can import awesome-lib and used it in each module. But issue is when I have to make any changes in awesome-lib.py. I would have to copy the file in each module folder separately, so this might not be a good approach.
  2. I can package the awesome-lib.py using distutils. Whenever I make the change in the module, I will update awesome-lib.py in each module using some script. But still I want the awesome-lib distribution package to be individually included in each module folder.

Can anyone please tell me an efficient way to achieve this? So that I can easily change one file and the changes are reflected in all the modules separately.

P.S: I want the awesome-lib.py in each module folder separately because I need to zip the contents of it and upload each module on AWS Lambda as a Lambda zip package.

1

There are 1 best solutions below

3
On

let only one copy of awesome-lib.py placed where it is placed and append it's path in other modules. let sample path is "/home/user/awesome-lib.py"

Add following code in every other module you want to import awesome-lib.py

import sys sys.path.append('home/user/awesome-lib') import awesome-lib

Note: path of awesome-lib may differ on your choice