6

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.

Xonshiz
  • 1,307
  • 2
  • 20
  • 48
  • Did you try cx_Freeze? It will do all the hard work for you with ease. – Stam Kaly Nov 07 '16 at 12:34
  • You were close with `pyinstaller`. The `datas` option doesn't work like that. You need to do something similar to [this](http://stackoverflow.com/a/12033695/3837382). Personally, I'd stick with `pyinstaller` because (as far as I know) it is the only one that is actively developed/maintained. – Repiklis Nov 08 '16 at 12:29
  • cxfreeze 5.0 is coming out soon with support of python3.5, it's all about opinions though... – Stam Kaly Nov 12 '16 at 15:59
  • @Repiklis : I have already tried that. But, my script takes arguments to work. Which works fine in .py file. But, when I run it through Pyinstaller as mentioned in that link. It does nothing. Just makes an exe which literally does nothing. – Xonshiz Nov 13 '16 at 15:46
  • [THIS](https://github.com/Xonshiz/comic-dl) is the repository and I wanted to make a `onefile` exe for it. Couldn't do it. Got anything for this? – Xonshiz Nov 13 '16 at 16:37

1 Answers1

3

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.

Stam Kaly
  • 668
  • 1
  • 11
  • 26
  • Yes! This friggin' worked like a charm. Had to search a little to install cxfreeze, but it's working! Thanks a ton! – Xonshiz Nov 07 '16 at 14:09
  • Yeah it was my bad that I made you search for the installation, its been a while since I last worked on a windows machine. Glad that `cx_Freeze` did the trick for you. – Stam Kaly Nov 07 '16 at 14:39
  • @Xonshiz could you please remove that extra sentence in the solution since it was my bad? Just not to confuse people with the same problem as yours. – Stam Kaly Nov 07 '16 at 14:53
  • which line do you want me to remove? let me know, I'll remove ASAP :D – Xonshiz Nov 07 '16 at 15:00