Python

From CCRMA Wiki
Jump to: navigation, search

Why Python?

Lets face it, Matlab is a great tool for matrix manipulation (e.g. DSP), but is crazy expensive (which is why is not the standard in the industry) and also is not the greatest when you want to do something outside matrix manipulations. Sure, you can do GUIs, connect to DBs and parse text, for example, but all these tasks (and many others) are not particularly well integrated. Furthermore, although OO has been added to Matlab, is clear that it wasn't designed with OOP in mind. All this makes Matlab a good prototyping tool, but whatever code you generate will probably be far from production.

Python, on the other hand is a general purpose language, which was designed for readability and flexibility. Is also free! And, thanks the numerous packages (aka modules) such as NumPy and Matplotlib, allows to do most of what you usually do with Matlab.

Luckily, if you want to work with Python, the CCRMA workstations have it installed for you, along the afore mentioned NumPy (and SciPy), Matplotlib, Nose and other packages. When creating this page, the installed version of Python is 2.7, which is the standard (there's currently a version 3, but is still not widely adopted, as many packages haven been ported to it yet).


Need to install a package?

Although most of the most common packages are installed, sometimes you'll need to use one that's not installed. If you think it could be beneficial to the community to installed system-wide, the pleas contact the sysadmins to request the installation. But probably, you'll use it only for a single project and only you care about it. The preferred solution (there are others) is to use virtualenv.

Basically, virtualenv allows you to create a virtual and self contained python environment (you can even use a different Python version from the one installed in the system!). In this environment, you can install all the dependencies required, without needing admin privileges.

To create a virtual environment simply type:

virtualenv <folder>

or

virtualenv --system-site-packages <folder>

The former will create an empty environment (just Python and the PIP package manager, but none of the packages existing in the system), while the later will also include the system packages inside the virtual environment.

Once the environment is created, cd into it and type

source bin/activate

to start the virtual environment---your prompt will now start with (<folder>). From then on, you can use it as any other python environment you have used. As mentioned, the virtual environment also installs the pip package manager, so if the package you need to install is included in pip (e.g. theano), you can install it by typing

pip install theano

Once you are done, you should type deactivate to quit the virtualenv.