"Python is an interpreted high-level programming language for general-purpose programming."
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.
- See Conda/Anaconda below.
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
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 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 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
On Windows, conda is the simplest package management system to use. It uses the Anaconda navigator to explore and configure environments.
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.
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
The IPython QT Console is an QT GUI version of the basic console. It supports embedded charts and interactive elements.
Spyder is an interactive python environment similar to R Studio and Matlab.
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.
- 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.
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)