Combine multiple python files into one python file

Thing you want to do

For example, a project where main.py calls another file ...

- main.py
- sub1.py
- folder
    - sub2.py

↓ ↓ ↓ The functions and classes that depend on it ...

onefile.py

Gyu! !! !! I'm surprised.

(The motivation is that I wondered if I could easily execute the python file managed by the project with google colab.)

Solution

Here, you can use a python package called stickytape. https://pypi.org/project/stickytape/

stickytape = Adhesive tape

Experiment

All the experimented code is published on Github, so for reference.

install Now you can use the stickytape command. (No command name ...)

$ pip install stickytape

Prepare the file

The configuration is as follows.

- main.py
- sub1.py
- folder
    - sub2.py

folder/sub2.py Make an Apple class as you like and give it a value property as you like.

class Apple:

    def __init__(self, value):
        self.value = value

sub1.py I will make an average function as appropriate.

sub1.py


def mean(a, b):
    return (a+b)/2

main.py Import it, calculate it appropriately, and display it appropriately.

from sub1 import mean
from folder.sub2 import Apple

apple1 = Apple(value=100)
apple2 = Apple(value=200)

result = mean(apple1.value, apple2.value)
print(result)

Now! In one file!

Execute the following command. (Of course, anything in onefile.py is OK)

$ stickytape main.py > onefile.py

result

The following ʻonefile.py` will be generated.

#!/usr/bin/env python


import contextlib as __stickytape_contextlib

@__stickytape_contextlib.contextmanager
def __stickytape_temporary_dir():
    import tempfile
    import shutil
    dir_path = tempfile.mkdtemp()
    try:
        yield dir_path
    finally:
        shutil.rmtree(dir_path)

with __stickytape_temporary_dir() as __stickytape_working_dir:
    def __stickytape_write_module(path, contents):
        import os, os.path

        def make_package(path):
            parts = path.split("/")
            partial_path = __stickytape_working_dir
            for part in parts:
                partial_path = os.path.join(partial_path, part)
                if not os.path.exists(partial_path):
                    os.mkdir(partial_path)
                    open(os.path.join(partial_path, "__init__.py"), "w").write("\n")

        make_package(os.path.dirname(path))

        full_path = os.path.join(__stickytape_working_dir, path)
        with open(full_path, "w") as module_file:
            module_file.write(contents)

    import sys as __stickytape_sys
    __stickytape_sys.path.insert(0, __stickytape_working_dir)

    __stickytape_write_module('sub1.py', 'def mean(a, b):\n    return (a+b)/2')
    __stickytape_write_module('folder/sub2.py', 'class Apple:\n\n    def __init__(self, value):\n        self.value = value')
    from sub1 import mean
    from folder.sub2 import Apple
    
    apple1 = Apple(value=100)
    apple2 = Apple(value=200)
    
    result = mean(apple1.value, apple2.value)
    print(result)

For a moment, it became "What's wrong !?", but when I executed this ...

150.0

The correct calculation result is displayed safely.

Experiment with Google Colab

I copied the above code to Google Colab and ran it.

As shown below, 150.0 was displayed safely.

(Don't worry about the rainbow-colored cat walking.)

スクリーンショット 2020-06-25 12.38.44.png

Scripting

It's a digression from here.

The command stickytape is long, and I'm lazy to specify the directory of the generated file one by one, so I think it's a good idea to script it as follows.

- main.py
- sub1.py
- folder
    - sub2.py
- scripts
    - tape.sh
- build
    - onefile.py

tape.sh


#initial value
entry="main.py"
output="onefile.py"

#option
while getopts e:o: OPT
do
	case $OPT in 
		"e" ) entry=${OPTARG};;
		"o" ) output=${OPTARG};;
	esac
done

#Run
stickytape ${entry} > "build/${output}"

The following command will run main.py and generate onefile.py in the build directory.

$ sh scripts/tape.sh

I also prepared options.

Option name Description
-e Entry point filename
-o File name to output
$ sh scripts/tape.sh -e <file name> -o <file name>

The generated directory is fixed with build, so if you don't like it, change it.

Self-introduction

If you write it at the beginning, it will get in the way, so let me introduce yourself quietly at the end.

name Aki Wataoka
school Kobe University Graduate School
Undergraduate research Machine learning,Speech processing
Graduate study Machine learning,fairness,Generative model, etc
Twitter @Wataoka_Koki

Follow us on Twitter!

Recommended Posts

Combine multiple python files into one python file
[Python & Unix] Combine multiple PDF files into one.
[Python] Combine multiple Excel sheets into one
Combine multiple csv files into one csv file with python (assuming only one line of header)
Combine multiple Excel files loaded using pandas into one
Combine python function arguments into one variable
Convert multiple jpg files to one PDF file
Decompress multiple compressed files (Python)
[Python] Send gmail with python: Send one by one with multiple image files attached
[Blender] Split Blender script into multiple files
Handle multiple python versions in one jupyter
Bulk read multiple files & combine vertically (DataFrame)
[Python] Combine multiple Excel sheets into one
Combine python function arguments into one variable
Combine multiple python files into one python file
[Python & Unix] Combine multiple PDF files into one.
Combine multiple Excel files loaded using pandas into one
Script python file
Combine multiple CSVs and data frames into one data frame at high speed [60x speed]
[Automation] Convert Python code into an exe file
Python file processing
Python code that removes contiguous spaces into one
Combine multiple Excel files loaded using pandas into one
[Python] Combine multiple Excel sheets into one
Combine multiple python files into one python file
Connect multiple videos in one shot using OpenCV!
[Python & Unix] Combine multiple PDF files into one.
Multiple graphs are displayed in one window (python)
Convert multiple proto files at once with python
Sample to put Python Kivy in one file
A script that combines multiple pages of a PDF file into one page without margins
Python text reading for multiple lines and one line
Convert multiple Jupyter notebooks together into an html file
Remove headings from multiple format CSV files with python
File operations in Python
[Python] File / directory operations
One liner in Python
[Note] File reading ~ Python ~
File processing in Python
python Zundko One Liner
[Python] Create multiple directories
Generate multiple HTML files at once by pouring JSON data into an HTML template with Python
Automatically execute python file
Reading pyc file (Python 3.5.2)
File operations in Python
Python --Tagging MP3 files
Read Python csv file
python external file reading
[Python] Reading CSV files
[R] [Python] Memo to read multiple csv files in multiple zip files
I tried to divide the file into folders with Python