Make standalone executable from Python code with PyInstaller

Published on: 01.09.2017

I wanted to create single file that person could run on Windows machine, from my Pythone code.

After some investigation I found PyInstaller and 1 hour later I had my EXE file from Python code.

Process for generating EXE files from Python code with PyInstaller was quite easy, at least from my experience.

I have used it on Windows 7 64-bit and had no problems.

My program was one file script with 300 line and dependencies to docopt and pyautogui.

Steps for generating exe file with PyInstaller

This will install PyInstaller
pip install pyinstaller

This will generate script.exe in dist directory
pyinstaller --onefile script.py

After this you have your EXE program.

How PyInstaller is working

Here I have used --onefile option for PyInstaller what will make one file EXE program.

If you just use pyinstaller script.py, with out --onefile option, than in dist folder you will get folder script with EXE file and all additional files for your EXE file to work.

If you use --onefile option, then your one file EXE program need every time to uncompresses all files every time when it starts.

Uncompression is described in details in official documentation, temporary directory for uncompression in Windows is %TEMP%.

Some other solutions:
http://nsis.sourceforge.net/Main_Page
http://nuitka.net/
https://pypi.python.org/pypi/pynsist
https://cx-freeze.readthedocs.io/en/latest/
http://www.py2exe.org/

Comparison of some others solutions.

Computer setup, electricity consumption

Published on: 01.08.2017

My current computer setup (in 8/2017) is
Apple MacBook laptop “Core 2 Duo” 2.4 GHz 13-inch with
Dell 24-inch UltraSharp Monitor – U2412M

Conclusion

I was surprised how little electricity is used by computer and monitor.

All together it is maximum of 50 Watts, so for 1 kilo Watt hour, 20 hours of work is needed.

I remember how CRT 15 inch monitor was using 70 Watt.

Pentium 3 was using 55 Watt and Pentium 4 was using 130 Watt, at 100 CPU.

Even my 19 inch HP L1950g is using 30 Watt.

What is much more than 24 inch and it is much smaller than 24 inch.

Planned obsolescence from Apple

In MacBook I have 10 GB of DDR3 1067 MHz memory.

Originally I had 4 GB but that was too little, so I had idea to put 16 GB inside 2x 8GB.

But, for ever reason (I think that Apple have some software/hardware lock, so that you need to buy new laptop and can not just install more memory) 2x 8 GB did not work, I could not boot it.

With try and error, I found that if I leave original 2 GB in one slot and put 8 GB in another slot that it is working fine and that is how I have 10 GB.

Basics of electricity consumption

This will just be basic introduction to electricity consumption, so that you can understand what is Watt.

For electricity consumption you always have two parts:
– electric current, it is measured in ampere
– voltage, it is measured in volt

To get electricity consumption you need to multiply electric current(ampers) with voltage(volts).

Unit of measurement for electricity consumption is called Watt.

Usually electrical devices are rated in Watt/hour, what just means how many watt will some device use in 1 hour of their work.

Electricity consumption of monitor

Electricity consumption of monitor depend on how white/bright is your screen.

So you can separate it by brightness and main color of screen.

Table show some real measurements.

Main Background Color Brightness
0% 75% 100%
White 10.9 Watt 20.3 Watt 22.7 Watt
Black 8.2 Watt 17.3Watt 19.9Watt

More brightness use more electricity, same is with white color.

I also have tested contrast, but contrast does not have big impact.

With brightness and contrast set to 0%, electricity consumption was 7.9 Watt, regardless of background color.

But with brightness and contrast set to 0%, you just have black screen with just small back-light, so it is not useful.

With brightness and contrast set to 100%, electricity consumption for white color was 22.9 Watt and for black 19.9 Watt.

I have my monitor brightness and contrast set to 75% and electricity consumption was never above 20 Watt.

When Flurry screensaver is running it is 17.6 Watt.

For one kilo Watt to be used, with biggest possible consumption of 20 Watt, monitor need to be on for 50 hours.

