Lesson 2: Installing Python

This lesson covers how to install and run Python on both Windows and Gnolinux operating systems, and concludes with a brief description of Python’s “closed management system.”

✔Level: Basic

Headlines

Lesson 2: Installing Python
  • Download
  • Installed on Windows
  • Installed on GNU Linux
  • Package management system

Download

To translate and execute the source code created in Python, you need to download the “Python installation package” (the same CPython or, to put it simply: Python), which includes the interpreter, the standard library, the IDLE program (the default Python editor), and so on. Install on the operating system.

Attention

Python is the name of a programming language. This means that it is a structure for expressing the programmer’s ideas or commands to the machine. Understanding Python commands to the machine, like any other high-level language, requires a “Language Processor”. CPython is the standard Python language processor, simply referred to as Python.

Python may be pre-installed on any operating system (especially on Gnolinux), which you will no longer need to download and install if you are satisfied with the current version; To find out, you can enter the command python –version on the operating system command line; However, in Windows, because the operation of this command depends on manipulating the Path variable, it is better to go to Control Panel> ‌ Programs and Features.

tip

Path is one of the “Environment Variables” of the operating system. This variable contains a list of directories in which the operating system looks for an executable file of the same name as the command entered on the command line.

This package is now available for download based on two different versions of Python (2x and 3x) from the download page, depending on the type of operating system and processor architecture (size: approximately between 12 to 25 MB), which for installation in Windows in the form of a The installation file (in msi format) and compatible with the two architectures 32 (x86) and 64 (AMD64, EM64T, x64, x86-64) will be published and if you need to install it in Gnolinux, you will need its source code (which is in C language). Is Download.

Attention

It is possible to install different versions of Python (even from a single branch) side by side.

At the time of writing, 2.7.9 and 3.4.2 are the most recently released versions of Python. To access the latest features, it is recommended that you always download the latest available version of Python.

Installed on Windows

Just like any other program in Windows, the installation is easily completed with just a few clicks on the Next button. It is recommended that you do not change the default installation path (for example, to install version 3.4.2: \C:\ Python34).

When installing the version that you intend to use as the default version of your Python, note that in the Customize step, enable the option to automatically add the Python interpreter path to the Windows Path variable (as shown below). In this case, by entering the python command in the Windows command line, the Python interpreter (this version) is called. To get started, enter the python-V or python -version command to find out the installed Python version:

python
customize python
> python --version
python 3.4.2

If the Path variable is not set, you must enter the Python interpreter installation path through the command line window and the cd command:

> python --version
'python' is not recognized as an internal or external command, operable program or batch file.
> cd C:\Python34\
> python --version
python 3.4.2

Of course, it is possible to manipulate the Path at any time:

Go to Control Panel> System> Advanced system settings> Advanced and then click on Environment Variables, a new window will open, find and select the Path in the System variables section. Click Edit at the bottom of the same window and type; C:\ Python34; C:\ Python34\Scripts (for version 3.4) or; C:\ Python27; C:\ Python27\Scripts (for version 2.7) at the beginning of the text Enter the new window in the Variable value section and then click the OK button.

python
edit system variable

consideration

In Windows, the semicolon character (Semicolon) or ; Used to separate the path of directories in the path variable. C:\Python34 from this phrase specifies the path of the Python interpreter (python.exe) and due to the addition of pip (Python package management system) to the Python installation package from version 3.4 onwards, C:\Python34\ Scripts Added to enable access and call (pip.exe or pip3.exe or pip3.4.exe – no matter).

If you want to install a version of Python on both windows or even several different versions of each, such as versions 2.6, 2.7, 3.3 and 3.4; A good way to get the Python interpreter for each of them on the Windows command line is to use the Python Launcher. This solution does not need to manipulate the Path variable and uses the py command instead of the python command; In this way, with the py or py -2 command, the interpreter of the latest available version of the 2x Python branch is called, and with the py-3 command, the interpreter of the latest available version of the 3x Python branch is called. To call the interpreter of other versions, their versions must also be explicitly mentioned (eg: py -3.3 and py-2.6):

