<<

Pedro P. Irazoqui 6/30/2015

Python installation on Mac OS X:

1. Install Xquartz First you’ll need to install a UNIX windows manager. Mac OS X is a UNIX-based running a window manager called by default. Installing XQuartz will allow you to really take advantage of your machine and open up a world of free (e.g. Gimp for photoshop-like image processing; Octave for a free implementation of Matlab; and countless others) and paid software (e.g. Matlab for scientific computing; Cadence for microchip design, simulation, and layout; and many others). Download the XQuartz from here: http://xquartz.macosforge.org/landing/

Follow the instructions, and drag the XQuartz icon onto your . You’ll be using it a lot!

As an engineer you should really know your way around a UNIX . You can get there in an afternoon by going through this tutorial once you’ve installed XQuartz: http://linuxcommand.org/learning_the_shell.php

After you do that, you’ll want to configure both your environment and your shell to work best with python. My own xinitrc and bashrc files are located in the same directory as this document. They’re a good place to start, and you can modify from there. When they’re ready, you should put them in your home directory and prefix them with a “.” so you’ll have a .xinitrc and .bashrc files. Once you do this, you’ll no longer see them if you type “ls” at the prompt. Use “ls -a” instead.

Finally, you’ll want to settle on a text editor to write your programs. I think most people use emacs, but old-school guys like me use vi. Both are already installed on your Mac, and you can start them by typing $ emacs or $ vi at the terminal prompt. Note that the prompt here is assumed to be “$”, don’t type that, just type the part after the “$”. If you’ve used my .bashrc file above, your prompt will end in “>>”. vi is definitely a little cryptic at first, but I think it’s well worth it if you get the hang of it. That’s up to you though. If you decide you want to learn vi, go through this quick tutorial: http://www.unix-manuals.com/tutorials/vi/vi-in-10-1.html

2. vi configuration (optional) First you’ll want to install Python-mode for vi. Note: vim is a newer version of vi, when you type vi at the prompt, you’re actually running vim. Essentially vim has replaced vi, and the two terms are used interchangeably with most everyone using vim and calling it vi. The easiest way to install Python-mode is to use pathogen. At the prompt, type: $ mkdir -p ~/.vim/autoload ~/.vim/ $ curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim $ cd ~/.vim/bundle $ git clone git://github.com/klen/python-mode.git

1 of 5 Pedro P. Irazoqui 6/30/2015

Then create a .vimrc file in your home directory. You should use the vimrc file I’ve included in the same directory as this document as a template. Remember to prefix my vimrc with a “.” so it’s .vimrc, then re-start XQuartz to have it take effect. This file will customize your vi editor so that it color codes your python programs, error checks your syntax, etc… Makes for a very nice development environment. Everybody has their own preferences, and that’s part of the beauty of UNIX, so feel free to change and customize your .xinitrc, .bashrc, and .vimrc files after you have everything up and running.

3. Install Python 3.X Go to the main python.org website: http://www.python.org/getit/ And download dmg for the most recent version of Python. I used the Python 3.4.3 Mac OS X 64-bit/32-bit x86-64/i386 Installer. Your machine or operating system may differ. Please follow the installation instructions.

Now when you wish to run python type the following at the prompt $ python3

Note that this won’t work unless you’ve added the .bashrc file in step 1 above, since the command python3 is an pointing to the location of the Python 3.4.3 installation. If all this UNIX stuff is giving you a headache, don’t worry, you don’t need to understand what I just said, just make sure you follow the directions in step 1 above.

4. Install pip for Python 3.X pip is the package manager that links with PyPi to download and install packages, as well as uninstall or upgrade them down the road. $ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py $ sudo python3 get-pip.py

Now you need to add the path to your .bashrc file by adding to or editing the line at the top of that file to include at least this: export PATH="$PATH:/usr/local/bin:/Library/Frameworks/Python.framework/Versions/ 3.4/bin"

Any additional directories you want in you path should be appended to that line and separated by colons so the PATH is of this form: export PATH = “$PATH:path1:path2:path3:pathN”

After installing pip for python 3.X, restart your X Windows server and type pip3 at the prompt: $ pip3

You’ll see a list of the commands that go with pip3. Note that if you type pip rather than pip3, you’ll run the installation of pip that goes with the default Python 2.7 installation that came with

2 of 5 Pedro P. Irazoqui 6/30/2015

OSX. Installing packages with pip will install them for Python 2.7, installing them with pip3 will install them for Python 3.X. So be sure to use pip3.

