This article will help you in the installation of python 3 on Ubuntu. You will learn the basics of configuring the environment to get started with Python.
Python is an Interpreted programming language that is very popular these days due to its easy learning curve and simple syntax. Python finds use in many applications and for programming the backend code of websites. It is also very popular for data analysis across industries ranging from medical/scientific research purposes to retail, finances, entertainment, media and so on.
When writing a python program or program in any other language, people usually use something called an IDE or Integrated Development Environment that includes everything you need to write a program. It has an inbuilt text editor to write the program and a debugger to debug the programs as well.
The latest version of python is python3 and the latest release is python3.9.0.
For downloading python and the documentation for Ubuntu, visit the official website and go to the downloads section, from where you can download the latest python version for Ubuntu.
pip is a package manager to simplify installation of python packages. To install pip ,run the below command on the terminal
sudo apt install python3-pip
Once the installation is done just install a package by running
pip install <package-name>
The purpose of virtual environments is to have a separate space where you can install packages which are specific to a certain project, For example if you have lot of flask or Django- based applications and not all the applications are using the same version, we use virtual env where each project will have its own version.
In order to use a virtual environment you need to be on the python 3.x version. Let’s understand how to create the virtual environment. You do not need any library as it comes along with standard python installation.
If you don't have virtual environment installed, use this command to install it:
pip3 install virtualenv
So to create a new virtual env, run the below command:
virtualenv env (name of virtual env)
This will create a virtual environment and will install some standard packages as well as part of the virtual environment creation.
To activate the virtual environment on ubuntu, use the below command:
To deactivate it you can run the below command in the environment:
To download Python, visit the official website and go to the downloads section. You can download the latest python version for Ubuntu as shown below:
Download the tarball and untar the file. After untarring the file, you will see a couple of files. The file you will be interested in is readme file where you can access a set of instructions to install the python on the ubuntu machine.
Open the terminal, change the directory of the untarred python file, and run the below command under cd ~/<python untarred folder>
Install python command:
This will install python as python3.
If you get an error when running sudo ./configure, like no compiler found, just install the below library to get rid of it:
apt-get install build-essential
Also if you get an error when running, like make not found, just run the below command to install make:
sudo apt install make
So once you are done installing the above libraries, like make and build-essential, you should be good with the above install python command.
The other way of installing python is by running apt get commands as below:
Open the terminal and run:
sudo apt-get update
This will make sure repos are updated to the latest in Ubuntu. Install python by running the below command:
sudo apt-get install python3
To find the existing system path set in your machine, you can run the below command:
Now suppose you want to set a different path for your Python executable, you can just use export command and give it a directory path like below:
export PATH=$PATH:`<path to executable file>’
By just running the above export command, this will not be persisted across different terminals. Again, if you close that terminal and open it again, the change would have been lost. So to make it persistent, you need to add the above command in the ~/.bashrc file present in the home directory of the ubuntu system.
To run python code just run the command
If you want to see what all packages are installed in the env, run the command pip3 list which will list down the current packages installed in the env. If you install any other packages in the env, for instance let’s say you want to install request library, you can just install it by running pip3 install requests. Now try running pip3 list again to see this requests lib installed in this env.
Inside the python project or directory you should have a file called __init__.py inside the directory. So you can create this file by a simple touch command. This file does not need to have any data inside it, it only has to exist inside the directory for that to work as a package.
This article will help you with stepwise instructions on the installation of Python on ubuntuOs.
Your email address will not be published. Required fields are marked *
Statistics is a science concerned with collection,... Read More