0

I'm working with some code that's been handed off to me by someone else. I need to reuse some parts of the project, but not all. I'm basically adding a Flask server that hosts a single function from all the project.

I'm packaging it into a docker image, and I want to keep it as lightweight as possible. For this, I'm planning to only include the files that are actually use/referenced from my entry point of a Flask server written in the "server.py" file.

Is there a way to get a list of files that are referenced/imported via my entry point into the code? I'm thinking something along the lines of pipreqs, except I'm not looking for pip modules, but rather files in my folder.

For example, my "server.py" imports a local module called "feature_extraction" that then imports another local module called "preprocessing" and so on. BUT starting from my server, the module "train_model" is never imported. I'd like to keep only the files that are necessary (~"feature_extraction" and "preprocessing"), and not use any of the other files in the project (~"train_model").

Is there a module that supports this? If not, how would I go about it programmatically? Doing this by hand would take a long time.

lte__
  • 7,175
  • 25
  • 74
  • 131
  • Check out [`modulefinder`](https://docs.python.org/3/library/modulefinder.html). – Selcuk Jan 11 '23 at 14:10
  • This might be helpful to check out: https://stackoverflow.com/questions/2540202/how-can-i-check-for-unused-import-in-many-python-files – JonSG Jan 11 '23 at 14:22

0 Answers0