Python – Environment Setup and Editors

Python Environment Setup

Python is available for use with Windows, Linux, Mac OS as well as certain other platforms such as IBM AS/400, iOS, Solaris, etc.

To install Python on your local machine, get a copy of the standard distribution of Python software from https://www.python.org/downloads based on your operating system, hardware architecture and version of your local machine.

Install Python on Windows:

To install Python on a Windows platform, you need to download the installer. A web-based installer, executable installer and embeddable zip files are available to install Python on Windows. Visit https://www.python.org/downloads/windows and download the installer based on your local machine’s hardware architecture.

The web-based installer needs an active internet connection. So, you can also download the standalone executable installer. Visit https://www.python.org/downloads and click on the Download Python 3.7.0 button as shown below. (3.7.0 is the latest version as of this writing.)

Download Python Library

This will download python-3.7.0.exe for 32 bit. For the 64 bit installer, go to https://www.python.org/downloads/windows and select the appropriate 64 bit installer, as shown below.

Download Python for Windows 64 bit

Download the Windows x86-64 executable installer and double click on it to start the python installation wizard as shown below.

Python Installation Wizard

Installation is a simple wizard-based process. As you can see in the above figure, the default installation folder will be C:\Users\{UserName}\AppData\Local\Programs\Python\Python37 for Python 3.7.0 64 bit. Check the Add Python 3.7 to PATH checkbox, so that you can execute python scripts from any path. You may choose the installation folder or feature by clicking on Customize installation. This will go to the next step of optional features, as shown below.

Python Installation Wizard

Click Next to continue.

Python Installation Wizard

In Advanced Options, select the Install for all users option so that any user of your local machine can execute Python scripts. Also, choose the installation folder to make a shorter path for Python executable (something like C:\python37), keeping the rest of the choices to default and finally click on the Install button.

Python Installation Wizard

After successful installation, you can start working with Python on your local machine.

Install Python on Linux

Most of Linux distributions come with Python already installed. However, the Python 2.x version is incorporated in many of them. To check if Python 3.x is available, run the following command in the Linux terminal:$ which python3

If available, it will return the path to the Python3 executable as /usr/local/bin/python3. If not, install it by following the procedure on Ubuntu Linux:$ sudo apt-get update
$ sudo apt-get install python3.6

For other Linux distributions use the corresponding package managers, such as YUM for Red Hat, aptitude for debian, DNF for Fedora, etc.

For installation on other platforms as well as installation from the source code, please refer to the official documentation on python.org

To check whether Python installation is successful, open the command prompt window, type Python and press ENTER. A Python Prompt comprising of three Greater Than symbols (>>>) should appear.

Python Shell on Windows

Python – Shell (Interpreter)

Python is an interpreter language. It means it executes the code line by line. Python provides a Python Shell (also known as Python Interactive Shell) which is used to execute a single Python command and get the result.

Python Shell waits for the input command from the user. As soon as the user enters the command, it executes it and displays the result.

To open the Python Shell on Windows, open the command prompt, write python and press enter.

Python Shell

As you can see, a Python Prompt comprising of three Greater Than symbols (>>>) appears. Now, you can enter a single statement and get the result. For example, enter a simple expression like 3 + 2, press enter and it will display the result in the next line, as shown below.

Command Execution on Python Shell
Command Execution on Python Shell

Execute Python Script

As you have seen above, Python Shell executes a single statement. To execute multiple statements, create a Python file with extension .py, and write Python scripts (multiple statements).

For example, enter the following statement in a text editor such as Notepad.Example: myPythonScript.py

print ("This is Python Script.")
print ("Welcome to Python Tutorial by shishirkant.com")

Save it as myPythonScript.py, navigate command prompt to the folder where you have saved this file and execute the python myPythonScript.py command.

Python – IDLE

IDLE (Integrated Development and Learning Environment) is an integrated development environment (IDE) for Python. The Python installer for Windows contains the IDLE module by default.

IDLE is not available by default in Python distributions for Linux. It needs to be installed using the respective package managers. For example, in case of Ubuntu:$ sudo apt-get install idle

IDLE can be used to execute a single statement just like Python Shell and also to create, modify and execute Python scripts. IDLE provides a fully-featured text editor to create Python scripts that includes features like syntax highlighting, autocompletion and smart indent. It also has a debugger with stepping and breakpoints features.

