Live Fast, Code Hard, Die Young

Posts tagged ‘chocolatey’

Python rocks – on Windows too

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:

  1. Python itself
  2. A Python package installer
  3. Virtualenv for Python

The first two are the most important so I will leave the last out one for a future post.

Installing Python
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!

Installing PIP
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.

Using Python

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('.'):
  if os.path.isfile(f):
    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:

27286076704f65d178baa46e66232a9e20d7e3dc diskstation-dir.bat
b8f7f52219520b64cac9c8ad49f533ed40cbbac8 fix-vs.py
6ebe48a8f124447600cb2bcf4135b5586933d0d2 import-itunes.bat
200b0b05edd6638aa43f1281f7a5f77bb2517303 killsteam.cmd
c86b8442e1e0561ca1715874a9d8fddda4a0d965 md5hash.py

 

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!

 

Advertisement

Chocolatey delicious package manager goodness

As a long time Windows user I have always found it nice and easy to use UI’s to perform various tasks. However, some things are actually quite a bit nicer to do from the command line. One such thing is installing programs. Installation is mostly a tedious procedure where you have to go and find an installer to download, run the installer and carefully select not to install any crap with the software etc. It is a series of steps that differ from program to program and they are not especially fun – just something you have to do.

So what can we do about this? Well, we can use Chocolatey!

With Chocolatey all program installation become streamlined into a single command line operation. This means you have to think less about what to do, just look up the package you need and type it in. Package names are also easy to remember or maybe put in a text file so you have them when you need to re-install or upgrade apps. It’s very nice.

Chocolatey has been around for a few years and I actually tried it when it was released but found it to be a bit rough on the edges back then. Now it works much better! And the amount of packages available has also gone up considerably.

Installing Chocolatey

It is super easy to install Chocolatey. Just open up a command prompt (hit Windows+R, type cmd and press Enter) and then paste in the following:

C:\> @powershell -NoProfile -ExecutionPolicy unrestricted 
-Command "iex ((new-object net.webclient)
.DownloadString('https://chocolatey.org/install.ps1'))" && 
SET PATH=%PATH%;%systemdrive%\chocolatey\bin

 

Yeah, this is a mouthful of stuff. What it does is to run a Powershell command that downloads and runs a script that installs Chocolatey. This could have been done with a regular installer program but this is a way to promote installation via the command prompt to make it easy to get going.

Using Chocolatey

Once you have installed Chocolatey you can go ahead and install programs from the command prompt by simply typing cinst:

C:\> cinst vlc

 

This command will install the latest VLC for you in one simple step (you need to click Yes in the UAC prompt though). This works even if you already have VLC installed. Go ahead and try it now!

Just for fun I recorded a video of the installation procedure just to show you how easy it is to set it up.

 

So, there you have it – package management for Windows. With my Mac I use Homebrew and it is lovely and I’m really happy to see the same type of tool coming to Windows too. If you haven’t tried it yet – now is the time! 🙂