How to add properties file inside a pex package?

143 Views Asked by At

I have this project:

project_folder/
    main.py
    setup.py
    requirements.text
    properties.ini

I build my pex file with this command: pex . -r requirements.txt -c main.py -o mypex.pex

Q: How do I add properties.ini into my final pex package? My python script cannot work without the properties.

I have tried this solution, by adding the properties file with the zip command:

pex . -r requirements.txt -c main.py -o mypex.pex  # build the package
zip mypex.pex properties.ini  # add properties file, like suggested in the answer linked above

When I run with python3 mypex.pex, the line pkg_resources.resource_filename gives me an actual path:

/home/ubuntu/.pex/unzipped_pexes/884d0e35e7ba50e4dd0867ffd91d3d4fd273263c/properties.ini

but if I open(path, 'r') it, it gives me a FileNotFoundError. I can confirm the file does not exist because I tried to cat it.

That's what the beginning of main.py looks like:

properties_path = pkg_resources.resource_filename(__name__, "properties.ini")
print(f'Properties path: {properties_path}') # prints an actual path (see above)

with open(properties_path, 'r') as f:
    print(f.read()) # throws an FileNotFoundError

My setup.py looks like this.

from distutils.core import setup

setup(
    name='my-script',
    version='1.0',
    scripts=['main.py']
)

This method looks more like a hack than an official solution; what is the proper way of adding properties files (or any configuration file) to a pex package? Perhaps it has something to do with setup.py?

0

There are 0 best solutions below