Skip to content
Basic stuff for Python programming
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
img
Colab.md
README.md
conda.md

README.md

Brief summary of

  1. key concepts and tips for Python programming
  2. how to set up Python environment
  3. Jupyter notebooks

1. Learning Python language

Think Python 2nd Edition

Allen Downey's excellent book on Python is strongly recommended. You can see a free pdf from here. Code is here

More on Python 3

There are many good tutorials on the web. I found a tuturial in https://www.python-course.eu/python3_course.php very useful for it's simple explanations and numerous figures.

From the tutorial, let's learn key concepts and practice them. The following links are what, I believe, you should know to get started.

basic

data structure

flow control

function

modules

2. Setting up Python environment

python 2 vs 3

Python 2.7 used to be very popular for scientific computing for a long time. But now, it's safe to switch to Python 3 (3.7 is the latest). So, if you are new to Python, start from Python 3.

If both python 2 and 3 are installed on your machine, you can (should) run python 3 by typing python3 instead of python.

python3

You can check the version of the python as follows.

python --version

On Ubuntu 16.04.3, it will be Python 2.7.12.

python3 --version

On Ubuntu 16.04.3, it will be Python 3.5.2.

For OSX users

You can install python 3 (and all the other open-source packages) using Mac Port.

  1. Install Mac Port https://www.macports.org/install.php
  2. Install pythone 3.
sudo port install python37

As of Apr. 2018, python 3.6 is the latest. Using the following command, you call it by python3.

sudo port select --set python3 python37
  1. Install pip for pythone 3
sudo port install py37-pip

Next, you can select pip for python3.

sudo port select --set pip pip37

This command will make symoblic links in /opt/local/bin/.

MP3:~ yyoo$ ls -al /opt/local/bin/pip*
lrwxr-xr-x  1 root  wheel  67 Apr  4 23:10 /opt/local/bin/pip -> /opt/local/Library/Frameworks/Python.framework/Versions/3.6/bin/pip
lrwxr-xr-x  1 root  admin  67 Mar 29 07:27 /opt/local/bin/pip-3.6 -> /opt/local/Library/Frameworks/Python.framework/Versions/3.6/bin/pip

(Optional) I found it useful to change the name pip to pip3 not to be confusde with the sytem default python 2.7. I recommend to do this.

sudo mv /opt/local/bin/pip /opt/local/bin/pip3

Also, you can keep MacPort updated using the following commands.

sudo port -v selfupdate
sudo port upgrade outdated

See https://www.macports.org for more details.

virtual environment

In Python 3 (from 3.3), venv is the standard way for creating and using virtual environments.

You can create a new virtual environment using

python3 -m venv [PATH-TO-NEW-VENV]

and activate it using

source [PATH-TO-NEW-VENV]/bin/activate

When you want to leave the current virtual environment, deactivate it.

deactivate

See https://docs.python.org/3/library/venv.html for more information.

Installing packages using pipin an virtual environment (RECOMMENDED)

pip

You can use 'pip' to install python modules.

Here, I assume that you installed pip for python3 and it's name is pip. You can do it by

sudo apt install python3-pip

See https://docs.python.org/3/installing/index.html?highlight=pip for more information.

It is quite tricky where the actual modules and librararies are installed. In the virtual environment, don't worry about it and just use pip without any options. (This is another reason why virtual environment is useful and recommended.)

Example. Install tensorflow without GPU support

For example, you can install latest tensorflow as follows.

pip3 install --upgrade tensorflow

Example. Install tensorflow with GPU support

For example, you can install latest tensorflow as follows.

pip3 install --upgrade tensorflow-gpu

Add the following three lines to ~/.bashrc.

export PATH=$PATH:/usr/local/cuda/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64
export LD_INCLUDE_PATH=$LD_INCLUDE_PATH:/usr/local/cuda/include

Log out your account and log in again.

You should be able to import tensorflow in your python.

Install a package without an virtual environment (NOT RECOMMENDED)

If you want to install some module for syetem-wide access, then you could do it in two ways.

  1. If you have the admnistrator privilege, you can do something like this.
sudo pip3 install --upgrade tensorflow

But, you will face some warning that says ....

  1. If you are not an admnistrator, you must install the modules under your home folder with --user option.
pip3 install --user --upgrade tensorflow

3. Jupyter notebook

Jupyter notebook (previously known as Ipython notebook) is a great tool for interactive data analysis.

What is Jupyter?

Jupyter is an interactive and integrated interface for scientific computing. Using Jupyter, you can put code, text, plots and even equations in a single document, called notebook (*.ipynb). A notebook looks like this. Example of notebook

Other useful features include:

  • You can launch your notebook from (any) web browser on your local machine while real computing is performed on either your own machine or on a remote server.
  • You can run block-by-block by typing Shift+Enter! This is very useful for debugging.

Another good source is this book Learning Ipython for Interactive Computing and Data Visualization (2nd ed.).

How to install?

The easest way to install Ipython is to use pip as follows.

pip3 install jupyter

How to use it?

You have two options to run and edit Jupyter notebooks.

on local machine

  1. Open an Terminal
  2. Go to the folder that contains your notebooks
  3. Launch the Jupyter kernel by typing the following command.
jupyter notebook
  1. Open (any) web browser and go to localhost:8888 (Step 3 will usually start Step 4 automatically.)
  2. In the web browser, you can run each block by Shift+Enter.

Launch Jupyter over network

Jupyter is also very useful to launch the actual computations on a server and control it through a web-brower on your local machine. You're going to use two terminals, one for SSL tunneling and the other for launching the Jupyter on a remote server.

  1. In a terminal, log in to the server and launch Jupyter notebook.
ssh [ID]@[SERVER ADDRESS]

[GO TO THE FOLDER YOU'RE WORKING ON] 

jupyter notebook --no-browser
  1. In a new terminal, establish a SSH tunnel between your local machine and the remote server.
ssh -N -L localhost:8888:localhost:8888 [ID]@[SERVER ADDRESS]

Type your password and keep this window opened.

  1. In a web browse and open the address and port (default is 8888) provided by the notebook.

See here for more information.

ETC.

Matplotlib issue on Mac OSX (link)

Add the following line to ~/.matplotlib/matplotlibrc

backend: MacOSX
You can’t perform that action at this time.