setting up python featured image

Installing Python 3 and Setting up a Local Programming Environment on Ubuntu 16.04

Introduction

Python is an interactive, object-oriented, and simple programming language which can be used for a plethora of different programming projects.

A fun fact is that Python is not named after the python snake but after the comedy television show Monty Python’s Flying Circus. Python is a great language to get started with programming and a great choice for experienced programmers as well. It is easy to learn as it is an expressive and high-level programming language. The latest version of the language is Python 3 and since Python 2 is no longer supported, Python 3 is the version of the language to go for.

In this tutorial, you will learn how to install Python 3 on your local Linux machine and how to set up a programming environment using the command line. We will be using Ubuntu 16.04 for the explicit installation procedure, but the steps are general and could be used on any other Debian Linux distribution.

First Things First

As this is a hands-on tutorial, you should have an installation of Ubuntu 16.04 as your operating environment, as well as have root privileges to that machine and an internet connection. Here is a step-by-step tutorial that will walk you through the Ubuntu installation.

Step 1: Set Up Python 3

In this tutorial, you will be using the command line for installation and setup, which is a text-based interface to your system. It is often referred to as terminal, shell, console, or command prompt. While using the terminal you’ll be typing your input in the form of text and the response from your system will be in text as well. It is an everyday tool for a developer as there are several powerful actions you can perform with ease using the terminal.

Updating Python

Ubuntu 16.04 has a pretty intuitive user interface. You can find out the Terminal application by clicking on the left-hand corner of the screen and searching for “terminal.” You can also click on the Terminal application icon to open it. One more way to open the terminal application is to hit the shortcut the CTRL, ALT, and T keys at the same time:

Updating Python

Ubuntu 16.04 ships with both Python 2 and Python 3 pre-installed. To ensure that your system is up-to-date, let’s update and upgrade the system with apt:

The “-y” flag is used to confirm that you are agreeing to all the packages and prerequisites to be installed. Taking into account the version of Linux you are using, more confirmations may be required for system updates and upgrades.

Now the update/upgrade process should be complete. You can check for the version of Python 3 installed in the system by running the below command:

You will be able to see the output with the version number in the terminal window. The version number can be different, but it will look like the one below:

Installing pip

You can use the tool – Pip to manage software packages for Python. You can install pip using the command below:

It is a tool that can install and manage programming packages you may require in your projects. You can install the Python packages using the command below:

In the above command, package_name can be any Python package or library, such as Flask for web development or OpenCV to process images and videos. If you would like to install Flask, you can install it using the command pip3 install Flask.

To ensure that you have a proper set-up for the programming environment, you can install some more essential packages using the following command:

Once Python is set up and other tools are installed, you can set up the virtual environment for projects.

Step 2: Virtual Environment Setup

Virtual environments enable you to have an isolated space on your computer for Python projects. Thus, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects. Using a programming environment enables you to have fine-grain control over different versions of packages in Python projects. It becomes really helpful while working with different third-party packages across projects. You can have as many Python programming environments as you need. Each of these environments is essentially a folder in your system which has a few scripts to make it work as a separate environment.

Installing venv

First of all, you need to install venv module, which is a part of the Python 3 library, So that you are able to create a virtual environment. venv can be installed using the following command:

You can create virtual environments now. Select any directory in which you would like to put virtual Python programming environments, alternatively, you can create a new directory to host your environments.

Once you are in the desired directory, you can create a virtual environment using the following command:

This step will set up a new directory that contains a few directories and files which can be viewed using ls command:

All in all these files will work to make sure your project space is separate from your local system’s files, so that project files don’t get mixed up.
It is a good practice to have separate environments for version control and to manage particular versions of packages required by your project. Python Wheels are a component of the Python ecosystem that helps to make package installation smoother and efficient. It allows for fast install and stability in the package distribution process. When you install the package using pip, there are chances that wheels did the installation.

Using venv

You need to activate the virtual environment before using it. To activate the virtual environment, run the following command:

The command prompt will now appear with the name of the virtual environment prefixed. In this case, it will show up as my_virtual_env. Your environment may look different but your virtual environment name will be the first thing on the command line prompt.

The virtual environment name in the form of the prefix is the name of the active environment. Programs in that specific environment will use the packages in the same environment.

Note: In the virtual environment, if preferred, use python instead of python3 and pip instead of pip3. Outside the virtual environment, python3 and pip3 commands have to be used.

Step 3: Using the Virtual Environment

Since your virtual environment is now ready to use, let’s try to run a simple “Hello, World!” program. By doing this step you will make sure the virtual environment is working and it will make you a little more familiar with Python. You can use any command-line text editor such as nano or vim and create a new Python file:

Once the editor opens you can type out your program:

If everything looks fine, press Ctrl + O to save the file. Then, press Ctrl + X to exit the editor. After that, you can run the program:

The hello_world.py program that you have just written should produce the output as below:

To exit the virtual environment, use the deactivate command and you should return to your directory.

Conclusion

Congratulations! Now you have set up a Python 3 programming environment on a local Linux machine.

Here are some other tutorials if you want to start utilizing Python:

Happy Computing!