To start IDLE interactive shell, search for the IDLE icon in the start menu and double click on it.

Python IDLE

This will open IDLE, where you can write Python code and execute it as shown below.

Python IDLE

Now, you can execute Python statements same as in Python Shell as shown below.

Python IDLE

To execute a Python script, create a new file by selecting File -> New File from the menu.

Enter multiple statements and save the file with extension .py using File -> Save. For example, save the following code as hello.py.

Python Editors (IDE)

There are many free and commercial editors available for Python. Here, we will learn how to use some open-source editors to execute Python scripts or statements.

IPython

IPython stands for Interactive Python. Created by Fernando Perez in 2001, IPython is an enhanced read-eval-print loop (REPL) environment particularly suitable for scientific computing. It offers a fully compatible replacement for the standard Python interpreter, with convenient shell features, special commands, command history mechanism and output results caching.

To install IPython, execute the pip3 command which is the in-built Python package installer.

Execute the pip3 install ipython command in the command prompt to install iPython, as shown below.C:\Users\{user name}>pip3 install ipython

After successful installation, invoke IPython from the command prompt by entering ipython and pressing enter. You can then execute the Python statement as we execute it below in command prompt.

Execute Python Statements in IPython

Jupyter Notebook

The Jupyter Notebook is a browser-based graphical interface to the IPython shell. It allows the user to include formatted text, static and dynamic visualizations, mathematical equations, JavaScript widgets etc. along with the Python code. The Jupyter Notebook document can be exported to PDF, Python script or HTML.

By default, the IPython kernel drives the Jupyter Notebook application. However, it supports other languages like Julia and R. (Jupyter stands for JUlia, PYThon and R).

To install Jupyter, use the pip utility shipped with Python software.C:\Users\{user name}>pip3 install jupyter

After successful installation, we can start the Jupyter editor from the command prompt as below.C:\Users\{user name}>jupyter notebook

Jupyter Notebook is a client-server application. The server is deployed on the localhost’s default port 8888 and the client is opened in a browser window as shown below:

Jupyter

As you can see, Jupyter will display files and folders from the Python installation folder. You can create, open and execute python scripts from the appropriate folders. Start a new notebook by choosing Python 3 from the “new” drop down as shown below:

New Python Script in Jupyter

This will open another window to enter python statements and run them as shown below.

New Python Script in Jupyter

The interface is similar to IPython shell. However, there are a lot of other advantages.

For instance, you can insert and delete cells. A cell can contain code, heading or a markdown text which acts as documentation. The code in any cell can be run. Another advantage is that data visualizations generated by libraries like Matplotlib can be incorporated inline.

The notebook is saved with the .ipynb extension. It can be exported to HTML or PDF format so that it can be shared.

Virtual Environment

Many times, Python packages developed by certain third-parties have to be installed while developing Python-based applications. However, requirement for a specific version of the same package may sometimes be conflicting with other applications’ requirements. Hence, it is desired to have side-by-side environments for each purpose to avoid compatibility issues. This is achieved by setting up a virtual environment.

The venv module in a standard Python library used to create virtual environments. First run following commandC:\Python36>python -m venv c:\myvenv

This will create c:\myvenv and directories inside it containing a copy of the Python interpreter, the standard library, and other supporting files.

To activate the environment run a batch file called activate.bat in the scripts subdirectory. The name of the current environment appears on the left side of the windows command prompt.

Now you can run a local copy of the Python interpreter from the command prompt.

Python Virtual Environment Setup

Run deactivate.bat to terminate the virtual environment

Online Python Shell

Installing Python (or any software) can be a little daunting for a newbie. Fortunately there are many online resources to get familiar with the syntax, features and philosophy of Python before deciding to install Python in the local machine.

You can launch an online Python Shell directly from the official website – https://www.python.org/shell. The Shell terminal shows a Python prompt (>>>) in front of which any valid Python expression can be written, which is executed on pressing ‘Enter’.

Python Environment Setup

Many interactive Python environment shells can be found on the internet. They work based on REPL (Read, Evaluate, Print, Loop). Using https://repl.it it is possible to execute Python in interactive as well as in scripting mode.

Python – repl.it
Follow Us On