I always thought of Python as a language mostly useful for doing stuff on Linux machines. You know, like a pretty nice little language for scripting tasks but not useful for doing advanced stuff.
Well, I have changed my mind…This past year I have had the pleasure of working a lot in Python and I have come to like it very much. Python is the bomb!
So what I have done with it? Well…
- I have built massively scalable cloud services running on Google App Engine.
- I have built command line tools for intelligently packing sprite sheets, extracting font information, performing custom encryption, modifying XCode projects and more…
- I even started working on a cross platform Git client application…
…and I must say that I have really enjoyed it very much.
Python has some really nice benefits:
- The code is easy to read – very clean and compact
- It is cross platform
- Compiles to exe
- Has lots and lots of third party libraries to do anything
- Good integration with C
- With Cython you can transform Python to fast performing C code
- Interactive console (easy to try things out)
- Large community
- It is wrist friendly – no curly bracket typing 🙂
But is it really useful for any serious development? Well, you tell me! Many well known applications and services are written in Python such as GMail, Google Maps, Dropbox, Spotify and Mercurial to name a few…
But…can I really use it on Windows? Python originated in the Unix world and must therefore not work properly on Windows, right? Hmmm, I think this is the biggest hurdle for Python acceptance. Many people thinks it is hard to use on Windows and yes…that was my notion too until I tried it. As a matter of fact it is easy to install and it works really well. Let’s take a look!
How to get started – Python essentials
Before getting started, make sure you have Chocolatey – the lovely package manager for Windows.
I believe there are three essential things you need to have a basic Python setup on your system:
- Python itself
- A Python package installer
- Virtualenv for Python
The first two are the most important so I will leave the last out one for a future post.
With Chocolatey it is very easy to install the latest version of Python:
C:> cinst python.x86
This will install the 32 bit version which I recommend for now.
UPDATE: This command will install the latest version which is now Python 3.4. If you want to run an older version you can supply a version argument like this:
C:> cinst python.x86 -Version 2.7.6
This installs a specific version of Python.
Once this is done you can actually start writing code using any text editor you like. Notepad works but it is nice to have some syntax highlighting and I recommend either Sublime Text or Notepad++. Even better is to use a full featured IDE such as PyCharm which I use. It has more advanced features that makes your life easier!
Once you start working with Python you realize that you sometimes need 3rd party packages. To install them you need the Python package manager PIP. You can install PIP using easy_install and easy_install can be installed with Chocolatey. Isn’t that lovely? 🙂
C:> cinst easy.install
After you have run this, close the command prompt window and open a new one to ensure that the environment variables are reloaded. Then run this:
C:> easy_install pip
When this is complete you are all set to go. You can now run Python stuff directly from the command prompt. You can even install Python scripts using pip if you just want to get some nice tools written in Python in this way.
What about virtualenv?
I told you there are three essentials you need on your Python system. The last thing is virtualenv. All packages you install with PIP will be installed globally unless you use virtualenv. For now, that is ok. In a future post I will explain how to use virtualenv to setup isolated development environments.
To show a little example of how you can use Python, open up a text editor and paste in the following Python code. This short snippet will calculate the SHA-1 hashes of all files in the directory that you are in.
import hashlib, os
for f in os.listdir('.'):
print hashlib.sha1(open(f, 'rb').read()).hexdigest(), f
Save it out as hash.py and run it (from the command prompt) and it will show something like this:
That’s quite nice for only four lines of code. Python is often like that – few lines of code and high productivity. I like that.
For some reason Python is not very popular on Windows. I hope this will change in the future. If you have never tried it – give it a spin. You may like it!