Python Environment Setup

Python is available on a various number of platforms such as Linux and Mac OS X. let's grasp the knowledge to set up our Python environment.

Local Environment Setup
How to get python?
How to install python?
How to set up the PATH?
How to Set up the path at Unix/Linux?
How to set up a path at Windows?
Various Python Environment Variables
Various methods to Run Python

Local Environment Setup

Open a terminal window and type the word "python" to check that python is already installed and if yes, then which version is installed.

  • Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, SunOS, IRIX, etc.)
  • Win 9x/NT/2000
  • Macintosh (Intel, PPC, 68K)
  • OS/2
  • DOS (multiple versions)
  • PalmOS
  • Nokia mobile phones
  • Windows CE
  • Acorn/RISC OS
  • BeOS
  • Amiga
  • VMS/OpenVMS
  • QNX
  • VxWorks
  • Psion
  • Python has also been ported to the Java and .NET virtual machines

How to get python?

The latest and current source code, binaries, documentation, news, etc., is available on the official website of Python https://www.python.org/

Python documentation can be downloaded from https://www.python.org/doc/. The documentation is available in a variety of formats such as HTML, PDF, and PostScript.

How to install python?

Python distribution is available for a huge number of platforms.  To install python, You need to download only the binary code suitable for your platform.

If the binary code for your platform is not available, it's necessary to compile the source code by C compiler manually. Compilation of source code offers more flexibility such as choice of features that you want in your installation.

Let's understand how to install python on different platforms:

Unix and Linux Installation

Basic steps to install Python on Unix/Linux machine.

  • Open a Web browser and go to https://www.python.org/downloads/.

  • Download zipped source code available for Unix/Linux by following the link.

  • Download and extract files.

  • Edit the Modules/Setup file if you want to customize some options according to your requirements.

  • run ./configure script

  • make

  • make install

Python is installed at standard location /usr/local/bin and its libraries at /usr/local/lib/pythonXX where XX is the version of Python.

How to install Python on windows OS?

Steps to install Python on a Windows machine are as follows:

  • Open a Web browser and go to https://www.python.org/downloads/.

  • Follow the link for the Windows installer python-XYZ.msi file where XYZ is the version you want to install.

  • The Windows system must support Microsoft Installer 2.0, to use this installer python-XYZ.msi. Save the installer file to your local machine and then run it to check if your machine supports MSI.

  • Run the downloaded file. This brings up the Python install wizard, which is convenient for usage. Just accept the default settings, wait until the install is finished, and you are done.

Macintosh Installation

Latest Macs available with Python installed, but it may be numerous years out of date. See http://www.python.org/download/mac/ for guidelines on getting the up to date version along with additional tools to support development on the Mac. For older Mac OS's before Mac OS X 10.3 (released in 2003), MacPython is available.

Jack Jansen continues it and you can have full access to the complete documentation at his website − http://www.cwi.nl/~jack/macpython.html. You can get all installation details for Mac OS installation.

How to set up the PATH?

Programs and other executable files are present in various directories. To resolve this, operating systems provide a search path which lists the directories that the OS searches for executables.

The path is stored in an environment variable, which is a named string and operating system maintains it. This variable consists of all data available to the command shell and other programs.

The path variable is termed as PATH in Unix or Path in Windows (Unix is case sensitive; Windows is not).

In Mac OS, the path details are handled by the installer. To invoke the Python interpreter from any special directory, its necessary to add the Python directory to your path.

How to Set up the path at Unix/Linux?

Methods to add the Python directory to the path for a particular session in Unix −

  • In the csh shell − type setenv PATH "$PATH:/usr/local/bin/python" and press Enter.

  • In the bash shell (Linux) − type export PATH="$PATH:/usr/local/bin/python" and press Enter.

  • In the sh or ksh shell − type PATH="$PATH:/usr/local/bin/python" and press Enter.

  • Note − /usr/local/bin/python is the path of the Python directory

How to set up a path at Windows?

Methods to add the Python directory to the path for a particular session in Windows −

At the command prompt − type path %path%;C:\Python and press Enter.

Note − C:\Python is the path of the Python directory

Various Python Environment Variables

Let's discuss some important environment variables, which can be identified by Python −

Sr.No. Variable & Explanation
1

PYTHONPATH

It has the same role to PATH. This variable explains the Python interpreter where to locate the module files imported into a program. It should contain the Python source library directory and the directories consisting Python source code. Sometimes, Python installer preset the PYTHONPATH.

2

PYTHONSTARTUP

It includes the path of an initialization file consisting Python source code. It is executed every time you start the interpreter. It is termed as .pythonrc.py in Unix and it holds commands that load utilities or customize PYTHONPATH.

3

PYTHONCASEOK

It is used to instruct Python in windows to find the first case-insensitive match in an import statement. To activate it, Set this variable to any value.

4

PYTHONHOME

It is another module search path. It is generally installed in the PYTHONSTARTUP or PYTHONPATH directories so that switching module libraries can be easy.

Various methods to Run Python

There are three different ways to start Python which are as follows:

Interactive Interpreter

You can start Python from Unix, DOS, or any alternative system that provides you a command-line interpreter or shell window.

Enter python the command line.

Start coding right away in the interactive interpreter.

$python # Unix/Linux
or
python% # Unix/Linux
or
C:> python # Windows/DOS

list of all the available command line options −

Sr.No. Option & Description
1

-d

It provides debug output.

2

-O

It develops optimized bytecode (resulting in .pyo files).

3

-S

To look for Python paths on startup, Do not run import site

4

-v

verbose output (Explained trace on import statements).

5

-X

disable class-based built-in exceptions (use only strings); obsolete starting with version 1.6.

6

-c cmd

run Python script sent in as cmd string

7

file

run Python script from given file

Script from the Command-line

A Python script can be executed at the command line by invoking the interpreter on your application, same as follows:

$python script.py # Unix/Linux

or

python% script.py # Unix/Linux

or 

C: >python script.py # Windows/DOS

Note −Assure that the file permission mode allows execution.

Integrated Development Environment

You can run Python from a Graphical User Interface (GUI) environment as well, only if you have a GUI application on your system that supports Python.

  • Unix − IDLE is the primary Unix IDE for Python.

  • Windows − PythonWin is the basic Windows interface for Python and is an IDE with a GUI.

  • Macintosh − The Macintosh version of Python along with the IDLE IDE is available from the main website can be downloaded in MacBinary or BinHex'd files.

If you are not capable to set up the environment accurately, then you can ask for help from your system admin. Make sure the Python environment is perfectly set up and working accurately.

Note − All the illustrations given in the following chapters are executed with Python 2.4.3 version available on CentOS flavor of Linux.

We already have set up Python Programming environment online, so that you can execute all the available illustrations online at the same time when you are learning theory. Feel free to customize any example and execute it online.