Python - gher-uliege/Documentation GitHub Wiki

Documentation

Web

Learning platforms

Youtube

Books

  • Learn Python the hard way, Z.A. Shaw, 2013
  • Learning Python, 5th Edition, M. Lutz, 2013
  • Python Programming: An Introduction to Computer Science, J.M. Zelle, 2002

Complete list: https://wiki.python.org/moin/PythonBooks

Installation

General python

Depends mainly on the Operating System and on the user ability.

Hard way

Download source and compile: https://www.python.org/downloads/

For Python 3.6.0:

./configure --enable-optimizations
make 
make test
sudo make install

Note that the make step can take a while.

Normal way

Use installer or package manager

python2.x and python2.x-dev packages

Easy way

Python distributions such as:

among other possibilities.

Modules

Example: SciPy: mathematics, science, and engineering

Easy way

Windows, Linux, Mac: use Scientific Python distribution

Intermediate

Harder

Build from source

python setup.py install

General

  • Use pip tool whenever possible
  • Use virtualenv (virtual environment) to deal with different versions of packages
  • Avoid mixing installation methods within a given machine

Using pip to manage modules

pip = recommended tool for installing Python packages

Installation

python get-pip.py

Usage

See the full documentation for pip. A few basic examples:

  • Install latest version + dependencies:

pip install PackageName

  • Specify exact version:

pip install PackageName==x.y.z

  • Specify minimum version:

pip install 'Package>=x.y.z'

  • Uninstall packages:

pip uninstall

  • List installed packages:

pip list

Example of output:

aptoncd (0.1.98-bzr117-1.2)
...
xhtml2pdf (0.0.6)
zope.interface (3.6.1)
  • Output installed packages in requirements format:

pip freeze

Example of output:

aptoncd===0.1.98-bzr117-1.2
...
zope.interface==3.6.1
  • Show information about installed packages:

pip show numpy

will return something like:

---`
Metadata-Version: 2.0
Name: numpy
Version: 1.11.1
Summary: NumPy: array processing for numbers, strings, records, and objects.
Home-page: `[`http://www.numpy.org`](http://www.numpy.org)

Useful modules

Science and numeric

  • numpy: scientific computing,
  • scipy: scientific and technical computing.

Data analysis

  • pandas: data structures and data analysis tools.

Graphical User Interface

  • Tkinter

Plotting

For notebooks

  • ndime: Notebook Diff and Merge tools

Running python code

Bash

Edit, then run in a shell:

python mycode.py

or if the shebang #!/usr/bin/python is present at the 1st line

mycode.py

Interactive python

ipython provides auto-completion, exploring objects, ... And the so-called magic functions:

  • %run: Run the named file inside IPython as a program
  • %timeit: Time execution of a Python statement or expression
  • %who: Print all interactive variables, with some minimal formatting

Integrated Development Environments (IDE)

Editor + build automation tools + debugger

Complete list: https://wiki.python.org/moin/PythonEditors

Virtual environments (venv)

Goal: to keep the dependencies required by different projects in separate places:
http://docs.python-guide.org/en/latest/dev/virtualenvs/

Installation

See https://virtualenvwrapper.readthedocs.io/en/latest/install.html for details

  • Install modules
pip install virtualenv
pip install virtualenvwrapper
  • Create a directory where the environments will be stored

mkdir ~/Envs

  • Assign the variable WORKON_HOME, PROJECT_HOME and source virtualenvwrapper.sh
export WORKON_HOME=/home/ctroupin/Software/Python/Envs
export PROJECT_HOME=/home/ctroupin/Projects/Python
source /usr/local/bin/virtualenvwrapper.sh

These 3 lines can be added in the .bashrc file.

Removal

If you have virtualenvwrapper installed:

rmvirtualenv

otherwise remove recursively the folder containing the environment.

venv usage

  • First, create a new environment

mkvirtualenv Diva

You know that you are inside the environment because of the (Diva) written before your username:

(Diva) ctroupin@gher13 ~ $

  • To close the session:

deactivate

  • To work on a given environment, no need to create it, just type:

workon Diva

venv with different python versions

One can specify the python version using the -p flag of mkvirtualenv, for example:

mkvirtualenv -p python3.6 Diva-python3.6

Configure matplotlib with virtualenv

The file matplotlibrc can be edited in order to customize the plots:
http://matplotlib.org/users/customizing.html

To find its location:

import matplotlib
matplotlib.matplotlib_fname()

If you work with a virtual environment, there is an additional file located in

${ENVDIR}/lib/python3.6/site-packages/matplotlib/mpl-data

where ENVDIR is the path to the main directory of the environment.

Jupyter notebooks

Web application that allows you to create and share documents that contain live code, equations, visualizations and explanatory text.

Troubleshooting

Install QT4 with python 3

Error message:

...
from PyQt4 import QtCore, QtGui
ModuleNotFoundError: No module named 'PyQt4'

Follow instructions from:\
<http://stackoverflow.com/questions/7942887/how-to-configure-pyqt4-for-python-3-in-ubuntu>

Install mpl\_toolkits in virtualenv
-----------------------------------

Sometimes it is not possible to install via pip, in these cases one has
to resort to other methods.\
<https://gist.github.com/yosemitebandit/91b4143ffad899aebef8>

Other Python tools
==================

[Django](Django "wikilink")
---------------------------