create_product
src
common
util.py
config
config.properities
templates
template1.txt
create_product.py
Python36
I'm using the above dir structure. While use the below command:
pyinstaller create_product.py
It creates the exe file inside the dist/create_product folder. But exe file requires for config folder and templates folder which contains the .properties and .txt file. By default these folders are not copied into the dist/create_product. If manually copied it works fine.
How to copy the two folders by pyinstaller command itself?
Related
I have a private pip package located in my virtual environment site-packages folder and I'd like to cythonize it for a speed boost and added protection.
My script successfully converts the files to .c, however, it places the build/ folder for the temporary .so files locally. It then tries to copy those .so files locally to a folder that does not exist. Instead, I want it to copy those files over to the venv site-packages/ python package where it had just created the .c files.
Is there a way I can specify where to copy those files over?
Or can I specify where that build folder gets created?
my_app/ (working dir/main program folder)
├── app_gui/ (my application)
├── build/ (build folder generated by cython)
virtualenvs/lib/python/site-packages/
├── my_pip_installed_package/
├── folder/
def main():
all_files = get_all_files(BASE_DIR)
py_files = [file for file in all_files if file.suffix =='.py']
py_file_strings = convert_to_str(py_files)
setup(ext_modules=cythonize(py_file_strings, compiler_directives={"language_level": "3"}))
I found what I would call a workaround to do this. Instead of running my cythonize script locally on the main application, I changed directories to the site-packages folder, changed where BASE_DIR points to and ran the script from there.
I have a project structure like this:
bin/
here I hold binaries, that "main.py" needs
those binaries are mostly shell scripts
src/
__init__.py (empty file, but pyinstaller needs it)
main.py (ENTRY POINT FOR APP)
main.glade (GTK Glade file for GUI)
funcs.py
setup.py (I configured it in order to use "import" reference from project's root)
assets/
here I hold *.svg images, that "main.py" uses
utils/
here I hold *.txt files, that "main.py" uses
Question: HOW do I compile all those files into ONE executable, which I could place into /usr/bin directory in order to execute it from EVERYWHERE?
What I do have tried so far is this:
pyinstaller --onefile main.py
It indeed creates me ONE executable file, however it only compiled all *.py files into ONE executable no all those images, text files and other binaries. Is there a way to do it, though? OR there is an another good option to somehow PACK this project and place it into /usr/bin?
Thank you in advance, cheers!
When I was compiling the desired folder and Python program into an executable using Pyinstaller I used datas = [("C:Users/Snoxzy/project/data", "data")],... line in .spec file to include 'data' folder into executable that would also be accessible as 'data' in bundle that pyinstaller would create during compilation. My Python program references a .png file from 'C:Users/Snoxzy/project/data' folder and that won't work if I later delete that .png file. If I was able to reference the newly created 'data' folder instance of 'C:Users/Snoxzy/project/data' in bundle that pyinstaller created I could later delete the original .png file and executable should still be working because I am referencing .png file from a 'data' folder that is integrated directly into executable, but I don't know how to do that. More precisley, the part of code where I refrence .png file is: field = ImageTk.PhotoImage(Image.open('C:Users/Snoxzy/project/data/other/field.png')). What should I use insted of this 'C:Users/Snoxzy/project/data/other/field.png' to access bundle folder 'data'?
You can use sys.executable to find the path to python.exe in your executable. Then use os module to build out the path to your .png. This example will work if you have your data folder in the same dir as python.exe.
import sys
import os
field = os.path.join(os.path.dirname(sys.executable), ('data\\' 'other\\' 'field.png'))
What files can I delete after creating .exe file with pyinstaller in order not to damage the application?
The directory contains:
.idea/
__pycache__/
build/
dist/
venv/
main.py
main.spec
...as also shown in this graphical listing:
You can delete build and dist, after you got the exe out. __pycache__ is created by python every time you run the application.
Background
I'm developing a python package with roughly the following directory structure:
mossutils/
setup.py
mossutils/
__init__.py
init.py
data/
style.css
script.js
...
My package's setup.py declares console_scripts and includes package_data files:
setup(
name='mossutils',
packages=['mossutils'],
package_data={"mossutils": ["data/*"]},
entry_points = {
"console_scripts": ['mu-init = mossutils.init:main']
},
...)
Installing the package via pip install works as expected: everything is installed in my Python's Lib\site-packages, including the data directory and all files in it, and script mu-init can be executed from the shell (or rather, command prompt, since I'm using Windows).
Goal
Script mu-init is supposed to do some kind of project scaffolding in the current working directory it is invoked from. In particular, it should copy all package_data files (data/style.css, data/script.js, ...) to the current directory.
Solution Attempt
Using module pkgutil, I can read the content of a file, e.g.
import pkgutil
...
data = pkgutil.get_data(__name__, "data/style.css")
Questions
Is there a way for my init.py script to iterate over the contents of the data directory, without hard-coding the file names (in init.py)?
Can the files from the data directory be copied to the current working directory, without opening the source file, reading the content, and writing it to a destination file?
You can get the list of files in the directory using pkg_resources library, which is distributed together with setuptools.
import pkg_resources
pkg_resources.resource_listdir("mossutils", "data")