Skip to content

Latest commit

 

History

History
162 lines (99 loc) · 5.52 KB

File metadata and controls

162 lines (99 loc) · 5.52 KB

Python

"Python is an interpreted high-level programming language for general-purpose programming."

Installing Python

Compared to Matlab and R, Python is fairly complicated to install and use, as it is general-purpose programming language. The situation is made more complicated by different versions (2.x vs 3.x), and different package managers.

Generally use python3.x for new projects, and only use python2.7 if a project you are using does not support Python3.

Windows installation

  • See Conda/Anaconda below.

Mac installation

Python2.7 will be installed system-wide by default on MacOS. However you should install Python3 and use that for your new projects.

  • Install Homebrew Install python3 via homebrew:
# bash mac

brew install python

Linux installation

Use the system package management system, ie aptitude or yum.

# bash ubuntu

sudo apt-get install python3-dev python3-setuptools
# bash centos

sudo yum -y install python36u python36u-pip python36u-devel

Virtualenv (Mac/Linux)

Virtualenv is the recommended approach to handling multiple installations of python on your system. Each python script or tool you use may require different versions of python, or different packages, or different versions of packages. Virtualenv is a way of seperating out these differences to resolve conflicts. Typically you would use a different virtual environment for each project you use. The following bash shell commands create a new virtual environment and then activate it for dynamic usage - all python commands entered after activate has run will use the python kernel from the virtual environment.

# bash
virtualenv ~/venv/[myenv]
source ~/venv/[myenv]/bin/activate

You can also access the virtualenv python binaries directly with:

# bash
~/venv/[myenv]/bin/python3

Pip Packages

pip is the main python package management system. It's used for installing packages into the python kernel, which can then be imported from within the scripts.

# bash
pip install numpy nPYc

Conda on Windows

On Windows, conda is the simplest package management system to use. It uses the Anaconda navigator to explore and configure environments.

The basic console

Once installed, python can be run in an interactive console mode, by simply running python from the command line. In console mode, python can be entered and executed line by line.

Example Python Console

Python can also run 'scripts', text files containing python code. Name these file *.py, and run them as follows:

# bash

python my_script.py

Command line applications can handle arguments in similar ways to linux cmd programs,

# bash

python my_script.py -a arg1 -b arg2 -c arg3

IPython QT Console

The IPython QT Console is an QT GUI version of the basic console. It supports embedded charts and interactive elements.

Example IPython QT Console

Spyder IDE

Spyder is an interactive python environment similar to R Studio and Matlab.

Example Spyder IDE

Jupyter/IPython Notebook

Jupyter Notebooks (previously called IPython Notebooks) are another widely-used interactive python environment that enable you to mix code and markdown to create, save, and share data analysis workbooks.

You are able to use Python kernels, R environments, Julia, and a wide-range of community supported kernels.

Each 'cell' contains either markdown, code, or plain text, and cells can be executed individually or sequentially.

Notebooks can be saved as json and exported and imported by other users.

Example Jupyter Notebook

Useful Packages

  • numpy - Fast numerical computation.
  • pandas - Advanced data structures.
  • jupyter-notebook - For running jupyter-notebooks.
  • scipy - Collection of packages for scientific computing.
  • scikit-learn - Machine learning in python.
  • plotly - Interactive charts.
  • nPYc Toolbox - NPC built toolbox for NMR and MS import, preprocessing, QC, and generating reports.

Using Packages

Once installed to your system-wide or virtualenv python kernel (using pip or conda), packages can be 'imported' and used in your python code.

# python

import numpy as np

a = np.arange(15).reshape(3, 5)
a.shape

# > (3,5)

Main page