Python Executable with Dunder Main

With Python you can run a directory as an executable. The __main__.py file in myprojectfolder serves as the entry point for your program.

myprojectfolder/
    |_ __main__.py
    |_ __init__.py

With __main__.py being:

print("Hello")

So you can call the directory like this:

python myprojectfolder
Hello

This functionality works not only at the top-level directory but also in submodules. If your project has a subfolder with its own __main__.py, you can access it by running python myprojectfolder.subfolder, and Python will execute the code inside that file as the entry point,

Compress the Artifact

Instead of calling a directory, you can zip up the whole porject and make the zip file a runnable program.

python -m zipfile -c myproject.zip myprojectfolder

Which will make it possible to run the myproject as one.

python myproject.zip

Create a Binary

You can also easily obfuscate and turn the project into a binary-like program by adding a shebang and combining the contents of the zip file.

Assuming you are working on a linux machine with Python installed along with all required packages:

echo '#!/usr/bin/env python' >> myprogram
cat myproject.zip >> myprogram
chmod +x myprogram

Now you can run the program like this:

./myprogram

Every program that can call a subprocess can now interact with the packaged contents.

Alternatively, you can place it in a directory within your PATH and have a command-line tool at your fingertips.