> py --version
2.7.9
> py -2 --version
2.7.9
> py -2.6 --version
2.6.6
> py -3 --version
3.4.2
> py -3.3 --version
3.3.5

Installed on GNU Linux

Python is usually pre-installed in GNU-Linux distributions (some of both versions also have versions installed, such as Ubuntu and Fedora). To be sure, just enter the commands python2 –version (for version 2x) and python3 –version (for version 3x) in the operating system command line; For example, the status of Python versions in Ubuntu 14.04 is as follows:

user> python --version
python 2.7.6
user> python2 --version
python 2.7.6
user> python3 --version
python 3.4.0

Attention

Since (early 2015) version 2x is still the default version of Python in most GNU-Linux distributions, the python -version command also calls the Python interpreter for version 2x and displays its version.

Arch Linux is the first GNU-Linux distribution to make 3x the default version of Python.

In the mentioned commands, instead of version–, V- (capital letter v) can also be used.

Now assuming that the distribution used already lacks the 3x version or that the installed version is so old that it needs to be upgraded; After downloading the source code of the new version (at this time the file: Python-3.4.2.tar.xz) we will do the following (install the 2x version is the same):

First, all the prerequisite packages must be installed on the operating system. For this purpose, you can enter the following commands (according to your type of distribution) in the command line of the operating system:

In Fedora:
user> sudo dnf update
user> sudo dnf install make automake autoconf pkgconfig glibc-devel gcc gcc-c++ bzip2 bzip2-devel tar tcl tcl-devel tix tix-devel tk tk-devel zlib-devel ncurses-devel sqlite-devel openssl-devel openssl readline-devel gdbm-devel db4-devel expat-devel libGL-devel libffi-devel gmp-devel valgrind-devel systemtap-sdt-devel xz-devel libX11-devel findutils libpcap-devel
In Ubuntu:
user> sudo apt-get update
user> sudo apt-get install build-essential
user> sudo apt-get install make automake autoconf pkg-config libc6-dev gcc g++ bzip2 libbz2-dev tar tcl tcl-dev tix-dev tk tk-dev zlib1g-dev libncursesw5-dev libsqlite3-dev libssl-dev openssl libreadline-dev libgdbm-dev db4.8-util libexpat1-dev libgl-dev libffi-dev libgmp3-dev valgrind systemtap-sdt-dev xz-utils libX11-dev findutils libpcap-dev

After confirming the installation of the prerequisite packages, go to the path where the Python source code (after downloading) is located (here: Downloads directory) and decompress the source code file. The result is a new directory with the same name as Python-3.4.2, which can be viewed using the ls command; Now you have to enter the path through the command line and the cd command.

user> cd Downloads/
user> ls
Python-3.4.2.tar.xz
user> tar xf Python-3.4.2.tar.xz
user> ls
Python-3.4.2 Python-3.4.2.tar.xz
user> cd Python-3.4.2/

At the end, enter the following commands in order:

user> sudo ./configure
user> sudo make
user> sudo make install

If a 3x version of Python is already installed (such as 3.3), the third line command will replace the new version (3.4); This command is useful when upgrading to Python. If you still want to access the previous version, replace the make install command with make altinstall. For example, the status of the 3x version of Python, before and after installing the new version, along with its installation path in Fedora 20 is given. Note the difference in performance between the two commands make altinstall and make install:

Get pre-installed versions:
user> python --version
Python 2.7.5
user> python2 --version
Python 2.7.5
user> python3 --version
Python 3.3.2
Get the path and version of Python 3x after installing version 3.4.2 using the make altinstall command:
user> which python3
/usr/bin/python3
user> which python3.4
/usr/local/bin/python3.4
user> python3 --version
Python 3.3.2
user> python3.4 --version
Python 3.4.2
Get the path and version of Python 3x after installing version 3.4.2 using the make install command:
user> which python3
/usr/local/bin/python3
user> which python3.4
/usr/local/bin/python3.4
user> python3 --version
Python 3.4.2
user> python3.4 --version
Python 3.4.2

Another way to install several different versions of a Python directory is to install each one at a specific location on the disk. For this purpose you must use the same commands below:

user> sudo ./configure --prefix=/opt/python3.4
user> sudo make
user> sudo make install

