While many programmers may argue which language is better, I decided to learn Python as most people agree that it is a good language for beginners. I purchased Getting Started with Python by Dusty Phillips, Fabrizio Romano, and Benjamin Baka and will be referring to this book. Before we begin any activity, let’s setup the Python development environment.
I’m currently using Ubuntu operating system and I used following commands in terminal:
sudo apt-get install python3.7 python3.7-dev python3.7-venv python3-venv python-pip
For other operating systems, please go through this quick guide on Python installation.
Even though I have successfully installed Python 3.7.1 on my desktop, I am still unable to use the same version. After typing python in console, the system is considering version 2.7 by default. To make Python 3.7 the default version, we need to modify update-alternatives.
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1
After using the above command in console, the system considered 3.7 to be the default version, as shown in the following output:
tushar@linux:~$ python Python 3.7.1 (default, Oct 22 2018, 11:21:55) [GCC 8.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
Virtual Environment Setup
To avoid common pitfalls in Python development, it is recommended to use virtual environment. After creating a directory for the project, use cd command in console and type the following:
python -m venv project_name
While researching, I found that most experienced programmers use the same names for directory as well as the project to avoid confusion.
After the above command executes, few additional folders will be created within the parent directory. The names of the folder are – bin, include, lib, and share. Along with folders, a configuration file will also be created with the name of pyvenv.cfg.
Out of the four folders, first three are most important. The bin folder contains files that are important for the virtual environment. The include folder contains header files that are essential for the compilation process. Finally, the lib folder contains dependencies that are essential for the project.
To activate the created virtual environment, we use the activate script found in bin folder.
As a confirmation, the environment name appears before the prompt in console.
To exit the virtual environment, simply type deactivate and press enter.