Python Basics--windows using Python

Source: Internet
Author: User
Tags python script shebang

Unlike most UNIX systems and services, Windows does not need Python locally, so a version of Python is not preinstalled. However, the CPython team has compiled the Windows Installer (MSI package) for each version for many years.

As Python continues to evolve, some previously supported platforms are no longer supported (due to lack of users or developers). Check the PEP For more information about all unsupported platforms.

    • Windows CE is still supported.
    • The installer in Cygwin provides the installation of the Python interpreter as well (see the source of the Cygwin package, the maintainer's version)

For more information about the platform that has the precompiled installer, see Python for Windows.

You can also see

Python on XP
"7 minutes to" Hello World! "by Richard Dooling,2006 year
Installing on Windows
In the "Dive into Python:python from novice to Pro" by Mark PILGRIM,2004,ISBN 1-59059-356-1
For Windows users
in "Install Python" in "A-byte python" by Swaroop ch,2003
1.1 Optional bundle

In addition to the standard CPython distribution, there are modification packages that contain additional functionality. The following is a list of popular versions and their main features:

ActivePython
The installer has multiple platform compatibility, documentation, PYWIN32
Enttought python distribution
popular modules (such as PYWIN32) and their respective documentation for building a tool suite for extensible Python Applications

Note that these packages are likely to install older versions of Python.

1.2 Configuration of Python

In order to run Python flawlessly, you may need to change some environment settings in Windows.

1.2.1 PostScript: Setting environment variables

Windows has a built-in dialog box for changing environment variables (the following guidelines apply to XP Classic View): Right-click the computer's icon (usually on the desktop and call My Computer), and then select Properties. Then, open the Advanced tab and click the Environment Variables button.

In short, your path is:

My computer? Properties-Advanced? Environment Variables

In this dialog box, you can add or modify user and system variables. To change a system variable, you need unrestricted access to the computer (that is, Administrator privileges).

Another way to add a variable to your environment is to use the set command:

Set PYTHONPATH =%pythonpath%; C \ My_python_lib

To make this setting permanent, you can add the appropriate command line to your autoexec.bat . msconfig is the graphical interface for this file.

Viewing environment variables can also be performed more directly: the command prompt will automatically extend the string that is wrapped as a percent semicolon:

echo%path%

Consultation set/? For more information about this behavior.

You can also see

http://support.microsoft.com/kb/100843
Environment variables in Windows NT
http://support.microsoft.com/kb/310519
How to manage environment variables in Windows XP
Http://www.chem.gla.ac.uk/~louis/software/faq/q1.html
setting environment variables, Louis J. Farrugia
1.2.2 Find Python executable program

Change version 3.3.

In addition to using the Start menu item for the automatically created Python interpreter, you may need to start Python at the command prompt. Starting with Python 3.3, the installer has an option for you to set.

On the Customize Python 3.3 screen, you can enable an option called Add Python.exe to search path to have the installer put your installer in %PATH% . This allows you to type python to run the interpreter. Therefore, you can also use command line options to execute a script, see command lines.

If this option is not enabled at installation time, you can rerun setup at any time to make a selection.

Another way is to manually modify %PATH% the instructions in using Excursus: Set environment variables. You need to set your %PATH% environment variables to include the Python bundle's directory, separated by semicolons from other entries. An example variable might look like this (assuming the first two entries are the default values for Windows):

C: \ WINDOWS \ system32; C: \ WINDOWS; C \ Python33
1.2.3 Find Module

Python typically stores its libraries (and thus your Site-packages folder) in the installation directory. So, if you have already installed Python C:\Python\ , the default library will reside in a C:\Python\Lib\ third-party module that should be stored in C:\Python\Lib\site-packages\ .

How this is sys.path populated in Windows:

  • Add an empty entry at the beginning that corresponds to the current directory.
  • If an environment variable PYTHONPATH exists, as described in the environment variable, its entry is added to the next. Note that on Windows, the paths in this variable must be separated by semicolons to distinguish them from C:\ the colon used in the drive identifiers (and so on).
  • Other application paths can be added in the registry as sub-keys under sub-keys and sub-options. A semicolon-delimited path string as a subkey of its default value causes each path to be added. (Note that all known installers use only HKLM, so HKCU is usually empty.) )\SOFTWARE\Python\PythonCore\version\PythonPathHKEY_CURRENT_USERHKEY_LOCAL_MACHINEsys.path
  • If the environment variable is PYTHONHOME set, it is assumed to be "Python Home". Otherwise, the path to the main Python executable file is used to locate a "landmark file" ( Lib\os.py ) to derive "Python Home". If a python home is found, the associated subdirectories are added to sys.path ( Lib , plat-win etc.) on that folder. Otherwise, the core Python path is constructed from the Pythonpath stored in the registry.
  • If the Python home cannot be found, is not PYTHONPATH specified in the environment, and the registry entry is not found, the default path with relative entries (for example .\Lib;.\plat-win , etc.) is used.

The end result of all this is:

    • When running python.exe or any other. exe (installed version, or directly from the Pcbuild directory) in the main Python directory, the core path is deduced, and the core path in the registry is ignored. The other "application Paths" in the registry are always read.
    • When Python is hosted on another. exe (different directories, through COM embedding, etc.), "Python Home" is not deduced, so use the core path from the registry. The other "application Paths" in the registry are always read.
    • If Python cannot find its home without a registry (for example, a frozen. exe, some very strange setup settings), you get a path with some default but relative path.
1.2.4 Execute Script

Starting with Python 3.3, Python includes a launcher that helps you run Python scripts. For more information, see Python Launcher for Windows.

1.2.5 Execute script without Python launcher

Without the Python launcher installed, the Python script (the file with the extension .py ) will be executed by Python.exe by default. This executable opens a terminal that remains open even if the program uses a GUI. If you do not want this to happen, use the extension .pyw , which will cause the script to be Pythonw.exe by default (two executables are located at the top level of the Python installation directory). This disables the terminal window at startup.

You can also .py use Pythonw.exe to execute all scripts, for example (you might need administrative permissions) to set it up with common tools:

    1. Start a command prompt.

    2. Associate the correct filegroup with the .py script:

      Assoc. py = Python.file
    3. Redirect all Python files to the new executable file:

      Ftype python.file = c \ Path \ to \ Pythonw.exe "%1"%*
1.3 Python Startup for Windows

New version 3.3.

The Python launcher for Windows is a utility that helps you locate and execute different versions of Python. It allows the script (or command line) to indicate preferences for a particular Python version and will locate and execute that version.

1.3.1 Getting Started 1.3.1.1 from the command line

You should make sure that the launcher is in your path-depending on its installation it may already be there, but check it out just in case it is not.

From a command prompt, execute the following command:

Py

You should find that the latest version of Python 2.x you have installed has started-it can exit normally, and any additional command line parameters specified will be sent directly to Python.

If you have more than one version of Python 2.x (for example, 2.6 and 2.7), you will notice that Python 2.7 is started-to start Python 2.6, try the following command:

py-2.6

If you have Python 3.x installed, try the following command:

Py-3

You should find the latest version of the Python 3.x launcher.

1.3.1.2 from Script

Let's create a test Python script-Create a hello.py file that is called with the following content

#! Python import syssys.stdout.write ("Hello from python%s \ n"% (Sys.version,))

From the directory where the hello.py is located, execute the command:

PY hello.py

You should note that the version number of the latest Python 2.x installation is printed. Now try to change the first behavior:

#! Python3

Re-executing the command should now print the latest Python 3.x information. As with the command-line example above, you can specify a more explicit version qualifier. Suppose you install Python 2.6 and try to change the first line, you should find the 2.6 version of the printed information.#! python2.6

1.3.1.3 From file association

The startup program should have been associated with Python files (that is,,, .py .pyw .pyc .pyo files) when installed. This means that when you double-click one of the files from Windows Explorer, the Launcher is used, so you can use the same tools described above to have the script specify the version that should be used.

The main benefit of this is that a single initiator can support multiple versions of Python at the same time, depending on the contents of the first line.

1.3.1.4 Shebang Line

If the first line of the script file starts #! , it is called the "shebang" line. Linux and other Unix-like operating systems natively support such rows and are typically used on such systems to indicate how the script is executed. This initiator allows the same facility to use Python scripts on Windows, the example above demonstrates their usage.

To allow shebang lines in a Python script to be ported between UNIX and Windows, this initiator supports multiple "virtual" commands to specify the interpreter to use. The supported virtual commands are:

    • /usr/bin/env python
    • /usr/bin/python
    • /usr/local/bin/python
    • python

For example, if the first line of the script

#! /Usr/bin/python

The default python will be positioned and used. Since many of the Python scripting work already has this line on UNIX, you should find that these scripts can be used by the initiator without modification. If you are writing a new script on windows and you want to be useful on UNIX, you should start /usr with a shebang line .

1.3.1.5 parameters in a shebang row

The Shebang line can also specify additional options to pass to the Python interpreter. For example, if you have a shebang line:

#! /usr/bin/python-v

Python will then start using this -v option

1.3.2 Custom 1.3.2.1 with INI file customization

The two. ini files will be py.ini returned by the application Data directory of the current user (that is, by calling the directory with csidl_local_appdata Windows functions SHGetFolderPath) and py.ini The initiator search in the same directory as the initiator. The same. ini file is used for the ' console ' version of the launcher (i.e., py.exe) and the ' Windows ' version (that is, Pyw.exe)

The customizations specified in the application directory take precedence over the one next to the executable file, so users who may not have write access to the. ini file next to the initiator can overwrite the commands in the global. ini file)

1.3.2.2 Customizing the default Python version

In some cases, you can include version qualifiers in the command to indicate which version of Python the command will use. The version qualifier begins with the major version number, optionally followed by a period ('. ') and minor version specifiers. If you specify a minor qualifier, you can optionally be followed by "32" to indicate that the 32-bit implementation of this version is used.

For example, the shebang row #!python does not have a version qualifier, and #!python3 a version qualifier specifies only the major version.

