From BarikWiki
Jump to: navigation, search


Python for Scientific Computing

Python for Games

Installation Walkthrough

While not a requirement, cURL (Windows download) simplifies the downloading processing by allowing you to use the command line. Installing the version without SSL is easiest; otherwise OpenSSL (Windows download) is required.

  • First, a Python public service announcement.
  • Install the appropriate version of Python (usually Python 2.7), since quite a few important frameworks still do not support Python 3 (namely, Twisted).
  • Previously, you could install setuptools. This would give you easy_install. The preferred way seems to be distribute now. Note that easy_tools is installed under the Python Scripts folder.
  • Next, install virtualenv and pip. By the way, installing virtualenv gives you pip automatically, and virtualenv is supported with PyCharm! Also, pip can generally be used to install things, even if the instructions say to use easy_install. For example: pip install zope.interface.
  • This is all still very confusing, so see the The Hitchhiker’s Guide to Packaging.
curl -O


  • JetBrains PyCharm has become a favorite editor of mine for working with Python.
  • PyCharm Resources, which includes a quick reference guide.



The IDLE shell has the ability to recall the command history. Use ALT + p for the previous command and ALT + n for the next command. Annoyingly, up and down do not work because these keys are reserved for cursor motion.


Unfortunately, IronPython doesn't even seem to be able run "Hello World" at this time, so give up on it for now; this system is garbage. Here's the full error log on IronPython. You can run the program through the IronPython Interactive (ALT + SHIFT + F5) though, which is a bit odd.


def fact(n):
    if n == 0:
        return 1
        return n * fact(n - 1)

Iterating over Standard Input

This is a common technique in algorithm competitions:

for line in iter(input, "-"):
  print line

The above code will keep processing lines until a dash (-) is received.

Timing Code

In algorithm competitions, perform is a huge factor, as it can make the difference between meeting and exceeding the problem time limit. The timeit module can help with this, though its usage is not at all intuitive:

import timeit

t = timeit.Timer("f(3)", "from __main__ import f")

The use of from __main__ import f is important! If no argument is passed to timeit, then the number of iterations is a million.


Python Wackiness

Python itself has some oddities when you start delving under the hood. Decorators? repr? Python functions with attributes?

  • The different import mechanisms get confusing to me.

Running Processes within Python

  • No one knows how to use the -c cmd argument in Python. Because it isn't nearly as nice as perl -e, so you'll have to do a bunch of escaping. Example of a simple print: python -c "print \"hello\"".
  • In Windows, you may want to run a process in the background and allow Python to exit without waiting for your process to finish. To do this, you should see this (also couples as a nice injection attack here):

line = sys.stdin.readline()
pid = subprocess.Popen([line], shell=True,

You can also use the Windows-specific startfile, which is far less arcane.

32 bit or 64 bit?

  • I couldn't find an easy way to tell what version of Python (32-bit or 64-bit) that I was actually running until I found this thread.

Some Oddities

  • takes no keyword arguments. This might actually be a function call to a pyd file, rather than a regular Python script.
  • Python provides heapq, without instructions on how to use it. The mystery of how __cmp__ is supposed to work.


Some commonly used Python packages on Windows:


Twisted is an event-driven networking engine written in Python and licensed under the open source.

If pip has not been configured, then Twisted can still be installed under Windows:

  • Download the 32-bit version of Twisted (Twisted for Python 2.7).
  • You will also need to install setuptools if easy_install does not exist. easy_install should be found under c:\Python27\Scripts\easy_install.exe. (I'm curious as to how I obtained easy_install in the first place, since I don't have setuptools. Aha -- it's because I've installed distribute).
  • Twisted requires the use of zope.interface, which can be downloaded as an egg file.
  • You can install the egg file with: C:\Python27\Scripts\easy_install.exe C:\zope.interface-4.0.2-py2.7-win32.egg.

Compiling Python

  • configure; make. But when installing to the home directory: make altinstall prefix=/home/tbarik exec_prefix=/home/barik.
  • ./configure CFLAGS="-I/home/tbarik/include" LDFLAGS="-L/home/tbarik/lib"