[Old and first version of this medium post. ]

Getting Started with Python (and Anaconda, Google Colabs and virtual environments)


Sooner or later the time comes you want to learn a bit of programming. Maybe you are in the middle of a career switch, maybe you need some automation and need to run a script, maybe you are a musician who needs to run Spleeter), or you got hyped up and stuck in some buzzwords (AI, machine learning, crypto, big data) or maybe you just think it’s cool.

No matter what, the time comes you decide to install some Python and learn it. Cool, welcome to a nice journey!

I’ll spare you the details of my exploration into Python. Here’s a short guide on how to install Python and get started to play with it. I will discuss how to:

  1. install Python with Anaconda (that, actually, most of the time ends up being called just Conda);
  2. use the (Conda) prompt;
  3. install new Python Modules;
  4. use Google CoLabs for machine learning;
  5. manage virtual environments.

Getting Python through Anaconda

Back in the days if you wanted to start Python you had to google a lot. There were two main running versions of the language: Python 2 and Python 3. Some of the syntax was different (i.e. how to print on the screen), some of the main tools were only running on one Python version, etc.

Today you can save all this googling. Go and install Python 3 (unless you have a reason to still use Python 2, which should not be the case if you are starting out). Despite the Python versions struggle being over, getting started with Python still has a counterintuitive issue. The best way to get started with Python is not installing Python.

Rather, to install Python you have to google and search ‘Anaconda. As the snake-based name suggests, Anaconda is related to Python. With Anaconda you are going to install Python plus some other things. The list of things includes:

  1. most of the scientific modules Python has to offer you (numpy, pandas, matplotlib…);
  2. IPython;
  3. Jupyter Notebooks support;
  4. a dedicated command shell: the Anaconda Prompt;
  5. Spyder: a development tool for Python.

Even if you are not interested in the Anaconda-based one stop solution, your Python experience will nonetheless require you to work into the command shell (which is a bit of a mystery for windows users). You may need to use some development tool (for more on the topic see here).

As a sidenote, one of the main reasons driving you towards Python could be the modules Anaconda is already providing as included batteries.

Installing Anaconda

Installing Anaconda is as easy as to go to https://www.anaconda.com/distribution/ and download the relevant distribution for your computer. If you come from Windows as I do, you need to find out if your system is 32 or 64 bit. Right click on This Pc, select ‘properties’ and you’ll find the answer. (If this quick fix doesn’t work, for more on the issue of determining if you are running 32 or 64 bit OS, for all main platforms see here).

Basically while installing Anaconda you will be asked where to install it, if you want it to be the default program to run Python files and how to interact with PATH variables. If something puts you into confusion the best way to find out what is going on is read the installation documentation. For bonus points on Windows and path variables see here.

Ok, now you should have Anaconda and Python installed!

Meet the Shell Terminal: the Anaconda Prompt

If you are a Windows user, chances are that ‘Terminal’, ‘Shell’ or ‘BASH’ sound unfamiliar to you. On Windows 10 there’s a Power Shell. Search it and open it.

It feels like the computer from the ’90s or early, doesn’t it? If you remember the MS-DOS days, that’s it. This is where you cd to change directories, you dir to know what’s inside a directory and that kinds of things.

What does this has to do with programming? you legitimately ask. If you want to update your program, install different modules and libraries, etc. it turns out the command line is a proficient way to do that. You can install more packages to your Python using the pip command, i.e. Pip Installs Packages (programmers seem to like recursive acronyms).

If this sounds confusing, there are two good news:

  1. there’s a nice tutorial called Learn Enough Command Line to Be Dangerous. This will do the trick and also give you a lot to feel comfortable when you’ll be using Git and GitHub (which are great tool to cooperate with the world and share your work);
  2. Anaconda will make this easier for us. Anaconda comes with its own command line interface called “Anaconda Prompt”. Just search for it and start it. You have a command line tool. Typing ‘conda’ plus something allows you to run the commands and interact with your Anaconda version.

Basic Anaconda Prompt Commands

