Install Python Package for User

If you do not have root privilege and want to install a python module, you can try the following approach:   python setup.py install –user This will install packages into subdirectories of site.USER_BASE. To check what is the value of site.USER_BASE, use:   import site print site.USER_BASE reference: https://docs.python.org/2/install/   Update 2018/01/06: using pip to …

Right way to put test codes in a Python project

I’ve been struggled about where to put test files in a python project for a long time. Ideally, I think it is succinct to create a folder called “test” with all test files in it. However, the test files nested in the test folder need to import modules from parent folder. It is troublesome to import Python module …

Jupyter Parallelism Tutorial

In this post, I am going to introduce my favorite way to make cells in Jupyter notebook run in parallel. 1. Initialize cluster using command lines or use Python `popen` (In the example below, I create a cluster with 2 workers): from subprocess import Popen p = Popen([‘ipcluster’, ‘start’, ‘-n’, ‘2’]) 2. Then, programmatically set …

Add permanent key bindings for Jupyter Notebook

This post shows how to add customized permanent key bindings for jupyter notebook. 1. check the location of your jupyter config folder using the command: sudo ~/.local/bin/jupyter –config-dir I am running Ubuntu. The config folder, by default is, `/home/your_user_name/.jupyter` 2. Create a folder `custom` in the config folder.  3. Create a file `custom.js` in the …

Use PDB to check variables before crashes

1. Use `python -i your_script.py` to execute your program with interactive mode. This means, after your program finishes executing, or your program crashes in the midway, you will enter a python shell. 2. Suppose your script has a bug so that you enter the python shell after it crashes. Now you can play with pdb …

Python multiprocessing map function with shared memory object as additional parameter

Suppose you want to create a bunch of processes to consume a list of elements: import multiprocessing def consume(ele): return ele ** 2 if __name__ == ‘__main__’: m_list = [9,8,7,6,5] pool = multiprocessing.Pool(processes=300) result = pool.map(consume, m_list) print result   Now, if you want to have a dictionary to be shared across processes, and pass …