Electricity consumption of MacBook

MacBook is laptop, so it has a screen, but screen brightness had little effect on electricity consumption.

Maximum difference between 0% and 100% brightness was 4 Watt and this was at load of 100% CPU.

When CPU load was at 10%, when computer is not in active use, difference was at 2-3 Watt.

Table show some real measurements.

Test Brightness
0 % 100%
CPU 10% 15 Watt 17 Watt
CPU 100% 28 Watt 32 Watt
Flurry screensaver 8 Watt 20 Watt

My regular work is at 25% CPU.

As it can been seen from table, main part of electricity consumption in computer is CPU load.

All this measurement as done with Dell 24-inch monitor connected to MacBook.

I did same measurement with Dell 24-inch monitor disconnected, difference was just in 2-3 Watt.

Electricity consumption some of my other equipment

iPhone 4 use 10 Watt to charge Battery from 0% to 100%.

iPad Retina Mini use 35 Watt to charge Battery from 0% to 100%. After 20 charging that is 1 kilo Watt, what I spend per month and I use it every day few (around 3) hours.

Additional explanation of measurements

I have used Velleman NETBSEM5 ENERGY METER for measuring electricity consumption.

For white Main Background Color I used https://www.google.com.

For black Main Background Color I used http://www.blackle.com/, look-like they really save electricity.

To get CPU at 100%, just yes > /dev/null & was used.

Grokking Algorithms, book review

Grokking_Algorithms-Book_Cover

Published on: 01.07.2017

Number of pages: 256
Written by: Aditya Bhargava
Publish by: Manning

Conclusion
Interesting book, visualizations are very helpful.

Review
When I saw this book first time I was not interested.
Book cover was too childish for me.

But after listening podcast about it, I changed my mind.

Illustrations in book are used to explain concepts, and they are very useful.
All code is in python, that was bonus point for me.

Here is video example of illustration:

I do recommend this book for reading, you can understand Big O notation from it and algorithms.

Ethernet Switches, book review

ethernet_switches-Book_Cover

Published on: 01.06.2017

Number of pages: 80
Written by: Charles E. Spurgeon and Joann Zimmerman
Publish by: O’Reilly Media

Conclusion
Useful to read.

Review
This is small book and I like small books.

Although it is small there is no shortage of information in book.

Book explains: what is Ethernet switch, for what is used, how it is working, VLAN (virtual LAN), STP (Spanning Tree Protocol), network design, different special purpose switches (like: multi-layer, access, industrial, wireless access point switch, ISP switch, data center switch) and advanced switch features (PoE, traffic flow monitoring).

Good read for getting advanced understanding of Ethernet switches.

How Machines Learn: An Illustrated Guide to Machine Learning, book review

How_Machines_Learn-Book_Cover

Published on: 01.04.2017

Number of pages: 63
Written by: Helen Edwards
Publish by: Self-publish

Conclusion
Best book for high-level understanding how today (in 2016/2017) machine learning is working.

Review
Illustrations in book are very helpful.
Without them it is much harder to understand machine learning concepts.
This book covers all topics in machine learning, it is not specializing on one aspect.

More thoughts on machine learning

This is my understanding of how machine learning is BIG change in software.
I would even say that machine learning is biggest change in software so far, it is a game changer.

Till last few yeas/decades, all computer programs (all work that was done by computers) had two important characteristics:
– Humans where making exact rules (algorithms) how computer will make decisions
– Everything that computer did, human could do it also

I maybe wrong, but AFAIK with machine learning, this is not true anymore.

Humans where making exact rules (algorithms) how computer will make decisions

Human was one who was designing and implementing algorithm.
If there was some BUG, person could understand what was the reason for the BUG.

But, for example, with neural networks and their weight there is no way to understand why some input have particular weight.
It is not possible to understand “computer” reasoning behind it.
This is BIG change in how algorithms work.
Because computer is fine tuning algorithm, a human can not understand how or why in details.

Everything that computer did, human could do it also

