[Old and first version of this medium post. ]
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:
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:
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 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!
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:
Anaconda Prompt Commands are pretty straightforward:
To configure virtual environments, see later.
To run Python through Anaconda you have at least two options:
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 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 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:
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.
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:
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:
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;
Install the spyder-kernels package there, with the command: conda install spyder-kernels=0.*
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.
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.
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: