Packaging custom .py files in .exe

I did a small project that includes importing various python files into other python files. Here is my directory structure.

Main_Folder |_ my_main_file.py |_ Sites (a directory inside Main_Folder) |_ __init__.py |_ some_other.py 

This is basically my directory structure. This some_other.py imported inside my my_main_file.py with the following command:

from Sites import *

I import everything from this directory. So what I wanted to do was make this whole project in a standalone binary . I use pyintaller to convert my .py files to .exe . But I only wrote scripts that have everything in 1 file, which makes the task easier. But this time I'm trying to do something new.

My python script takes command line arguments and works. Python script will not work without command line arguments. I can convert to exe, but this exe does nothing, even when I give arguments.

So, I got the .spec file from pyinstaller and modified it to get some_other.py file. The SPEC file looks like this:

 # -*- mode: python -*- block_cipher = None a = Analysis(['my_main_file.py'], pathex=['C:\\Users\\User Name\\Main_Folder'], binaries=None, datas=[('Sites/*.py','Sites')], hiddenimports=[], hookspath=[], runtime_hooks=[], excludes=[], win_no_prefer_redirects=False, win_private_assemblies=False, cipher=block_cipher) pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, name='my_main_file', debug=False, strip=False, upx=True, console=True ) 

This does the .exe , but this exe will not work. Exe will not show anything. But it looks like 11 MB. Is there anything for this? I tried nuitka , but that gives an error saying that I could not find gendef.exe , and I'm not very interested in installing minGW.

Btw, I'm on a 64-bit machine, and py2exe bundle_file:1 will not work for me.

+6
source share
1 answer

I would advise you to use cx_Freeze , the best code provider for python, in my opinion.

Installation

Installation is quite simple, just download and install this cx_Freeze installer ( this one if you are on python 3.4), taken from the official PyPi site package. When this is done open cmd and run python cxfreeze-postinstall

py to exe with cx_Freeze

The quickest way to do this is to open a command prompt in the parent directory of the project and run cxfreeze <your .py script> . This will create a dist folder containing your main program and all imported modules.

However, if you want to switch to advanced mode and have a wider selection of options when your code is executable, you can do setup.py as described here and then run cmd python setup.py build . Unlike the previous method, a directory is created with the name build , which contains another directory with the name of the platform on which you are compiling. Now inside it there is an executable file and libraries.

+3
source

Source: https://habr.com/ru/post/1012054/


All Articles