Python tips and tricks - UMKCgeg/Wiki GitHub Wiki

This page compiles some of the small tips and tricks that aren't worth of their own page.

Making a Python script executable

If you want to make an executable out of your Python script, you can. This will allow you to call yourscript.py from the command line rather than python yourscript.py. This doesn't sound like a big deal, but can be nice if you have created a self-contained tool that would be useful to run from the command line with minimum hassle for the user. Note that you also don't even need the .py extension if you make your file executable.

To do this, add this line at the very beginning of your file:

#! /usr/bin/env python

The comment is needed. This tells the machine where to look for the Python version to run the code is.

Then you'll need to change the permissions of your file to be executable. One way to do this is with chmod 755 yourscript.py from the command line. That does it! Your script is now executable.

When you have a Python script like this, there are some best practices to be aware of. Read this for more info. The recommendations here aren't absolutely necessary, but may be helpful in some circumstances.

Reading in an IDL save file

See here for the documentation of this function.

import scipy.io
idl_dict = scipy.io.readsav("file.sav")