Python tips & tricks

7 minute read

Today I'm going to explain some of the lessons and tools I've found that could be useful for someone developing data science (and also general purpose projects) in Python:

  • Ipython
  • Notebooks
  • Tricks for python in a terminal

For the readers already working in Python, probably some of these items are already known. But I love shortcuts and ways to save time, so maybe you can still find something useful in what follows.

IPython

An improved version of the typical python shell. Actually the "i" comes from "interactive", but I would rather say that it's more an improved version.

Installing ipython is as easy as any other package in python:

pip install ipython

In case you are in Ubuntu, you can also run

sudo apt install python-ipython

Hey, it's not a mistake. , since last versions, apt-get has been simplified to just apt. The advantages of ipython w.r.t. classical python shell are many, let's enumerate some:

  • As in a unix terminal, you can autocomplete and see suggestions using the tab command. Something really appreciated when introducing filenames, names of variables, etc.

  • Using the alt key, ipython allows to move the cursor word by word. This feature is not available (at least in my Mac) when using the classical python shell

  • More explicit and colour-highlighted error messages

  • Basic UNIX shell integration (you can run simple shell commands such as cp, ls, rm, cp, etc. directly from the IPython command line)

  • Help to commands directly from terminal, e.g:

Profiles

Have you ever been programming in your favorite IDE and thought: "Is numpy/pandas accepting this? I'm gonna try in a terminal a toy example, just to be sure". Then you go to a terminal, write [i]python and you have to write, for 334th time in a week, the famous:

import numpy as np
import pandas as pd

Isn't it possible to load them automatically? The answer is ipython profiles. So you can create a profile with your preferred libraries, and automatically import them from start. Step by step:

  1. Create a profile with: ipython profile create name_profile

  1. Modify the profile config file, e.g. with vim (see image above for location), as follows:

  1. Launch ipython using the profile with: ipython –profile=profile_name.

You can see in the first image how pd and np are understood without the need of importing. Notice that WordPress render the double dash as one large dash, so in this case there is a double "-" before profile, and similar for other cases below.

Ok, so once you have the profile created you can save the time to import those packages that you're always using at the cost of using –profile=profile_name after ipython. Not bad… but can this be ever more simplified? There are two options:

  1. Modify the default profile, found in the same directory as the profiles we create
  2. Create an alias, see last section in this post

Notebooks

Jupyter notebooks are really powerful environments where you can develop applications not only in Python, but also other programming languages such as R. They are a complete world, and I'm not gonna explain the entire list of features they have. See an example here. As you can see they might be useful to present work to other people, but also to have a more dynamic environment where you can run just some pieces of the code, so standing as an intermediate player between the terminal an running code in IDEs.

Installing the notebook feature is as easy as

pip install jupyter

and you can run it by

jupyter notebook

This will open a tab in your browser, and you'll be able to work in it. If you have conda installed, this same thing can be installed with

conda install notebook

I love keyboard shortcuts (more in the next section), and notebooks have many. The ones I more often use are:

  • intro (when a cell is selected): enter edit mode
  • esc (when editing a cell): exit edit mode
  • control+intro: execute current cell
  • shift+intro: execute current cell, and move to next cell
  • d twice: remove current cell
  • z: undo deletion
  • a/b: insert cell above/below
  • h: show help for other shortcuts

Another feature I like is notebook themes. Some of us don't like to code in a black on white schema (white background, black fonts) , though there is a lot of controversy about this. To be honest, before writing this post I always thought that it was healthier for my eyes, but it turns out that it depends on the environment light, and also everyone eyesight. In any case, if you feel better or at least the same with dark themes, you can do your bit and saving battery and energy, which is both good for your pocket and your planet. Instructions can be found at the link.

Python in a terminal

I highly recommend working in an Integrated Development Environment (IDE) to develop code and use Version Control System (VCS). My favorites are Pycharm and Git, respectively. They are free, popular, and enough for almost any task. However, in some situations we prefer/have to work in a python shell. Here I give you some tips and tricks to improve your experience in that situation.

The first resource I'm gonna share has worked for me in MacOS and Ubuntu, and I think it should do for all Unix-based systems as well. The idea is to save time when launching the python shell by the use of alias. To do so, edit the bash config file with

sudo vim ~/.bash_profile

and introduce your own aliases. Depending on your system you should edit the .bashrc file in the system location. Here some examples of aliases I currently use:

alias i=ipython
alias id=ipython –profile=dscience

alias notebook="ipython notebook –notebook-dir=~/Dropbox/PycharmProjects/notebooks >/dev/null 2>&1 &"

Remember that when editing the bash config files, you must source them, or close and open a new terminal to reload the config.

These alias just create shortcuts to save time, e.g:

Specially interesting is the last one, with which you can automate the directory opened for notebooks, and also the terminal can still be used while the notebook is running. Notice however that if you close the terminal, the notebook system is gonna break down, so take care. Also take care when copying the command, you probably will have to rewrite the double quotes (blogging issues...), so it's: (double-dash)notebook(dash)dir. Apart from this, it should work both in Mac and Linux.

Another interesting resource I want to share is using a better terminal client than the one natively provided. In Mac I use iTerm2, whereas in Ubuntu one might use Terminator. In addition to some better color scheme, the main advantage for me is that you can split the window into two terminals, and move from one to the other with control+tab, or the shortcut of your choice. This is specially useful when building client-server applications, where you need two terminals at the same time.

Finally, some shortcuts useful when running ipython in a unix terminal. I'm not gonna be rigorous with the terminology, just explain what they do in plain English:

  • control+c: kills the current process. When you are in Python terminal, it's useful to delete the current line, so saving time specially if it's long.

  • control+d: when pressed in Python terminal, you are asked to type yes or no to confirm exiting. The default is yes, so if you press intro the python shell ends. This saves you some (mili)seconds at the end of a week by pressing control+d+enter.

  • control+z: send to sleep (background) current process. This can be useful if you want to try something in terminal without losing your workspace in Python. Or if you want to work with two different python environments at the same time, since typing Python will start a new and completely independent environment. To return to the last slept process, run fg (foreground). A list of the current processes in a terminal can be obtained by running jobs. More info about this here.

  • I love using *home* and *end* buttons, but in my Mac I don't have them, and first days I was really disappointed. In some applications, such as in the browser, you can move cursor to beginning and end of line by pressing cmd+left/right cursor, but it doesn't work in the terminal. In such a case, the default shortcut is control+a and control+e. This works for the ipython shell as well.

Conclusion

Today we have reviewed some tips and tricks for working with Python in a more agile way.  There are many many other things that I could recommend, but they'll probably be matter of future posts. Mainly, we have reviewed the usefulness of ipython as a better interface to run python commands rather than the classical shell; notebooks as an innovate way of working and presenting work with Python; and finally some tips and tricks for using Python and related tools in a terminal.

I hope you enjoy this post and found something useful in it.None of the tools presented here are strictly necessary, but they make our life easier 🙂

As always, any recommendation, suggestion or improvement, please welcome. Thanks for reading!