简体   繁体   中英

Packing custom .py files into .exe

I've made a little project that includes importing various python files into another python files. Here's 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 is imported inside my my_main_file.py by the following command :

from Sites import *

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

My python script takes command line arguments and it is working. The python script will not work without command line arguments. I can convert to exe, but that exe is doing nothing even when I give arguments.

So, I got a .spec file from pyinstaller and modified it to get my some_other.py file. 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 makes an .exe , but that exe won't work.The exe won't show anything. But, it is like 11 MB in size. Got anything for this? I tried nuitka , but that one gives out an error about not being able to find gendef.exe and I'm not really interested in installing minGW.

Btw, I'm on a 64 bit machine and py2exe's bundle_file:1 won't work for me.

I would suggest you to use cx_Freeze , the best python code bundler in my opinion.

Installation

The installation is pretty straight-forward, just download and install this cx_Freeze installer ( this one if you are on python 3.4) taken from the official PyPi package site . Once this is done, open cmd and run python cxfreeze-postinstall

py to exe with cx_Freeze

The fastest way of doing it is opening a command prompt on your project's parent directory and running cxfreeze <your .py script> . This will create a dist folder which contains your main program and all the modules it imports.

However, if you want to go advanced and have a wider selection of options when making your code an executable, you can make a setup.py as explained here and then run in a cmd python setup.py build . Unlike the previous method this creates a directory named build which contains another directory with the name of the platform you are compiling on. Now, inside it there is the executable and the libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM