Python 7. Simple compiled .Exe example with cx_Freeze

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

After my first attempt to compile an executable programme with python, with no success. I thought I’d give it another try. The issue seems to be that the compilers do not play well with Python 3.6 yet.

The nearest I came to getting anything working was with the CX_Freeze module with a setup.py file  as per the documentation. I dodn’t get that method to work.

CX_Freezewith .BAT process

I came across this article in stack overflow.  I will put part of it here. (by Pickaroony):

“I installed cx_Freeze from https://pypi.python.org/pypi/cx_Freeze

When you install it, there’s an extra step that’s not documented. Using the Windows CMD shell, go to your python installation directory, into the Scripts sub-directory and you will find a few files called cxfreeze…

You need to create the batch file by typing python cxfreeze-postinstall

A file called cxfreeze.bat will be created which you can move into your main python folder or wherever your python program is located.

Then type cxfreeze mypythonprogram.py –target-dir dist

This should create a working exe in the dist directory. However, it doesn’t work for me as I get an error in the finder.py file which is part of the cxfreeze installation so I don’t know if I’ve done something wrong or this version is completely buggy.”

My process

I had previously installed cx_Freeze. So I went to my Python36\Lib\site-packages\cx_Freeze install directory and on the CMD line in that directory did the :

C:\Installed Directory >python cxfreeze-postinstall

and created the : cxfreeze.bat which is 2 lines:

@echo off
C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36\python.exe C:\Users\xxxxxxe\AppData\Local\Programs\Python\Python36\Scripts\cxfreeze %*

And after moving this file to my Python install:

C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36\

I opened a CMD window and ran: C:\ ..\Python36> cxfreeze mypythonprogram.py –target-dir x

and got errors.It was looking for “tcl\tcl8.6” which I had noticed in the other attempts I had tried for compiling the programme,

so added the following lines to the cxfreeze.bat

@echo off
set TCL_LIBRARY=C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36\tcl\tcl8.6
set TK_LIBRARY=C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36\tcl\tk8.6
C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36\python.exe C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36\Scripts\cxfreeze %*

And I got a compiled programme, unfortunately that didn’t work on the original pdf.py code.  A more complex code calling .csv files to read from and write to.

So I tried with a simpler code: A file Hello.py

print(“Hello World”)
input(“Press any key to continue….”)

(I put the input() in so that the CMD window wouldn’t close straight after the print(“Hello World”)),

and running the: Hello.exe  file the output is:

So, although the example is trivial, there is an executable file that is running after being compiled using Python 3.6 and cx_Freeze.

I can now build on this instance and debug the more complex code to see if I can get more useful Executable files.

I have just re-read the original StackOverflow article and noticed this from Pickaroony:

unfortunately I am unable to run the EXE on another PC, even by copying the whole dist folder. When I run it from CMD on the second PC it looks for a load of python files. Basically you need to install python in exactly the same structure as on the development PC or it won’t work. This is very disappointing… I’m not sure how anyone is supposed to develop python programs for public distribution on windows.

So I will need to try the Hello.exe out on another PC to see if it does work.

I  decided to look at the files a bit more carefully.

1/ I copied the Directory to a USB drive and ran the Hello.exe from there, it worked.

2/ I then copied just the Hello.exe to another directory and it would not run! But when I copied the “Y” directory across and ran it with the support files it worked fine.

3/ Copied the complete directory to another computer, the executable works fine.

The file structure is as follows:

And when you look at the folders properties:

11Mbs of files and 188 files just to say “Hello World”. A lot of overhead really.

I have since gone back and tried to do the more complex programme, pdf.py :

import pandas as pd

df3d = pd.read_csv(‘KaroriLib.csv’)
col_headers=[‘family’,’property’,’value’]
df3d.columns =col_headers
dfRm=df3d[df3d[‘family’].str.contains(‘3dRmTg’)]
Rooms= dfRm.pivot(values=’value’,index= ‘family’,columns= ‘property’)
colDel=[col for col in Rooms.columns if ‘[00]’ in col]
Rooms2=Rooms.drop(colDel, axis=1, errors=’ignore’)

BlockCode=[col for col in Rooms2.columns if ‘WCC_BlockCode’ in col]

cols0=BlockCode

Rooms3=Rooms2[cols0]

print(Rooms3)
input(“press enter to continue..”)

And that still doesn’t run, even after a cleanup. Also the CMD window does not pause at the end on the input() code. (it does in PyChem). So I think this compiler may be a bit delicate.

Simple Python scripts seem to work, such as this one, a random number on a question, and the compile size is the same as the Hello.exe:

import sys
import random

ans = True

while ans:
question = input(“Ask the magic 8 ball a question: (press enter to quit) “)

answers = random.randint(1, 8)

if question == “”:
sys.exit()

elif answers == 1:
print(“It is certain”)

elif answers == 2:
print(“Outlook good”)

elif answers == 3:
print(“You may rely on it”)

elif answers == 4:
print(“Ask again later”)

elif answers == 5:
print(“Concentrate and ask again”)

elif answers == 6:
print(“Reply hazy, try again”)

elif answers == 7:
print(“My reply is no”)

elif answers == 8:
print(“My sources say no”)

( this code copied verbatim from here. print changed to print() for Python 3)

So, it seems to work with simple python files but when calling external files (txt csv) it seems to be more temperamental.

In the cx_Freeze documentation it does discuss how to build the .exe with external files. I have not explored that yet.

End thoughts

The exercises in Python 4 post was to replicate a VBA macro in Excel.

The Python Data-frames definitely managed the data manipulation very well.

It did not do the transportable part well though. You would need to load Python onto all users PC to get them to use the code.

The compiling of the code in Python may not be the best method though, as Python is an un-compiled language, so trying to compile it to run an equivalent process to the Excel VBA Macro maybe is not the way to go. Using a compiled language such as C# or some programme that works with data-sets would be a better alternative to achieve a one file code.

25 Comments

Add a Comment

Your email address will not be published. Required fields are marked *