JSON is one of the ways to store this data in an organized and easy-to-handle manner. This data is provided to the program while running or built into the program since the beginning. Now that you aren’t spending all of your time trying to write customer pyinstaller scripts for your project, then you can celebrate by having a drink.Most of the programs need data to work. Simply pyinstaller my_package/package_file_0.py (assuming that is where your entry point is), and you are ready to rock! Drink Now that your files are simply bundled into your application, there should be nothing special about your package that requires MANIFEST.in or interferes with pyinstaller. Simply include stringify_py() in your setup.py script for my_package just before the call to setup().: from setuptools import setupĪs shown, you may also wish to add stringify to your setup_requires, but you will get an error on import if stringify isn’t installed, so it won’t mess up your packaging to skip this line. To reduce the chances of messing this up later, you may wish to add this funcitonality to your workflow so that it is automatically completed as you are developing and distributing your package. Tkinter.PhotoImage(data=img1) # with this oneĭepending on your use case, you may have to experiment with method of unpacking the data for use. Note that some functions, such as tkinter.PhotoImage() can take image data directly in this format.: # tkinter.PhotoImage(file='./images/img1.png') # replace this call. Replace any absolute function calls to those images with the variable. Within package_file_0.py, import the new image strings: from images import img1, img2 Recall that it was package_file_0.py that utilized the image data. Each of these has a long byte string assigned to it that can be easily referenced and mined for the data. You should see two variables located within called img1 and img2. The source tree as it existed earlier remains intact, but we have an extra file within my_package called images.py: setup.pyįeel free to have a look at images.py, you can’t hurt it and you can always regenerate it. For instance, green-dot one.png will become the variable green_dot_one. Note that dashes will be replaced with underscores and spaces will be replaced with underscores and that the extensions will be removed before the name of the python variable is created. Stringify_py(source_path='images', destination_file='my_package/images.py') Our files are now located within images and our desired location will be images.py within the package.: from stringify import stringify_py Package_file_0.py Run stringify_py() Utility We will put something in there to take their place, but dont worry about that for the moment. Move your binary files outside of the package that you wish to include them within. Note that the image paths are explicitlyĬalled out as part of my_package/package_file_0.py.: setup.py We will assume a project structure that starts begins as outlined below. Larger file packaging using this method has not been tested. This library allows you to read those binary files into python code and include that data as python variables in your code base.Ī quick note, this library has only been tested on relatively modestly-sized files - such as images. This doesn’t work so well when you try to bundle your package using pyinstaller, which doesn’t natively recognize your non-code dependencies. This works great when using python directly - such as when you install the package via pip and call it from the command-line. You probably have a directory called images or bin that the python code references when building a GUI or executing a program… whatever. Somewhere - probably within your module - you have a file that has static, non-code data. You could also download this repository and python setup.py install, but that isn’t as fun, is it? Use Case Simply pip install stringify should do the trick. Suddenly, you get to spend lots of time thinking about how to best write your spec files so that they capture the current files and any future changes as well. This may not be useful for your little tkinter project until you try to bundle the project as an executable using pyinstaller. The stringify module allows you to easily include your project’s binary files as python base64 strings.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |