Import Python preprocesses

I manage a fairly large python code base (> 2000 lines) that I want to be accessible as a single executable python script anyway. So I'm looking for a method or tool to merge a development folder, made from different python files, into a single script run.

The thing / method I'm looking for should include splitting the code into different files, possibly with the source file __init___.py , which contains the import and combines it into one big script.

Much like a preprocessor. It’s best if you have a native path, it’s better if I can run dev from the folder anyway.

I already tested pypp and pypreprocessor, but they don't seem to understand.

Something like the weird use of __import__() or maybe a bundle from foo import * is being replaced by preprocessor code? Obviously, I want to combine only my catalog, not shared libraries.

Update. What I want is to accurately support the code as a package, and then be able to "compile" it into a single script, easy to copy-paste, distribute and reuse.

+4
source share
2 answers

It sounds like you are asking how to combine your codebase into a single file with 2000 second source code - are you really sure you want to do this? This will make your code more difficult to maintain. Python files correspond to modules, so if your main script does not execute from modname import * for all its parts, you will lose the structure of the module by translating it into a single file.

I would recommend leaving the structure structured as it is and solving the problem of program distribution:

  • You can use PyInstaller , py2exe or something similar to create one executable file that does not even need to install python. (If you can count on the presence of python, see @Sebastian Comment below.)

  • If you want to distribute your code base for use by other python programs, you should definitely start by structuring it as a package, so you can download it with a single import .

  • To easily distribute a lot of python source files, you can pack everything into a zip archive or an “egg” (which is actually a zip archive with special overhead information). Python can import modules directly from a ZIP archive or an egg.

+5
source

waffles seems to do what you need, although I have not tried it

Perhaps you can do it manually, for example:

 # file1.py from .file2 import func1, func2 def something(): func1() + func2() # file2.py def func1(): pass def func2(): pass # __init__.py from .file1 import something if __name__ == "__main__": something() 

You can then merge all the files together by deleting any line starting with from . , and .. it can work.

However, an executable egg or a regular PyPI distribution would be much simpler and more reliable!

+2
source

Source: https://habr.com/ru/post/1403213/


All Articles