Saturday, 21 January 2017

How To Install Python 3 and Set Up a Local Programming Environment on Ubuntu 16.04

Setting Up Python 3 :

      This tutorial will get you up and running with a local Python 3 programming environment in Ubuntu 16.04.
      On Ubuntu 16.04, you can find the Terminal application by clicking on the Ubuntu icon in the upper-left hand corner of your screen and typing “terminal” into the search bar. Click on the Terminal application icon to open it. Alternatively, you can hit the CTRL, ALT, and T keys on your keyboard at the same time to open the Terminal application automatically.

    Ubuntu 16.04 ships with both Python 3 and Python 2 pre-installed. To make sure that our versions are up-to-date, let’s update and upgrade the system with apt-get:
           
                   $  sudo apt-get update
                   $  sudo apt-get -y upgrade

The -y flag will confirm that we are agreeing for all items to be installed, but depending on your version of Linux, you may need to confirm additional prompts as your system updates and upgrades.


Once the process is complete, we can check the version of Python 3 that is installed in the system by typing:
             
          $ python3 -V
You will receive output in the terminal window that will let you know 
the version number. The version number may vary, but it will look 
similar to this:

           Python 3.5.2
To manage software packages for Python, let’s install pip:

          $ sudo apt-get install -y python3-pip
A tool for use with Python, pip installs and manages 
programming packages we may want to use in our development projects. You
can install Python packages by typing:
     
                 $  pip3 install package_name
Here, package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install NumPy, you can do so with the command pip3 install numpy.

                 $  sudo apt-get install build-essential libssl-dev libffi-dev python-dev

Once Python is set up, and pip and other tools are installed, we can set up a virtual environment for our development projects.

Setting Up a Virtual Environment :

Virtual environments enable you to have an isolated space on your computer for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects.


Setting up a programming environment provides us with greater control over our Python projects and over how different versions of packages are handled. This is especially important when working with third-party packages. 

         We need to first install the venv module, part of the standard Python 3 library, so that we can invoke the pyvenv command which will create virtual environments for us. Let’s install venv by typing:
             $ sudo apt-get install -y python3-venv

With this installed, we are ready to create environments. Let’s choose which directory we would like to put our Python programming environments in, or we can create a new directory with mkdir, as in:
       
             $  mkdir environments
             $  cd environments

Once you are in the directory where you would like the environments to live, you can create an environment by running the following command:

             $   pyvenv my_env

Essentially, pyvenv sets up a new directory that contains a few items which we can view with the ls command:

             $   ls my_env


Together, these files work to make sure that your projects are isolated from the broader context of your local machine, so that system files and project files don’t mix. This is good practice for version control and to ensure that each of your projects has access to the particular packages that it needs. Python Wheels, a built-package format for Python that can speed up your software production by reducing the number of times you need to compile, will be in the Ubuntu 16.04 share directory

To use this environment, you need to activate it, which you can do by typing the following command that calls the activate script:

               $  source my_env/bin/activate