6

I've got a simple Python zipapp built with the following command:

python -m pkg/ -c -o test -p '/usr/bin/python3' -m 'test:main' zipapp

I would like to access the binary file from the script

$ cat pkg/test.py 
def main():
    with open('test.bin', 'rb') as f:
        print(f.name)

Directory structure

$ tree pkg/
pkg/
├── test.bin
└── test.py

0 directories, 2 files

But it looks like the script is referring to a file from the current directory:

$ ./test 
Traceback (most recent call last):
  File "/usr/lib64/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib64/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "./test/__main__.py", line 3, in <module>
  File "./test/test.py", line 2, in main
FileNotFoundError: [Errno 2] No such file or directory: 'test.bin'

It's a quite large binary file so I would like to avoid creating a variable. Is there a way to access this file from the script itself?

HTF
  • 6,632
  • 6
  • 30
  • 49

2 Answers2

4

While the accepted answer works it has some flaws. Mainly that the app can now only run as a zipapp. Instead one can use the importlib.resources module to support both modes simultaneously:

import importlib.resources

def main():
    # if you use multiple packages you can replace '__package__'
    # with an explicit module specifier like 'foobar.templates'
    print(importlib.resources.read_text(__package__, "data.txt"), end="")

if __name__ == "__main__":
    # this is used to support calling with 'python3 -m ...'
    main()
$ tree
.
└── myapp
    └── mypackage
        ├── cli.py
        ├── data.txt (contains "Hello World")
        └── __init__.py

2 directories, 3 files
$ (cd myapp && python3 -m mypackage.cli) # still callable in unzipped mode
Hello World
$ python3 -m zipapp --python "/usr/bin/env python3" --main "mypackage.cli:main" myapp      
$ ./myapp.pyz
Hello World

You only have to watch out to put the data inside of a proper package. That is you need an __init__.py file inside you package directory and the package itself has to be a subdirectoy of the directory passed to python3 -m zipapp. Otherwise importlib.resources will not recognize the package as such and fail to import any data.

$ unzip -l myapp.pyz
Archive:  myapp.pyz
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2021-05-11 21:26   mypackage/
      282  2021-05-11 21:25   mypackage/cli.py
        0  2021-05-11 19:39   mypackage/__init__.py
        0  2021-05-11 21:26   mypackage/__pycache__/
       12  2021-05-11 19:39   mypackage/data.txt
      427  2021-05-11 21:26   mypackage/__pycache__/cli.cpython-38.pyc
      168  2021-05-11 21:26   mypackage/__pycache__/__init__.cpython-38.pyc
       66  2021-05-11 21:27   __main__.py
---------                     -------
      955                     8 files
Septatrix
  • 195
  • 1
  • 10
3

OK, it looks like I can open the zip file within the script itself:

import zipfile

def main():
    with zipfile.ZipFile(os.path.dirname(__file__)) as z:
        print(z.namelist())
        with z.open('test.bin') as f:
            print(f.name)
HTF
  • 6,632
  • 6
  • 30
  • 49