Lightly Seared On The Reality Grill

Random expat geekery from The Low Countries

Browsing Posts tagged Python

In what I increasingly laughingly refer to as my spare time, I am taking another crack at improving my rather rudimentary Python skills. As a consequence of this, I have found myself browsing the Python PEPs.

PEP 20 – The Zen of Python captures the guiding principles for Python’s design into 20 aphorisms, only 19 of which have been written down. The 19 of which that have been written down are so true, and so true of any language, that I am repeating them here as a reminder to myself and anyone else that might pass this way.

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

flattr this!

There are plenty of Linux resources on the internet but far fewer (or none, as far as I could see) that provide a simple set of steps for getting a Python application up and running on your PC. So, as much for my own reference as for anyone else’s benefit, this is what I’ve come up with.

Firstly, though, a disclaimer. The steps described below work but that is no guarantee that they represent the correct approach. If anyone does have any corrections, suggestions or improvements, all feedback is gratefully accepted.

And another disclaimer. Always check the repositories for your Linux distribution first. If the application can be installed using your package manager, then that is what you should do.

However, there are sometimes cases in which you have either commuted an act of Python yourself or the application you want isn’t yet available and you want it enough to resort to downloading tarballs.

A tarball file is an archive of files in a single file and will have an extension of .tar.gz, .tgz or .tar.bz2. Since this post is specifically about Python applications, it is safe to assume that we are talking about an archive of source files. I am going to further assume that you have downloaded the tarball to your Home folder.

So the first thing you will need to do is uncompress the tarball. Depending on the extension, you will need to use one of the following commands:

$ tar zxf ~/shinyapp.tar.gz
$ tar zxf ~/shinyapp.tgz
$ tar jxf ~/shinyapp.tar.bz2
$ tar jxf ~/shinyapp.tbz2

You will, of course, need to replace the text shinyapp with the actual name of your downloaded tarball. I will continue to use the term shinyapp for the rest of this post – replace as appropriate.

Because Python is an interpreted language, there is no need to compile it. But you will need to do a bit of set-up to ensure you can execute it as easily as everything else. To do this, you need to be root.

First of all, move your uncompressed folder to the /usr/local/src directory:

# mv ~/shinyapp /usr/local/src

Next you need a Bash script to launch the application. The location of this script matters and you can establish what it should be with the following command

$ echo $PATH

This will return a colon delimited list of folders. These are the places that your distro expects to find Bash scripts – putting it anywhere else isn’t going to work.

The folder /usr/local/bin/ is generally a sensible place to put your local scripts.

To create the script file using the Nano text editor:

# nano /usr/local/bin/shinyapp

And then, in Nano, edit your empty file so it looks something like this:

#! /bin/bash
python /usr/local/src/shinyapp/shinyapp.py

Return to the command line and make your script executable..

# chmod +x /usr/local/bin/shinyapp

Congratulations. You have just created a command. You can execute this command from within a terminal or by pressing Alt-F2 (in Gnome), you can also – and more usefully – either create a launcher or add it to a menu.

flattr this!

Python is a remarkably effective scripting language – it’s easy to read and understand yet surprisingly powerful. As such, it is something I often turn to when I need to knock together a quick script on my home PC.

It runs on most platforms including, it turns out, the IBM i. iSeriesPython was ported to the IBM i by Per Gummedal.

Now I just need to find an excuse to install it.

flattr this!