Python basic in...login
Python basic introductory tutorial
author:php.cn  update time:2022-04-18 16:14:50

Python environment setup


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.

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 version)

  • PalmOS

  • Nokia Mobile Phone

  • 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 Python documentation in the following link. You can download documentation in HTML, PDF, PostScript and other formats.

Python document download address: www.python.org/doc/



Python installation

Python has been ported to many platforms (with changes to enable it to work on different platforms).

You need to download the binary code for the platform you are using, and then install Python.

If binary code is not available for your platform, you need to manually compile the source code using a C compiler.

The 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 the WEB browser and visit http://www.python.org/download/

  • Select the source code compression package suitable for Unix/Linux .

  • Download and decompress the compressed package.

  • If you need to customize some options, modify

    Modules/Setup

  • Execute ./ configure script

  • make

  • make install

After performing the above operations, Python will be installed in In the /usr/local/bin directory, the Python library is installed in /usr/local/lib/pythonXX, where XX is the version number of Python you are using.

Installing Python on Window platform:

The following are simple steps to install Python on Window platform:

  • Open a WEB browser and visit http:// www.python.org/download/

  • Select the Window platform installation package in the download list. The package format is:

    python-XYZ.msi file, XYZ for you The version number 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.

Installing Python on MAC platform:

Recent Macs systems all come with a Python environment. You can also download it at the link http://www.python.org/download/ Install the latest version.



Environment variable configuration

Programs and executable files can be in many directories, and these paths are likely not in the search path provided by the operating system for executable files.

Path (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

  • Enter
    ## in csh shell:

#setenv PATH "$PATH:/usr/local/bin/python"
  • , press "Enter".

  • In bash shell (Linux): Enter

##export PATH="$PATH :/usr/local/bin/python"
    , press "Enter".
  • In sh or ksh shell:

    enter

PATH="$PATH:/ usr/local/bin/python"
    , 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

Press "Enter".

Note: C:\Python is the installation directory of Python.

You can also set it in the following ways:

  • Right-click "Computer" and then click "Properties"

  • Then click "Advanced System Settings"

  • Select "Path" under the "System Variables" window and double-click it!

  • Then in the "Path" line, just add the python installation path (my D:\Python32), so later, just add the path. ps: Remember, the path is directly separated by a semicolon ";"!

  • #After the final setting is successful, enter the command "python" on the cmd command line to see the relevant display.

1025.png


Python environment variables

The following are important environment variables, which apply to Python:

Variable nameDescription
PYTHONPATHPYTHONPATH is the Python search path, which defaults to the module we import It will be searched from PYTHONPATH.
PYTHONSTARTUPAfter 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.
PYTHONHOMEAnother 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 through the command line window python and start writing Python code in the interactive interpreter.

You can perform python coding work on Unix, DOS or any other system that provides a command line or shell. python Line arguments:



Option

Description

-dShow debugging while parsing INFOGenerate optimized code (.pyo file)Start When not introducing the location to find the Python path##-VOutput the Python version number-XBuilt-in based exceptions (only for strings) are obsolete from version 1.6 onwards. -c cmdExecute the Python script and use the running result as a cmd string. fileExecute a python script in the given python file. You can execute Python scripts on the command line by introducing an interpreter into your application, as shown below:$ python script.py # Unix/LinuxorC:>python script.py # Windows/DOS
-O
-S
2. Command line script

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 each platform:

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:

    IDLE IDE can be used for Python on Mac. You can download IDLE for MAC on the website.
  • Before continuing to the next chapter, please make sure 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.