0

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.

Sibtain
  • 1,436
  • 21
  • 39
  • 1
    If it feels like you're doing it wrong, you probably are doing it wrong. – Jonathon Reinhart Dec 30 '16 at 11:20
  • Yes, so any work around for it? :) – Sibtain Dec 30 '16 at 11:22
  • 2
    I don't know much about AWS specifically but I would question the design of a system that _makes_ you put a copy of your module in every folder individually. That's the thing it seems like you're doing wrong. There should be some way to install `awesome-lib.py` centrally and have each module use it, or to express the dependency of `module1.py` etc. on `awesome-lib.py` and have a dependency resolution system take care of it. – David Z Dec 30 '16 at 11:27
  • In case of any other external library, I install it using `pip install -t /install/to/path` and then package that as a zip and upload zip on AWS Lambda. But now when `awesome-lib.py` is dependent on multiple modules and it is possible that it would change then what to do – Sibtain Dec 30 '16 at 11:42

1 Answers1

0

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

  • This technically works, but it's a bad idea except for single-use scripts because once you move any of the files involved to another computer, or change the directory structure, everything breaks. – David Z Dec 30 '16 at 11:28
  • why to change directory structure?? just let your modules where you want them and then simply append it's path.. example above only shows how to do it. – Muhammad Anns Dec 30 '16 at 11:34
  • The issue is when the module is packaged as zip, it needs to have an individual library in the zip package. If it doesn't, and I upload it on AWS Lambda, then that path won't be recognized on AWS Lambda – Sibtain Dec 30 '16 at 11:48