If the version qualifier is not found in the command, you PY_PYTHON can set the environment variable to specify the default version qualifier-The default value is "2". Note that this value can specify only the major version (for example, "2") or the Major.minor qualifier (for example, "2.6"), or even major.minor-32.

If the minor version qualifier is not found, you can set the environment variable, PY_PYTHON{major} which {major} is the current major version qualifier identified above, to the specified full version. If no such option is found, the initiator enumerates the version of Python that has been installed and uses the latest minor version found for the major version, although not guaranteed, but probably the most recently installed version in the series.

On 64-bit Windows that have the same (Major.minor) Python version of 32-bit and 64-bit implementations installed, the 64-bit version will always be preferred. This is true for both 32-bit and 64-bit initiator implementations, and the--32 bit launcher will prefer to perform a specified version of the 64-bit Python installation, if available. This is because the behavior of the initiator can be predicted only to know what version is installed on the PC, regardless of the order in which they are installed (i.e., do not know whether the 32 or 64-bit version of Python and the corresponding launcher is last installed). As mentioned above, you can change this behavior by using the optional "32" suffix on the version specifier.

Example:

  • If there is no relevant option set, the command python and python2 will use the latest Python 2.x version installed, and the command python3 will be used to install the latest Python 3.x.
  • Command python3.1 and python2.7 will not consult any options because the version is fully specified.
  • If PY_PYTHON=3 both commands python and python3 both will use the latest installed version of Python 3.
  • If PY_PYTHON=3.1-32 the command python will use 3.1 of the 32-bit implementations, the command python3 will use the most recently installed Python (Py_python is not considered as a major version at all as specified. )
  • If PY_PYTHON=3 and PY_PYTHON3=3.1 , commands python and python3 will all be used in special 3.1

In addition to environment variables, you can use them in the initiator. INI file, configure the same settings. Call the section in the INI file, [defaults] and the key name will be the same as the PY_ environment variable without a leading prefix (and note that the key names in the INI file are not case-sensitive.) The contents of the environment variable will overwrite the specified in the INI file.

For example:

    • The settings PY_PYTHON=3.1 are equivalent to the INI file, which contains:
[Default]python = 3.1
    • Set PY_PYTHON=3 and PY_PYTHON3=3.1 equivalent to the INI file contains:
[Default]python = 3python3 = 3.1
3.4.5. Diagnosis

If PYLAUNCH_DEBUG an environment variable (to any value) is set, the initiator prints diagnostic information to stderr (that is, to the console). While this information is verbose and concise, it should allow you to see what the Python version is, why you choose a specific version, and the exact command line used to execute the target python.

1.4 Additional Modules

Even though the goal of Python is to migrate across all platforms, there are some features that are unique to Windows. There are several modules that use these features in standard libraries and external and fragment.

Windows-specific standard modules are documented in MS Windows-specific services.

1.4.1 PyWin32

The PyWin32 module is a collection of advanced Windows-specific support modules by Mark Hammond. This includes the following tools:

    • Component Object Model (COM)
    • Win32 API calls
    • Registration Form
    • Event Log
    • Microsoft Foundation Class (MFC) user interface

Pythonwin is an example MFC application that is included with PyWin32. It is an embeddable IDE with a built-in debugger.

You can also see

Win32: How can I ...?
Tim Golden
Python and COM
by David and Paul Boddie
1.4.2 Cx_freeze

Cx_freeze is an distutils extension (see extension distutils), which wraps Python scripts into executable Windows programs (files). When you do this, you can distribute your app without needing your users to install Python.*.exe

1.4.3 Wconio

Because Python's advanced terminal processing layer is curses limited to UNIX-like systems, there is also a library dedicated to Windows: Windows console I/O for Python.

Wconio is a wrapper for Turbo-c CONIO.H , which is used to create a text user interface.

1.5 Python compiled in Windows

if you want to compile CPython yourself, the first thing you should do is get the source code . You can download the latest version of the source or just grab a new checkout.

The source tree contains the build solution and project files for Microsoft Visual C + +, which is used to build the official Python version of the compiler. View readme.txt in their respective directories:

Catalogue msvc Version Visual Studio Version
PC/VS9.0/ 9.0 2008
PCbuild/ 10.0 2010

Please note that any build directory in the directory is not PC necessarily fully supported. This PCbuild directory contains the files that are used to build the official version of the compiler.

Check PCbuild/readme.txt General information about the build process.

For extension modules, see Building C and C + + extensions on Windows.

You can also see

Python + Windows + distutils + SWIG + gcc MinGW
or "Use Swig to create a Python extension in C/C + + and use MinGW gcc in Windows to compile Python extensions" or sébastiensauvage,2003 year " Use distutils to install Python extensions without using Microsoft Visual C + + "
Mingw-python extension
by Trent Apted and others,
1.6 Other resources

You can also see

Python Programming in Win32
"Help for Windows programmers", Mark Hammond and Andy Robinson,o ' Reilly MEDIA,2000,ISBN 1-56592-621-8
Python for Windows Tutorial
By Amanda Birmingham,2004 Year
PEP 397-Python launcher for Windows
include the initiator's proposal in the Python distribution.

Python Basics--windows using Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.