Python Useful Commands

Install Anaconda Python 

Download anaconda. Get the latest link from

$ wget

$ bash Anaconda3* -b

Add anaconda to $PATH

$ echo "export PATH=$HOME/anaconda3/bin:\$PATH" >> ~/.bashrc

$ source ~/.bashrc


Jupyter comes with anaconda distribution. Configure jupyter

$ jupyter notebook --generate-config

Edit the vi ~/.jupyter/ This set the login password for jupyter to "pass123". 

from os.path import expanduser

c.NotebookApp.ip = ''

c.NotebookApp.notebook_dir = expanduser("~")

c.NotebookApp.open_browser = False

c.NotebookApp.password = u'sha1:298cdb938f91:e6462632083df74cb2f68d4b55b0e7a7d7e0e45b'

If you want to set your own password, generate password of your choice and replace the above password string.

$ python3 -c "from notebook.auth import passwd; print(passwd())"

Run Jupyter as service

Create bash file file that starts jupyter. Here I am using /opt/anaconda3/bin/ Configure as needed.

#!/usr/bin/env bash

/opt/anaconda3/bin/jupyter notebook

Give executable permission 

$chmod +x /opt/anaconda3/bin/

Create a service file 

$ sudo vi /etc/systemd/system/jupyter.service.

 Content of the file is a below. Update the ExecStart and User as per your requirement.


Description=Jypyter notebook service




#path to executable. 

#executable is a bash script which calls jar file







Activate the service and 

$ sudo systemctl daemon-reload

$ sudo systemctl enable jupyter.service

$ sudo systemctl start jupyter

$ sudo systemctl status jupyter

Setup log 

$ sudo journalctl --unit=jupyter

Tail the logs

$ sudo journalctl -f -u jupyter

Run jupyter notebook from terminal command

$ jupyter nbconvert --execute example.ipynb

Install Xgboost on mac

Install Homebrew

/usr/bin/ruby -e "$(curl -fsSL"

Install gcc 

brew install gcc6 --without-multilib  

Clone xgboost

git clone --recursive 

cd xgboost

Open make/ and uncomment these two lines

export CC = gcc-6

export CXX = g++-6

cp make/ .

make -j4

sudo ./

cd python-package

sudo /home/<username>/anaconda/bin/python install

Install Tensorflow

$ pip install tensorflow tensorflow-tensorboard --upgrade --force-reinstall

$ pip install protobuf==3.4.0 --upgrade --force-reinstall

Python packaging 

Pipe shell command output to python script

$ curl -s '' | python3 -c "import sys, json; print(json.load(sys.stdin)['name'])"

Run a python script as a cron job

Example of a python script - 


from urllib.request import urlopen

from boto3 import resource

from time import time

import json

# Find public ip of the current system

ip_address = json.load(urlopen(''))["origin"]

file_name = "ip_address/" + ip_address

# Create a zero byte file in S3 with the public ip as name

s3 = resource("s3")

bucket = s3.Bucket("")

bucket.put_object(Key = file_name)

Note that schebang (the first line) tells how to execute script as executable. Make changes as required. The above script runs on python3.

Give executable permission to the script

$ chmod +x

Edit crobtab - to update the details 

$ crontab -e

Add the following line to the file. This cron job runs every 15 minutes. 

*/15 * * * * /home/user01/workspace/python/

Crobjobs by default keep log in unix syslog.

View the crontask logs 

$ tail -f /var/log/syslog

Sep 13 22:55:01 einext02 CRON[11419]: (user01) CMD (/home/user01/workspace/python/

Sep 13 23:00:01 einext02 CRON[11466]: (user01) CMD (/home/user01/workspace/python/

Sep 13 23:05:01 einext02 CRON[11500]: (user01) CMD (/home/user01/workspace/python/

Sep 13 23:07:08 einext02 crontab[11515]: (user01) BEGIN EDIT (user01)

Sep 13 23:07:23 einext02 crontab[11515]: (user01) REPLACE (user01)

Sep 13 23:07:23 einext02 crontab[11515]: (user01) END EDIT (user01)

Sep 13 23:08:01 einext02 cron[998]: (user01) RELOAD (crontabs/user01)

Sep 13 23:14:50 einext02 crontab[11566]: (user01) BEGIN EDIT (user01)

Sep 13 23:15:02 einext02 CRON[11579]: (user01) CMD (/home/user01/workspace/python/

Sep 13 23:16:34 einext02 crontab[11566]: (user01) END EDIT (user01)

Python Web Framework Benchmark