Execute a Directory/Zip file in Python

Rachit Tayal
Published in
3 min readApr 13, 2021

--

Python provides us the capability to execute a directory or a zip file directly. Something which is not widely known in the Python community. Therefore, this blog post aims to uncover that. Also, in the process, we’ll acquire few less-known python tricks up our sleeves. So let’s dig in.

It is very straightforward to execute a directory in Python. All we have to do is make sure there is a __main__.py file present in that directory. Let’s understand it by considering an example. Here we have a file __main__.py with the following contents:

# file __main__.pyname = "Sarah"
age = 27
print("Hello {}!".format(name))

Now, let’s assume our directory structure is as this.

test/
__main__.py

Executing the __main__.py file or the test directory will yield us the same result.

roark:~/example/test$ python __main__.py
Hello Sarah!
roark:~/example$ python test # Run package as main
Hello Sarah!

Wow, that was easy. With __main__.py present, a directory can be invoked as a script. Therefore, __main__.py designates the entry point for a package.

It gets even better. Python can even similarly execute a zip file. Before trying that, let’s first create a zip file in a Pythonic way.
We can achieve this with Python from the shell using the zipfile module present in the standard library. The syntax for that is as below:

python -m zipfile -c zipname.zip sourcedir/*.py-m option runs a library as module (As a sidenote it comes very handy while installing packages using pip. We should always prefer
python -m pip install <package_name> instead of
pip install <package_name>
Since the former explictly calls out the Python verison where I want my package to be installed by binding Python with it's corresponding pip module.)
-c is create file option

where zipname.zip is the name of the destination file we want and sourcedir is the path to the source code directory. The -m option runs the library module as a script. Trying the above command in our example:

roark:~/example$ ls
test
roark:~/example$ python -m zipfile -c test.zip test/*.py
roark:~/example$ ls
test test.zip
roark:~/example$ python test.zip
Hello Sarah!

That is cool. Therefore, Python can also execute a zip file as a standalone script.

To go one step further, we can even prepend our zip file with a shebang (#!) to make it an executable and run it directly. The below snippet highlights the same.

roark:~/example$ ls
test test.zip
roark:~/example$ echo '#!/usr/bin/env python3' > test_exec
roark:~/example$ cat test.zip >> test_exec
roark:~/example$ chmod +x test_exec
roark:~/example$ ls
test test.zip test_exec
roark:~/example$ ./test_exec
Hello Sarah!

Et. Voila! We have our executable running and giving the desired output.
NOTE: Make sure to put the right shebang as to where the Python binary is present. If Python is installed directly in the System, then there is a fair chance shebang will be #!/usr/bin/python3

Conclusions:

To summarize, we’ve learned that Python offers not only invoking a directory as a script but even a zip file as well, provided we have __main__.py file present which serves as the entry point for the execution.
We further created an executable out of it which can be invoked directly. We can use this to make submodules executable in our Python app to allow them to run independently or when we want to package our code as binary for distribution.

--

--

Rachit Tayal
Python Features

Sports Enthusiast | Senior Deep Learning Engineer. Python Blogger @ medium. Background in Machine Learning & Python. Linux and Vim Fan