Monday, 24 November 2014

Python packaging/freezing experiment with Nuitka and cx_Freeze


A while ago I made the decision to move to Ubuntu Linux at the PC that I use at the office as well as the computer that I use at home. To me a Linux environment is more appealing. Although I've used Windows for a very long time I'm very attracted to the freedom that I have in Linux and that's why I started to clean up my Windows devices to be able to make a smooth transition to the new operating system. Recently I started with finishing some unfinished projects. Some of them can only run or Windows or take too much time to set up within the new Linux environment. So the easiest way was to finish them. One of this projects was a small experiment with Python freezing tools to find out how well Nuitka was able to freeze packages.

Freezing is an interesting process which allows you to distribute your Python based apps to other users. What you basically do during a freezing process is grabbing all the Python code and the related dependencies and you place them in a single place together with a Python interpreter. In this way the users can run your app.

During my experiment I played with two packages which allow you to freeze a Python project: Nuitka and cx_Freeze. The last one is a very well known tool to me. cx_Freeze allowed me to package a production grade app without problems. Although the configuration can be a little bit frustrating because the documentation lacks some examples it is a very reliable package. I would advice it to everybody who wants to freeze Python 3.x apps.

The second freezing tool is Nuitka. It's a Python compiler which compiles every construct of Python to C++. The parts that can't be compiled to C++ are executed by the default Python interpreter (libpython). This is done in a very compatible manner. This means it speeds up some parts of your application without breaking it. A nice addition to the compiler is that it can create standalone packages just like cx_Freeze and similar packages like py2exe and Pyinstaller.

screenshot example appsDuring my experiment I've frozen 2 small “Hello World!” applications: One CLI application and one GUI application based on Qt (PySide).
During the experiment I've found out that both packages are able to freeze the applications without problems. The main advantage of Nuitka was that it created a (signable) file without a library zip file which makes it less easy for people to decompile the application. It seems like Nuitka is promising because of it's performance gains and the highly compatible standalone feature. However there is still work to be done and the author needs some help in this.

At my project GitHub page you can check out the two examples. Hopefully it can help you out if you want to freeze one of your Python applications using one of these tools. I've tested it on a Windows 8.1 machine, but I'm quite sure that it will also work on other platforms.

Because I want to focus more on web development I'll not continue actively with experimenting with freezing tools. So this project is finished for now. However I'll watch the freezing projects because there are quite some interesting things going on like the updater package for Pyinstaller and I think projects like these can become a very nice foundation for upcoming (desktop) apps written in Python.


Wednesday, 19 November 2014

Automate your development laptop - Reinout van Rees @ Brainport Python Meetup #2

Last Tuesday there was a second Python meeting of The Brainport Python meetup group in Eindhoven.
The first meeting was two months ago and was received very well. Many of the people who visited the fist meeting did also attend the second one. It was really nice to see them again and to meet people who were new to the meeting group.

For the second meeting we introduced presentations as part of the program.
First Reinout van Rees did an excellent presentation about Development laptop automation. After eating some pizza I told about my journey and view on Desktop application development with use of Python. The slides can be found here.

 


Reinout did already summarize my talk. However nobody did summarize his one yet. So let me give some extra explanation about his slides. I think he did an excellent presentation about how you can optimize and automate your Development machine and it's really worth to take this topic serious. It already saved me some minutes of useless fiddling around while setting up my new development PC.

Reinout started with a very nice statement: "The shoemaker's children go barefoot". This saying is also applicable to many developers. We use version control, provisioning tools and we set up automated tasks for our products and clients, but most of us do not develop for themselves with the same care. We write scripts, but we lose them. We install software on our PC's and setup our IDE's. However we don't store the configurations. So when disaster strikes and you have to replace your machine it takes hours, days or weeks to fully recover.

The call to action is easy: "Automate your development laptop with the same care as your regular projects". You have to change your mindset a little for this: Be explicit and do something regularly.

Being explicit means that you create a safe central place where you can store your scripts, small programs and configurations. A good place for this is an on-line GIT repository.
Being explicit also means that you make an good overview of what's in your toolbox. You can do this by maintaining a README file in the repository.

The second mindset that you need is to do something regularly. So if you change something: Adjust your README file! It's also good to check which apps you have installed and to check regularly if every app is up to date. I now have for example a nice list of tools which I use very often and which I can install by just running a single command: Make install-essentials. This list of essentials will grow and shrink at some moments over time and by improving it regularly it ensures me that I'll install the right software when I need to install my essentials.

Reinout also advised to get out and start talking to other people about their setup. You can join them while programming and ask them how they organize their environment in order to find out how you can improve your own situation and increase your productivity. At last it's important to automate, improve or fix something regularly (e.g. every week) to ensure everything stays up to date.

A great tip of Reinout is to be not too perfectionistic. Reinout explained this in a very nice way. He showed us the roof of the train station of Eindhoven and some parts of the movie "A bridge too far". In that movie you see a temporary Bailey bridge being constructed. Similar bridge parts are now used as the roof in the Eindhoven station. Re-used second-hand parts, but it works fine. So you don't need to be too perfectionistic.

At last Reinout showed us some examples like zest.releaser which can automate the process of releasing zest components. Checkoutmanager is also useful since it allows you to manage multiple git repositories and it reminds you to push your changes or when to commit stuff. Various skeleton projects of Reinout help his colleagues out when they have to bootstrap a project. Beside this Reinout pointed out the trick to symlink .dotfile locations to your central toolbox so you're always using the right configurations.

I want to thank Reinout for his wonderful presentation and all the visitors of the meeting for coming to the meeting. It was nice to see everybody and to share ideas and experiences. I also want to thank BounceSpace and SendCloud for the location, the food and the drinks. I think the second meeting set a good example for the upcoming meetings and I hope that we can improve the meetings over time as well as the overall Python community in the area.