Anaconda Prompt Commands are pretty straightforward:

  1. conda install is used to install packages, e.g. conda install pdfminer (sometimes there are specific flags to be added or different channels to download your packages from. Just google ‘conda install [package]’ and you’ll find detailed instructions, e.g. https://anaconda.org/conda-forge/pdfminer);
  2. conda update is you choice to update the whole system
  3. jupyter notebook creates a Jupyter Notebook the folder you are into (see later for more Jupyter Notebooks).

To configure virtual environments, see later.

Running Python through Anaconda

To run Python through Anaconda you have at least two options:

  1. type “spyder” in your system search box. You will then run the Spyder app. Spyder is the IDE (Integrated Developer Editor) you will use to write and run your programs (see more below);
  2. another way to run Python is to open the Anaconda Prompt and type ‘spyder’. This will open the IDE.

The Benefits of Spyder

Spyder offers a lot of benefits. The one I’ve found the most useful is that you can devide your workspace in different cells. Each cell allows you to run Python code.

The importance of running more Python code cells is that you can have a main cell where most of the program lives and as many cells as you need to experiment. If you need to add a new feature to the main program you can test it in the cell, and then include it. With the default Python IDE (called IDLE, from the Monthy Python show) you either have to open more windows or comment out what you do not want to execute when you run your program.

Plus, Spyder supports you when you are typing the commands. Press ‘TAB’ and it will offer you suggestions about what you can do with certain objects. This also helps you learning. Spyder know your object is a string and will show you the available methods.

As you move your first steps into programming it is likely that make various syntax errors and helps you indent your code properly. Spyder checks your syntax and it can check it also against some formatting rules and styles (most notably, pep8).

IPython

IPython is an interactive shell to run Python commands. Contrary to the Python code you write in Spyder or Python editor, if you press ‘enter’ inside a Python shell that is immediately executed. No need to run it.

Working in the shell allows you to have faster responses. You type the commands and see if they are doing what you want. Prototyping in the shell is fast and it is also useful to stay in the shell after running a program to see what happened. IPython is perfectly suited for this and much more.

In fact, the ‘I’ in IPython stands for interactive. Interactivity is gained by tab completion of commands (as in Spyder) and by putting you close to your code. If you don’t know what’s the type of a certain object you simply ask the shell and it will tell you. Just write type(object).

Further you can use a ‘?’ to figure out what the various functions do if you don’t remember of if you are simply curious. In that way you prototype code and interact with the documentation of the module you are using.

Besides that, IPython offers you “magic methods” that you can use to measure how long does it take to run your code and much more.

Here is the IPython intro tutorial.

Jupyter Notebooks

Jupyter notebooks are a nice interaction of code and text. You run your code in cells or chunks, as in Spyder. Nonetheless, in a Jupyter notebook a cell can also be a text chunk (written in Markdown).

This allows to write tutorials in which you discuss a problem and then present the code. The code, then, can be run in the browser. Jupyter Notebooks are perfect for portability. Still, you need to have the dependencies and modules installed on your machine. (With Google CoLabs, see below, this is no longer an issue.)

Anyway, all you have to do to open a Jupyter Notebook with Anaconda is:

  1. open the Anaconda Prompt;
  2. go to the folder you want to create a notebook into (i.e. use cd to change directory and mkdir to create a new one, if needed)
  3. type jupyter notebook.

The notebook will be created and you’ll browser will open in the notebook. (To close the notebook get back to the conda shell and press ‘CTRL + C’.)

Here is a tutorial on running a Jupyter notebooks with Anaconda on Windows with step by step instructions and pictures.

Oh, there are controversies on whether Notebooks are as super good as they look like. Google ‘notebook skeptics’ out.

Virtual Environments

When you program, it is good to keep what you are developing in isolated compartments. You don’t want a system update to crush your program, neither you want some new module you download to interact and conflict with what you have.

Given that, wouldn’t it be nice to keep different programs in different places that do not interact with each other? The answer is building virtual environments.

To build that you need to:

  1. call the Anaconda Prompt;
  2. type conda create –name [environment name] (you can specify what packages do you want and which versions, e.g. conda create -n myenv python=3.6 scipy=0.15.0 howdoi, arcade)
  3. to active the environment, type: conda activate [environment name]
  4. to deactivate the environment, type: conda deactivate [environment name]
  5. to have a list of all you created environment type: conda info –envs or conda env list
  6. to remove the created env, type conda remove –name [environment name] –all

For more infos: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html

Anaconda and Spyder: activating the environment

Ok, we now have Python installed with Anaconda and are acquainted with Spyder. We also managed to create a virtual environment for your development. All you we need to do to start coding is running Spyder inside our virtual environment.

This requires some further tricks. First, read this (especially under the heading of “the modular approach”).

Summing up, this is how it worked for me:

  1. Activate the environment in which you’d like to work (e.g. with source activate myenv on macOS/Linux or activate myenv on Windows, workon myenv for virtualenv/venv, etc). Suppose you installed the arcade library into an environment call arcade. To activate it type: conda activate arcade;

  2. Install the spyder-kernels package there, with the command: conda install spyder-kernels=0.*

  3. Once you have everything up and running (i.e. you created the env and installed all you need there). Run spyder from conda prompt with the environment activated. To do that, type ‘spyder3’ or cmd /c spyder3.exe and you’ll have the dependencies loaded in your environment.

Google CoLab or Python on the web

The last option to consider to get start with Python is Google CoLab. If you want to try out Python and don’t want to mess up with installations just go over to https://colab.research.google.com/ and try out Google CoLab.

Here’s their explanation of the platform:

What is Colaboratory? Colaboratory, or “Colab” for short, allows you to write and execute Python in your browser, with Zero configuration required Free access to GPUs Easy sharing.

Nice, isn’t?

Basically you will have the architecture of Jupyter Notebooks that you can run on Google’s machines. Notebooks allows you to mix Markdown language (i.e. a super fast way to typeset your text. Think faster and easier HTML or the language of the first internet forums) and Python cells.

If this doesn’t look exciting enough, the platform has many built in tutorials on data analysis and machine learning, like these:

  1. Intro to pandas: https://colab.research.google.com/notebooks/mlcc/intro_to_pandas.ipynb
  2. tensorflow programming concepts: https://colab.research.google.com/notebooks/mlcc/tensorflow_programming_concepts.ipynb
  3. charting data: https://colab.research.google.com/notebooks/charts.ipynb