Argument is: computer can calculate average of 1000 number in less the second.
Human can do it also, but it need more time, much more time.
So computer are used for this kind of work because it is more profitable, but still human can do it.

In this argument I am exempting robots that work in conditions that are harmful for human, like high temperature, radiation, etc.
I am just talking about software/calculations.

In unsupervised learning goal is to find patterns that humans do not know about.
Humans can find some patterns across few dimensions, but even after 10 dimensions, what to speak of 100 or more it is not possible for human.

What will be result I do not know, but problems will arise

I do not see big problem when neural network is used to do OCR, eg. you write some numbers on you touch screen, or when it classifies pictures of dogs and cats.
If there is some error human can recognize error and cost of error is not important.
Not important in contexts, that result of error do not have huge impact on person life.
You got number 5 instead of 3, or it showed you a dog instead of cat.

But what if neural networks starts to be used for much more important decision ?

For example: visa acceptance, medical diagnostics, bank credit approval, decision if person is terrorist or not.

I would not be surprise that in future, there will be neural network making decisions, without anybody understanding what is the reason behind decision.

How can you even debug neural network when you have no idea how it decided on its input weights ?

How to know is it false negative or false positive ?

It will be interesting to see where machine learning will be in 10 years.

Essential Copying and Pasting From Stack Overflow, book review

essential_copying_and_pasting_from_stack_overflow-Book_Cover

Published on: 01.04.2017

Number of pages: n/a
Written by: Stack Overflow
Publish by: O’Reilly Media

Conclusion
Best book to make you better coder.
Most important book for every professional developer.
Most important book for any professional developer.
Most important book for all professional developer.

Review
I can not overexert importance of this book.
This book should be read from cover to cover at least every second day before noon.

Forget about algorithms, data structures, CPU/memory trade-off, Big O notation, copy/pasting from Stack Overflow is what gets thing done, apps build and make you billionaire.

But not everybody can be super developer just with internet connection and access to Stack Overflow.
To beat professional developer you need to learn how to effectively copy/paste.

Few tips are:

Real men use keyboard shortcuts.
Their little finger is always on CTRL and index finger ready for C.
Or Command, if their Linux is broken.

Recognizing good code from bad code (code planted from NSA to make backdoor in your software).
Although to let NSA backdoor in your software is not so bad, then they can fix it when it stops working, because you will not know how after successful career in copy/pasting.

Advanced tips:

How to written interesting code questions, so that other people write your code for free.

Fell free to share your effective copying and pasting from Stack Overflow in comments.

Learning Selenium Testing Tools with Python, book review

Learning_Selenium_Testing_Tools_with_Python-Book_Cover

Published on: 01.03.2017

Number of pages: 216
Written by: Unmesh Gundecha
Publish by: Packt Publishing

Conclusion
Book about Selenium with Python examples.

Review
Only book about Selenium with Python examples that I have found so far.
I found much more books with Java examples, alto it is not difficult to translate syntax from Java to Python, because Selenium API is similar, I prefer to read code in Python.

Usually I read programing books for two reasons:
– when I do not know anything about topic, so that I get basic understanding
– when I have already used technology, even did some project, but want to see what I do not know about it

With Selenium is second reason.

Book is good, it have lot of code examples how to use Selenium API.
Good to have as reference.

Automate the Boring Stuff with Python, book review

automate-the-boring-stuff-with-python-Book_Cover

Published on: 01.02.2017

Number of pages: 504
Written by: Al Sweigart
Publish by: No Starch Press

Conclusion
Best book for learning programming.

Review
I am very impress with this book.
I am recommending it to anybody who want to learn programming.

Book is available for free online.
Book is also available as video tutorial and first 15 videos are on YouTube for free.

This book is going to teach you how to use programming knowledge to automate tasks on computer.
Some examples are: send emails, automate web page interaction, automate GUI interaction.
All other books on programing for beginners just teach you basic programming, but after that people have no idea how to use it in everyday life.
This book is different because it will show you real examples how to use it in everyday life.