Python Environment Setup
Installing Python in Windows
You can install Python by downloading the installation package from this link: https://www.python.org/downloads/ On this page, you can choose between Python 3.5.1 and Python 2.7.11. The two latest versions of Python 2 and 3 are available on this page. Alternatively, you can choose between Python 2.5.1 and Python 2.7.11., if you are looking for a specific release, you can scroll down the page to find download links for earlier versions.
You would normally opt to download the latest version, which is Python 3.5.1. This was released on December 7, 2015. However, you may opt for the latest version of Python 2, 2.7.11. Your preferences will usually depend on which version will be most usable for your project. While Python 3 is the present and future of the language, issues such as third party utility or compatibility may require you to download Python 2.
Installing Python in Mac
If you're using a Mac, you can download the installation package here: https://www.python.org/downloads/mac-osx/ Installing Python: After you've finished downloading, you can run the .exe file to begin installing Python. IDLE, pip, and documentation are included in the standard installation.
We will learn how to set up our Python environment on Linux and Mac OS X.
Local Environment Setup
Open a terminal window and type "python" to find out if it is already installed and 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
Getting Python
Python's official website, https://www.python.org/, has the most up-to-date and current source code, binaries, documentation, news, etc.
Python documentation is available at https://www.python.org/doc/. Documentation is available in HTML, PDF, and PostScript.
Installing Python
Python distributions are available for a wide range of platforms. Install Python and download only the binary code applicable to your platform.
If your platform does not have binary code, you can compile the source code manually using a C compiler. It offers more flexibility in terms of choosing the features you want to include in your installation when you compile the source code.
The following is a quick overview of how to install Python on various platforms:
Unix and Linux Installation
The following steps explain how to install Python on a Unix/Linux machine.
-
Go to https://www.python.org/downloads in a Web browser.
-
You can download the zipped source code for Unix/Linux by following the link.
-
Files should be downloaded and extracted.
-
You can customize some options by editing the Modules/Setup file.
-
Run the ./configure script
-
make
-
make install
Python is installed at /usr/local/bin and its libraries at /usr/local/lib/pythonXX, where XX is the Python version.
Setting up PATH
Operating systems provide a search path, a list of the directories where executable files can be found, for programs and other executable files.
A path is stored in an environment variable, which is a named string maintained by the operating system. Command shells and other programs can access this variable.
PATH is the path variable in Unix; Path is the path variable in Windows.
The installer handles path details in Mac OS. In order to invoke the Python interpreter from a particular directory, you must add the Python directory to your path.
Setting path at Unix/Linux
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
Setting path at Windows
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
Python Environment Variables
Here are important environment variables, which can be recognized by Python −
Sr.No. | Variable & Description |
---|---|
1 |
PYTHONPATH It plays a similar role to PATH. The Python interpreter uses this variable to find the module files imported into a program. The Python source library directory and the Python source code directories should be included. Python installers sometimes pre-set PYTHONPATH. |
2 |
PYTHONSTARTUP An initialization file containing Python source code is contained in this file. Every time you start the interpreter, it is executed. In Unix, it is called .pythonrc.py and contains commands to load utilities and modify PYTHONPATH. |
3 |
PYTHONCASEOK In Windows, it instructs Python to find the first case-insensitive match in an import statement. To activate this variable, set it to any value. |
4 |
PYTHONHOME This is an alternative path for searching modules. To make switching module libraries easy, it is usually included in the PYTHONSTARTUP or PYTHONPATH directories. |
Running Python
There are three different ways to start Python −
Interactive Interpreter
You can start Python from Unix, DOS, or any other 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
Here is the list of all the available command line options −
Sr.No. | Option & Description |
---|---|
1 |
-d It provides debug output. |
2 |
-O It generates optimized bytecode (resulting in .pyo files). |
3 |
-S Do not run import site to look for Python paths on startup. |
4 |
-v verbose output (detailed trace on import statements). |
5 |
-X disable class-based built-in exceptions (just use 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 command line by invoking the interpreter on your application, as in the following −
$python script.py # Unix/Linux or python% script.py # Unix/Linux or C: >python script.py # Windows/DOS
Note − Be sure the file permission mode allows execution.
Integrated Development Environment
If your system supports Python, you can also run Python from a Graphical User Interface (GUI).
-
Unix − IDLE is the first Python IDE for Unix.
-
Windows − PythonWin is the first GUI-enabled IDE for Python on Windows.
-
Macintosh − Python and the IDLE IDE are available from the main website as MacBinary or BinHex files.
You can ask your system administrator for help if you are unable to set up the environment properly. Ensure the Python environment is properly configured and working.
Please note that all examples in the subsequent chapters are based on Python 2.4.3, which is available on CentOS.
Python Programming environment is already set up online, so you can execute all the available examples at the same time as you are learning theory. You are welcome to modify any example and run it online.
Frequently Asked Questions
Recommended Posts:
- Python Object Oriented
- Python Regular Expressions
- Python CGI Programming
- Python MySQL Database Access
- Python Networking Programming
- Python Sending Email using SMTP
- Python Multithreaded Programming
- Python XML Processing
- Python GUI Programming (Tkinter)
- Python Extension Programming with C
- Python Quick Guide
- Python Tools/Utilities
- Python Useful Resources
- Discuss Python
- Python - DS Home