You’ll find a few commands particularly useful. “list” will give you a list of the packages you’ve already installed: $ pip3 list

“show” will tell you about package_name, including the most current version number: $ pip3 show package_name

“install” will install the package named package_name: $ pip3 install package_name

“uninstall” will uninstall the package named package_name: $ pip3 uninstall package_name

The “--upgrade option” will upgrade the package named package_name: $ pip3 install package_name --upgrade

5. Install packages Now that you have pip, there are loads of packages to choose from. For starters, make sure you install NumPy, SciPy and matplotlib to allow you to do everything from signal processing to data presentation: $ pip3 install numpy $ pip3 install scipy $ pip3 install matplotlib

6. Install iPython iPython is not a required package, but it is a much nicer command-line environment to work from than the default that comes with your Python installation. Also, iPython comes with notebook which is truly .

$ pip3 install ipython

From there, you’ll want to install some basic dependencies to enable the full functionality of iPython. First install readline, for command-line editing, tab completion and other ease-of-use capabilities: $ pip3 install readline

You’ll want to install nose: $ pip3 install nose

And you’ll want to install pexpecgt $ pip3 install pexpect-u

3 of 5 Pedro P. Irazoqui 6/30/2015

You’ll definitely want to have notebook installed, which requires you to enable parallel computing by installing PyZMQ $ pip3 install pyzmq

For notebook, you’ll also need to install Tornado: $ pip3 install tornado

And finaly, you’ll also need to install Jinja: $ pip3 install jinja2

7. Learning Python Please work your way through the entire Python tutorial: http://docs.python.org/3.4/tutorial/

Now that you’ve learned the basics, you can begin to explore iPython and notebook by going through the tutorial here: http://ipython.org/ipython-doc/stable/interactive/tutorial.html iPython is what I use every time for my interactive shell. Highly recommended.

Congratulations, you are now ready for BME 301: Bioelectricity!!!

8. Extras There are a few other packages you may be interested in. Don’t feel like you have to install them all from the get go, I’m just mentioning them here so you’ll know where to begin if you’re interested.

C++: If you’re interested in writing hardware interfaces (or anything really) using Objective- and want to run those code segments in Python, install PyObjC: $ pip3 install -U pyobjc-core $ pip3 install -U pyobjc

GUIs: If you’re interested in creating graphical user interfaces (buttons, windows, etc…), you can choose from Tkinter, PyQT, and wxPython among others. Most of what I’ve read recommends PyQT. Head on over to: http://qt-project.org/downloads and download the installer (I used the offline one). Mount the dmg (by double clicking it), and launch the Qt installer. This will install Qt in your home directory. If you wish to run the QT Creator app, it won’t be in your applications folder, instead: $ cd ~/Qt5.2.0 $ open Qt\ Creator.app/ Before going further, add the Qt bin directory to the PATH in your .bashrc: PATH=$PATH:/Users/your_home_dir_name/Qt5.2.0/5.2.0/clang_64/bin

4 of 5 Pedro P. Irazoqui 6/30/2015

Now that you have Qt installed, get to know the software (note: you can launch Qt Designer from within Qt Creator as shown on this link): http://qt-project.org/doc/qt-5/designer-to-know.html Then, go through a tutorial: http://qt-project.org/doc/qt-5/designer-quick-start.html And finally, you can explore the manual: http://qt-project.org/doc/qt-5/qtdesigner-manual.html

After you’ve installed the Qt libraries and modified the PATH in your .bashrc (remember to restart XQuartz so the changes take effect), you’ll need to install SIP. Go to: http://www.riverbankcomputing.com/software/sip/download and download and unzip SIP. From the terminal prompt, cd into the directory you just unzipped: $ cd sip-4.15.4 Configure SIP for installation: $ python3 configure.py Compile: $ make Install: $ make install Once this is done, you may delete the sip-4.15.4 directory you ran these commands in as the binary files have been placed where they belong.

Now you have SIP installed, it’s time to install PyQt. Go to: http://www.riverbankcomputing.com/software/pyqt/download5 and download and unzip PyQt. From the terminal prompt, cd into the directory you just unzipped: $ cd PyQt-gpl-5.2 Configure PyQt5 for installation $ python3 configure.py Compile: $ make Install: $ make install Once this is done, you may delete the PyQt-gpl-5.2 directory. To get started, go through this quick tutorial: http://www.thehackeruniversity.com/2014/01/23/pyqt5-beginner-tutorial/

Standalone applications: If you wish to deploy standalone applications, have a look at PyInstaller: http://www.pyinstaller.org

5 of 5