How to split a very large python file?

I use and maintain a python script that automates the analysis of the compilation, execution, and performance of some specific applications. The script was quite simple when I created it (it provided only a compilation option), but now it is very large (2100 lines, not optimized, I agree), quite complex and providing many different command line options (managing arguments with argparse is a nightmare, and I'm not able to do what I need for sure)

To simplify this, I plan to break it into several scripts:

  • compile.py
  • run.py
  • analyse.py

These three scripts will need to have access to common functions, classes, and constants. Regarding this limitation, my question is, what is the pyhtonic way to handle this?

+4
source share
1 answer

You can put the general code in separate files, and then import file as a module in each script that it needs. To find out how the module system works in Python, see the module documentation for Python 2.7 or the module documentation for Python 3.4 , depending on which version of Python you are writing.

+3
source

All Articles