Getting Started with Python, Anaconda, Google Colab and Virtual Environments

Guglielmo Feis
Analytics Vidhya
Published in
9 min readFeb 13, 2020

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

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

[The only outside view you’ll get while programming — Photo by Fotis Fotopoulos on Unsplash]

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

  • install Python with Anaconda (that, actually, most of the time ends up being called just Conda);
  • use the (Conda) prompt;
  • install new Python modules;
  • use Google CoLab for machine learning;
  • manage virtual environments in Python.

Edit: If you want to dive further into using Python for academia and Digital Humanities, check this book (which the rest of this post was one of the first outlines).

Getting Python through Anaconda

Back in the days if you wanted to start using 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 libraries 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 now). 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 install ‘Anaconda. As the snake-based name suggests, Anaconda is related to Python. With Anaconda you are going to install Python plus something else:

  • most of the scientific modules Python has to offer you (numpy, pandas, matplotlib…);
  • IPython;
  • Jupyter Notebooks support;
  • a dedicated command shell: the Anaconda Prompt;
  • Spyder: an integrated development environment (IDE) for Python; think a text editor on stereoids.

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 tools (for more on the topic of comparing Python IDEs, see here).

As a sidenote, one of the main reasons driving you towards Python could be the scientific-oriented modules Anaconda is 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). 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 sounds confusiong, the best way to find out what is going on is read the installation documentation (make it an habit to read the docs). 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 all 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. ‘Prompt’ is another way for the command line interface. This is where you cd to change directory, you dir to know what’s inside a directory and that kind of things.

“What does this has to do with programming?”, you legitimately ask. If you want to update your Python version, 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 pip, 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 tools 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:

  • ‘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);
  • ‘conda update’ is you choice to update the whole system
  • ‘jupyter notebook’ creates a Jupyter Notebook the folder you are into (see later for more Jupyter Notebooks).

To configure virtual environments, see later.

[Mom was right to be afraid about this Python programming thing… Photo by Maxx Rush on Unsplash]

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 Development Environment) 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 Monty 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).

[Not a Jupyter Notebook. Photo by Dominic Brügger on Unsplash]

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 CoLab, see below, this is no longer an issue.)

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

  • open the Anaconda Prompt;
  • 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)
  • 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.

[Virtual envs getting real… Photo by Markus Spiske on Unsplash]

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, docs are here again: 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 we are acquainted with Spyder. We also managed to create a virtual environment for development. All you we need to do to start coding is running Spyder inside our virtual environment.

This requires some further trick. 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 activate myenv on Windows). 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 ‘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:

Was this interesting? Feel free to connect over on Linkedin or join a broader conversation over on Substack.

This work is carried out as part of a CAS Fellowship at CAS-SEE Rijeka. Find out more about the Fellowship here.

--

--

Guglielmo Feis
Analytics Vidhya

M.A. phil. Ph.D.. Now in tech as a programmer. I send random stuff in your inbox https://1110sillabo.substack.com/ Guitar player, digital gardener and more.