consideration

The phrase opt/python 3.4/ in the first line indicates the location of the Python installation, which is determined by the user.

If you use this method, the installed version of the Python interpreter can be called similar to either of the following two commands (with the installation path here: opt/python3.4/):

user> /opt/python3.4/bin/python3 --version
Python 3.4.2
user> /opt/python3.4/bin/python3.4 --version
Python 3.4.2

For convenience of calling, you can add the address of the Python interpreter directory to the operating system Path variable. To do this, hide the bashrc file. (Of course, if you use the default shell, ie bash) Open the home directory (path) by a text editor, enter a similar phrase below it, and then save the change:

export PATH=$PATH:/opt/python3.4/bin

Now to call the installed Python, you no longer need to enter its path, but keep in mind that due to the 3x version that was pre-installed (here: 3.3.2), you need to call the new version by explicitly mentioning the version:

user> python3 --version
Python 3.3.2
user> python3.4 --version
Python 3.4.2

Attention

In general, to call Python version 3x, one of the commands python3.4, python3 or python3.x is used, where x represents the part of the Python version, and for version 2x, the commands python2.7, python2, python or python2.x are used. In this regard, if Python is installed in a specific path, its path needs to be added to the Path variable. The same procedure is followed for calling pip and IDLE of each version.

Package management system

pip is a Python package management system. pip is a command line-based tool used to install, uninstall, update, and manage Python packages (or third-party libraries) in general. After finding the package you need in PyPI or other websites and services such as github.com and bitbucket.com, the programmer can install it in Python using the pip command in the command line.

tip

PyPI or Python Package Index is where many Python libraries or third-party applications are housed. Python users can publish their project (or package) via PyPI or search and download the packages they need.

pip has been added to the Python installation package since version 3.4 and will be installed with it, but if you need pip for older versions, you will need to install it separately.

Attention

Python version 2.7.9 has been released after version 3.4; So by installing this version and newer versions of it from the 2x Python branch, pip will also be available.

To install pip, you need to download the get-pip.py file.

Then, with a command similar to python get-pip.py in the command line, you can install pip with the Administrator user level (in Windows) or root (in Gnolinux). Remember, an internet connection is required during installation.

Attention

Python in the python command get-pip.py means calling the version of the Python interpreter in which we want to install pip.

For example; Assuming that get-pip.py is downloaded and it is in the Downloads directory of the operating system, ‌ To install pip in the 3x (for example old!) Version of Python, proceed as follows:

In GNU Linux:
user> cd Downloads/
user> sudo python3 get-pip.py
[...]
Successfully installed [...]
user> pip3 --version
pip 7.0.1 [...]
In Windows:
> cd Downloads\
> python get-pip.py
[...]
Successfully installed [...]
> pip --version
pip 7.0.1 [...]

Note that before this, we had added Python version 3x to the Windows Path variable in the order mentioned, and cmd was also executed as Administrator.

Pip is very easy to work with. For example, to install Bottle, which is a Web Framework for Python, the pip install bottle command is used. By entering this command, Bottle is searched in PyPI (as the default pip repository) and after finding, first download, then install and add to the Python site-packages directory.

Here are some common pip commands. For more information on how to use pip, you can refer to its documentation.

  • Install the latest version of a package:
# pip install [package name]
root> pip install SomePackage
  • Install a specific version of a package:
# pip install [package name]==[version]
root> pip install SomePackage==1.0.4
  • Delete a package:
# pip uninstall [package name]
root> pip uninstall SomePackage
  • Update a package:
# pip install --upgrade [package name]
root> pip install --upgrade SomePackage

The same template is used to update pip itself: pip install –upgrade pip

Of course, in Windows, you must use the command python -m pip install -U pip.

Instead of upgrade– you can also use U-.

  • Get a list of all installed packages:
user> pip list
  • Get a list of all the packages that need to be updated:
user> pip list --outdated
  • See details of an installed package:
# pip show [package name]
user> pip show SomePackage
  • Install all packages specified in a text file such as requirements.txt (sample file):
root> pip install -r requirements.txt

I hope it was useful 😊

Leave a Reply

Your email address will not be published. Required fields are marked *