In this chapter we will introduce to you how to set up a Python development environment locally.
Python can be used on multiple platforms including Linux and Mac OS X. General Linux distributions come with Python, and the latest version of Mac OS X also comes with Python, which means it is already installed and does not need to be configured.
Download the latest version of Python 2.7.9 directly under Windows. Please select
when installing. You can enter the "python" command through the terminal window to check whether Python has been installed locally and the installation version of Python.
Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, SunOS, IRIX, etc.)
Win 9x/NT/2000
Macintosh (Intel, PPC, 68K)
OS/2
DOS (Multiple DOS versions)
PalmOS
Nokia Mobile Phones
Windows CE
Acorn/RISC OS
BeOS
Amiga
VMS/OpenVMS
QNX
VxWorks
Psion
Python is also portable to Java and .NET virtual machines.
Python download
Python’s latest source code, binary documents, news information, etc. can be viewed on Python’s official website:
Python official website: http://www.python.org/
You can download it at the link below Python documentation, you can download documentation in HTML, PDF and PostScript formats.
Python document download address: www.python.org/doc/
Python installation
Python has been ported on many platforms (with modifications to enable it to work on different platforms).
You need to download the binary for the platform you are using and then install Python.
If the binary code for your platform is not available, you need to manually compile the source code using a C compiler.
Compiled source code has more selectivity in functions and provides more flexibility for python installation.
The following are how to install Python on different platforms:
Installing Python on Unix & Linux platforms:
The following are simple steps to install Python on Unix & Linux platforms:
Open a WEB browser and visit http://www. python.org/download/
Select the source code compression package for Unix/Linux.
Download and unzip the compressed package.
If you need to customize some options and modify Modules/Setup
Execute the ./configure script
make
make install
After executing the above operations, Python will be installed in the /usr/local/bin directory, and the Python library will be installed In /usr/local/lib/pythonXX, XX is the version number of Python you are using.
Install Python on Window platform:
The following are simple steps to install Python on Window platform:
Open the WEB browser and visit http://www.python.org/download/
Select Window platform installation in the download list Package, the package format is: python-XYZ.msi file, XYZ is the version number you want to install.
To use the installer python-XYZ.msi, the Windows system must support Microsoft Installer 2.0. Just save the installation file to your local computer and run it to see if your machine supports MSI. Windows XP and later versions already have MSI, and many older machines can also install MSI.
After downloading, double-click the download package to enter the Python installation wizard. The installation is very simple. You only need to use the default settings and click "Next" until the installation is completed.
Install Python on MAC platform:
Recent Macs systems all come with a Python environment, but the Python version that comes with it is an old version. You can check the MAC through the link http://www.python.org/download/mac/ Introduction to the new features of Python.
You can view the complete Python installation tutorial on MAC: http://www.cwi.nl/~jack/macpython.html
Environment variable configuration
Programs and executable files can be found in many Directories, and these paths are most likely not in the search paths provided by the operating system for executable files.
path is stored in an environment variable, which is a named string maintained by the operating system. These variables contain information about available command line interpreters and other programs.
The path variable in Unix or Windows is PATH (UNIX is case-sensitive, Windows is not case-sensitive).
In Mac OS, the installation path of python is changed during the installation process. If you need to reference Python from another directory, you must add the Python directory to the path.
Set environment variables in Unix/Linux
In csh shell: Enter
setenv PATH "$PATH:/usr/local/bin/python" and press "Enter".
In bash shell (Linux): Type
export PATH="$PATH:/usr/local/bin/python" and press "Enter".
In sh or ksh shell: Enter
PATH="$PATH:/usr/local/bin/python" and press "Enter".
Note: /usr/local/bin/python is the installation directory of Python.
Set environment variables in Windows
Add the Python directory in the environment variables:
In the command prompt box (cmd): enter
path %path%;C:Python and press "Enter".
Note: C:Python is the installation directory of Python.
Python environment variables
The following are important environment variables, which apply to Python:
Variable name
Description
PYTHONPATH PYTHONPATH is the Python search path. By default, the modules we import will be searched from PYTHONPATH.
PYTHONSTARTUP After Python starts, it first looks for the PYTHONSTARTUP environment variable, and then executes the execution code specified by the variable in this file.
PYTHONCASEOK Adding the PYTHONCASEOK environment variable will make python case-insensitive when importing modules.
PYTHONHOME Another module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directory, making it easier to switch between the two module libraries.
Running Python
There are three ways to run Python:
1. Interactive interpreter:
You can enter python through the command line window and start writing Python code in the interactive interpreter.
You can do python coding work on Unix, DOS or any other system that provides a command line or shell.
$python # Unix/Linux
C:>python # Windows/DOS
The following are Python command line parameters:
Options
Description
-d Display debugging information during parsing
-O Generate optimized code (.pyo file)
-S Does not introduce the location to find the Python path when starting
-v Output the Python version number
-X Based on built-in exceptions from version 1.6 onwards (only for strings ) is obsolete.
-c cmd Execute the Python script and use the running result as a cmd string.
file Execute python script in the given python file.
2. Command line script
You can execute Python scripts on the command line by introducing an interpreter into your application, as shown below:
$python script.py # Unix/Linux
or
./ script.py # Unix/Linux
or
C:>python script.py # Windows/DOS
Note: When executing the script, please check whether the script has executable permissions.
3. Integrated Development Environment (IDE: Integrated Development Environment)
You can use the graphical user interface (GUI) environment to write and run Python code. The following are recommended IDEs for use on various platforms:
Unix: IDLE is the earliest Python IDE on UNIX.
Windows: PythonWin is a Python integrated development environment, which is better than IDE in many aspects.
Macintosh: Python's Mac can use IDLE IDE, and you can download the corresponding IDLE for MAC on the website.
Before proceeding to the next chapter, please ensure that your environment has been set up successfully. If you are not able to set up the correct environment then you can get help from your system administrator.
The examples given in subsequent chapters have been tested in Python 2.7.6 version under Ubuntu (Linux).