{"id":3165,"date":"2018-05-20T21:06:44","date_gmt":"2018-05-20T21:06:44","guid":{"rendered":"https:\/\/max-drake.cc\/?p=3165"},"modified":"2018-05-21T12:33:37","modified_gmt":"2018-05-21T12:33:37","slug":"python-7-simple-compiled-exe-example-with-cx_freeze","status":"publish","type":"post","link":"https:\/\/max-drake.cc\/?p=3165","title":{"rendered":"Python 7. Simple compiled .Exe  example with cx_Freeze"},"content":{"rendered":"<p>After my <a href=\"https:\/\/max-drake.cc\/2018\/05\/06\/python-4-3dpdfs-csvs-pandas-exes\/\" target=\"_blank\" rel=\"noopener\"><strong>first attempt<\/strong><\/a> to compile an executable programme with python, with no success. I thought I&#8217;d give it another try. The issue seems to be that the compilers do not play well with Python 3.6 yet.<\/p>\n<p>The nearest I came to getting anything working was with the CX_Freeze module with a <strong>setup.py <\/strong>file&nbsp; as per the documentation. I dodn&#8217;t get that method to work.<\/p>\n<h3>CX_Freezewith .BAT process<\/h3>\n<p>I came across <a href=\"https:\/\/stackoverflow.com\/questions\/42541623\/how-do-i-convert-my-code-from-python3-6-to-exe-using-cxfreeze\" target=\"_blank\" rel=\"noopener\"><strong>this article<\/strong><\/a> in stack overflow.&nbsp; I will put part of it here. <em> (by<a href=\"https:\/\/stackoverflow.com\/users\/4544072\/pickarooney\" target=\"_blank\" rel=\"noopener\"> Pickaroony<\/a>)<\/em>:<\/p>\n<p style=\"padding-left: 90px;\"><em>&#8220;I installed cx_Freeze from <a href=\"https:\/\/pypi.python.org\/pypi\/cx_Freeze\" rel=\"nofollow noreferrer\"><strong>https:\/\/pypi.python.org\/pypi\/cx_Freeze<\/strong><\/a><\/em><\/p>\n<p style=\"padding-left: 90px;\"><em>When you install it, there&#8217;s an extra step that&#8217;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&#8230;<\/em><\/p>\n<p style=\"padding-left: 90px;\"><em>You need to create the batch file by typing <strong>python cxfreeze-postinstall<\/strong><\/em><\/p>\n<p style=\"padding-left: 90px;\"><em>A file called cxfreeze.bat will be created which you can move into your main python folder or wherever your python program is located.<\/em><\/p>\n<p style=\"padding-left: 90px;\"><em>Then type <strong>cxfreeze mypythonprogram.py &#8211;target-dir dist<\/strong><\/em><\/p>\n<p style=\"padding-left: 90px;\"><em>This should create a working exe in the dist directory. However, it doesn&#8217;t work for me as I get an error in the finder.py file which is part of the cxfreeze installation so I don&#8217;t know if I&#8217;ve done something wrong or this version is completely buggy.&#8221;<br \/>\n<\/em><\/p>\n<h3>My process<\/h3>\n<p>I had previously installed cx_Freeze. So I went to my <em>Python36\\Lib\\site-packages\\cx_Freeze<\/em> install directory and on the CMD line in that directory did the :<\/p>\n<p>C:\\Installed Directory &gt;<em><strong>python cxfreeze-postinstall <\/strong><\/em><\/p>\n<p>and created the : <em><strong>cxfreeze.bat <\/strong>which is 2 lines: <\/em><\/p>\n<p style=\"padding-left: 30px;\"><em>@echo off<\/em><br \/>\n<em>C:\\Users\\xxxxxx\\AppData\\Local\\Programs\\Python\\Python36\\python.exe C:\\Users\\xxxxxxe\\AppData\\Local\\Programs\\Python\\Python36\\Scripts\\cxfreeze %*<\/em><\/p>\n<p>And after moving this file to my Python install:<\/p>\n<p style=\"padding-left: 60px;\"><em>C:\\Users\\xxxxxx\\AppData\\Local\\Programs\\Python\\Python36\\<\/em><\/p>\n<p>I opened a CMD window and ran: C:\\ ..<em>\\Python36&gt; <\/em><em><strong>cxfreeze mypythonprogram.py &#8211;target-dir x <\/strong><\/em><\/p>\n<p>and got errors.It was looking for &#8220;tcl\\tcl8.6&#8221; which I had noticed in the other attempts I had tried for compiling the programme,<\/p>\n<p>so added the following lines to the <em><strong>cxfreeze.bat <\/strong><\/em><\/p>\n<p style=\"padding-left: 90px;\"><em>@echo off<\/em><br \/>\n<em><strong>set TCL_LIBRARY=C:\\Users\\xxxxxx\\AppData\\Local\\Programs\\Python\\Python36\\tcl\\tcl8.6<\/strong><\/em><br \/>\n<em><strong>set TK_LIBRARY=C:\\Users\\xxxxxx\\AppData\\Local\\Programs\\Python\\Python36\\tcl\\tk8.6<\/strong><\/em><br \/>\n<em>C:\\Users\\xxxxxx\\AppData\\Local\\Programs\\Python\\Python36\\python.exe C:\\Users\\xxxxxx\\AppData\\Local\\Programs\\Python\\Python36\\Scripts\\cxfreeze %*<\/em><\/p>\n<p>And I got a compiled programme, unfortunately that didn&#8217;t work on the original pdf.py code.&nbsp; A more complex code calling .csv files to read from and write to.<\/p>\n<p>So I tried with a simpler code: A file <strong>Hello.py<\/strong><\/p>\n<p style=\"padding-left: 90px;\"><em>print(&#8220;Hello World&#8221;)<\/em><br \/>\n<em>input(&#8220;Press any key to continue&#8230;.&#8221;)<\/em><\/p>\n<p>(I put the input() in so that the CMD window wouldn&#8217;t close straight after the <em>print(&#8220;Hello World&#8221;)<\/em>),<\/p>\n<p>and running the: <strong>Hello.exe<\/strong>&nbsp; file the output is:<\/p>\n<p><img decoding=\"async\" class=\"wp-image-3245 aligncenter lazyload\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa07-1024x825.png\" alt=\"\" width=\"1245\" height=\"1003\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa07-1024x825.png 1024w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa07-300x242.png 300w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa07-768x619.png 768w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa07.png 1599w\" data-sizes=\"(max-width: 1245px) 100vw, 1245px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1245px; --smush-placeholder-aspect-ratio: 1245\/1003;\" \/><\/p>\n<p>So, although the example is trivial, there is an executable file that is running after being compiled using Python 3.6 and cx_Freeze.<\/p>\n<p>I can now build on this instance and debug the more complex code to see if I can get more useful Executable files.<\/p>\n<p>I have just re-read the original StackOverflow article and noticed this from <em><a href=\"https:\/\/stackoverflow.com\/users\/4544072\/pickarooney\" target=\"_blank\" rel=\"noopener\">Pickaroony<\/a><\/em>:<\/p>\n<p style=\"padding-left: 60px;\"><em><span class=\"comment-copy\">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&#8217;t work. This is very disappointing&#8230; I&#8217;m not sure how anyone is supposed to develop python programs for public distribution on windows.<\/span> <\/em><\/p>\n<p>So I will need to try the <strong>Hello.exe<\/strong> out on another PC to see if it does work.<\/p>\n<p>I&nbsp; decided to look at the files a bit more carefully.<\/p>\n<p>1\/ I copied the Directory to a USB drive and ran the Hello.exe from there, it worked.<\/p>\n<p>2\/ I then copied just the Hello.exe to another directory and it would not run! But when I copied the &#8220;Y&#8221; directory across and ran it with the support files it worked fine.<\/p>\n<p>3\/ Copied the complete directory to another computer, the executable works fine.<\/p>\n<p>The file structure is as follows:<\/p>\n<p><img decoding=\"async\" class=\"wp-image-3252 aligncenter lazyload\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa08-1024x232.jpg\" alt=\"\" width=\"1286\" height=\"292\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa08-1024x232.jpg 1024w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa08-300x68.jpg 300w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa08-768x174.jpg 768w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa08.jpg 1229w\" data-sizes=\"(max-width: 1286px) 100vw, 1286px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1286px; --smush-placeholder-aspect-ratio: 1286\/292;\" \/><img decoding=\"async\" class=\"wp-image-3251 aligncenter lazyload\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa09-1024x701.jpg\" alt=\"\" width=\"1295\" height=\"886\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa09-1024x701.jpg 1024w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa09-300x205.jpg 300w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa09-768x526.jpg 768w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa09.jpg 1209w\" data-sizes=\"(max-width: 1295px) 100vw, 1295px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1295px; --smush-placeholder-aspect-ratio: 1295\/886;\" \/><\/p>\n<p>And when you look at the folders properties:<\/p>\n<p>11Mbs of files and 188 files just to say &#8220;Hello World&#8221;. A lot of overhead really.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-3250 lazyload\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa10-1024x506.jpg\" alt=\"\" width=\"1336\" height=\"660\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa10-1024x506.jpg 1024w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa10-300x148.jpg 300w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa10-768x379.jpg 768w, https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa10.jpg 1622w\" data-sizes=\"(max-width: 1336px) 100vw, 1336px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1336px; --smush-placeholder-aspect-ratio: 1336\/660;\" \/><\/p>\n<p>I have since gone back and tried to do the more complex programme, <strong>pdf.py <\/strong>:<\/p>\n<p><em>import pandas as pd<\/em><\/p>\n<p><em>df3d = pd.read_csv(&#8216;KaroriLib.csv&#8217;)<\/em><br \/>\n<em>col_headers=[&#8216;family&#8217;,&#8217;property&#8217;,&#8217;value&#8217;]<\/em><br \/>\n<em>df3d.columns =col_headers<\/em><br \/>\n<em>dfRm=df3d[df3d[&#8216;family&#8217;].str.contains(&#8216;3dRmTg&#8217;)]<\/em><br \/>\n<em>Rooms= dfRm.pivot(values=&#8217;value&#8217;,index= &#8216;family&#8217;,columns= &#8216;property&#8217;)<\/em><br \/>\n<em>colDel=[col for col in Rooms.columns if &#8216;[00]&#8217; in col]<\/em><br \/>\n<em>Rooms2=Rooms.drop(colDel, axis=1, errors=&#8217;ignore&#8217;)<\/em><\/p>\n<p><em>BlockCode=[col for col in Rooms2.columns if &#8216;WCC_BlockCode&#8217; in col]<\/em><\/p>\n<p><em>cols0=BlockCode<\/em><\/p>\n<p><em>Rooms3=Rooms2[cols0]<\/em><\/p>\n<p><em>print(Rooms3)<\/em><br \/>\n<em>input(&#8220;press enter to continue..&#8221;)<\/em><\/p>\n<p>And that still doesn&#8217;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.<\/p>\n<p>Simple Python scripts seem to work, such as this one, <em>a random number on a question, <\/em>and the compile size is the same as the Hello.exe:<\/p>\n<p style=\"padding-left: 180px;\"><em>import sys<\/em><br \/>\n<em>import random<\/em><\/p>\n<p style=\"padding-left: 180px;\"><em>ans = True<\/em><\/p>\n<p style=\"padding-left: 180px;\"><em>while ans:<\/em><br \/>\n<em> question = input(&#8220;Ask the magic 8 ball a question: (press enter to quit) &#8220;)<\/em><\/p>\n<p style=\"padding-left: 180px;\"><em>answers = random.randint(1, 8)<\/em><\/p>\n<p style=\"padding-left: 180px;\"><em>if question == &#8220;&#8221;:<\/em><br \/>\n<em> sys.exit()<\/em><\/p>\n<p style=\"padding-left: 180px;\"><em>elif answers == 1:<\/em><br \/>\n<em> print(&#8220;It is certain&#8221;)<\/em><\/p>\n<p style=\"padding-left: 180px;\"><em>elif answers == 2:<\/em><br \/>\n<em> print(&#8220;Outlook good&#8221;)<\/em><\/p>\n<p style=\"padding-left: 180px;\"><em>elif answers == 3:<\/em><br \/>\n<em> print(&#8220;You may rely on it&#8221;)<\/em><\/p>\n<p style=\"padding-left: 180px;\"><em>elif answers == 4:<\/em><br \/>\n<em> print(&#8220;Ask again later&#8221;)<\/em><\/p>\n<p style=\"padding-left: 180px;\"><em>elif answers == 5:<\/em><br \/>\n<em> print(&#8220;Concentrate and ask again&#8221;)<\/em><\/p>\n<p style=\"padding-left: 180px;\"><em>elif answers == 6:<\/em><br \/>\n<em> print(&#8220;Reply hazy, try again&#8221;)<\/em><\/p>\n<p style=\"padding-left: 180px;\"><em>elif answers == 7:<\/em><br \/>\n<em> print(&#8220;My reply is no&#8221;)<\/em><\/p>\n<p style=\"padding-left: 180px;\"><em>elif answers == 8:<\/em><br \/>\n<em> print(&#8220;My sources say no&#8221;)<\/em><\/p>\n<p>( this code copied verbatim from <a href=\"http:\/\/www.pythonforbeginners.com\/code-snippets-source-code\/python-code-examples\/\" target=\"_blank\" rel=\"noopener\">here<\/a>. print changed to print() for Python 3)<\/p>\n<p>So, it seems to work with simple python files but when calling external files (txt csv) it seems to be more temperamental.<\/p>\n<p>In the <a href=\"https:\/\/cx-freeze.readthedocs.io\/en\/latest\/faq.html#problems-with-running-frozen-programs\" target=\"_blank\" rel=\"noopener\">cx_Freeze documentation<\/a> it does discuss how to build the .exe with external files. I have not explored that yet.<\/p>\n<h3>End thoughts<\/h3>\n<p>The exercises in <a href=\"https:\/\/max-drake.cc\/2018\/05\/06\/python-4-3dpdfs-csvs-pandas-exes\/\" target=\"_blank\" rel=\"noopener\">Python 4 post<\/a> was to replicate a VBA macro in Excel.<\/p>\n<p>The Python Data-frames definitely managed the data manipulation very well.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After my first attempt to compile an executable programme with python, with no success. I thought I&#8217;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&nbsp; as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3245,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-3165","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"featured_image_src":"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa07.png","featured_image_src_square":"https:\/\/max-drake.cc\/wp-content\/uploads\/2018\/05\/impa07.png","author_info":{"display_name":"Max Drake","author_link":"https:\/\/max-drake.cc\/?author=1"},"_links":{"self":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/posts\/3165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3165"}],"version-history":[{"count":0,"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/posts\/3165\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/media\/3245"}],"wp:attachment":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}