Python for Windows

Содержание

Installation Python for Windows OS

Get Python installer from official site.

The Windows version is provided as an MSI package. To install it manually, just double-click the file. The MSI package format allows Windows administrators to automate installation with their standard tools.

By design, Python installs to a directory with the version number embedded, e.g. C:\Python27\, so that you can have multiple versions of Python on the same system without conflicts. Of course, only one interpreter can be the default application for Python file types. It also does not automatically modify the PATH environment variable, so that you always have control over which copy of Python is run.

Typing the full path name for a Python interpreter each time quickly gets tedious, so add the directories for your default Python version to the PATH. Assuming that your Python installation is in C:\Python27\, add this to your PATH:

C:\Python27\;C:\Python27\Scripts\

You do not need to install or configure anything else to use Python. Having said that, I would strongly recommend that you install the tools and libraries described in the next section before you start building Python applications for real-world use. In particular, you should always install Distribute, as it makes it much easier for you to use other third-party Python libraries.

Briefly, the default Python package automatically provides you with:

  • The Python runtime
  • An interactive shell (use the menu icon, or type python in a command prompt window)
  • A basic IDE, called IDLE
  • A large standard library, along with documentation
  • An extensive tutorial, to help you get started

IDLE is intended as a basic and portable development environment that lets new programmers start without having to learn their way around a large IDE. It’s code completion is not perfect, and it’s integration with the built-in Python debugger currently doesn’t work at all on Windows. If you already use IDEs, it’s probably best to ignore IDLE, and either buy a license for the Wingware IDE, or follow the instructions below to install the free Eclipse IDE. My preferred text editor on Windows is Notepad++, but there are plenty of others that support Python.

Whichever text editor or IDE you use, the supplied tutorial will walk you through the basics of Python. The documentation for the standard library does give simple examples for many features, but it is specifically designed as a reference, rather than for learning. To understand particular features you should start with the supplied tutorial, or one of the books mentioned below.

Essential Tools

There are a number of de-facto standard utilities and libraries for Python software development, but a few tools are so fundamental that you should install them even before you begin to write Python code.

Distribute

The most crucial third-party Python software of all is Distribute, which extends the packaging and installation facilities provided by the distutils in the standard library. Once you add Distribute to your Python system you can download and install any compliant Python software product with a single command. It also enables you to add this network installation capability to your own Python software with very little work.

To obtain the latest version of Distribute for Windows, go to the Distribute page on the Python Package Index Web site. Download the Windows installer and run it.

You can now use the easy_install application in the Scripts subdirectory of your Python installation to get other tools and libraries. Many of the products listed on the Python Package Index site can be installed in this way.

C Extensions Require a Compiler: Setup a C compiler, such as MinGW, if you need to install Python applications with C extensions.

Virtualenv

If you are using Python 2, the next development tool that you should install after Distribute is virtualenv. Python 3 has the functionality of virtualenv built-in, as venv and the pyvenv utility.

To add virtualenv to your Python system, use easy_install:

easy_install virtualenv

The venv system and the virtualenv kit provide the ability to create virtual Python environments that do not interfere with either each other, or the main Python installation. If you use virtual environments from the start you can get into the habit of creating completely clean Python environments for each project. This is particularly important for Web development, where every application will have many dependencies.

To set up a new Python environment with virtualenv, change the working directory to where ever you want to store the environment, and run the virtualenv utility:

virtualenv --distribute --no-site-packages MyNewEnv

To use a virtualenv environment, run the activate.bat batch file in the Scripts subdirectory of that environment. Your command prompt will change to show the active environment. Once you have finished working in the current virtual environment, run the deactivate.bat batch file to restore your settings to normal.

Each new environment automatically includes a copy of easy_install in the Scripts subdirectory, so that you can setup the third-party libraries and tools that you want to use in that environment. Put your own code within a subdirectory of the environment, however you wish. When you no longer need a particular environment, simply copy your code out of it, and then delete the main directory for the environment.

Virtualenv Relies on PATH: The activation batch file adds extra elements to the Windows PATH variable, which can expand the total length of your PATH beyond the permitted size. If your PATH becomes too long then file references will not be resolved correctly, and applications within your virtual Python environment will fail.

A Version Control System

If you do not already use version control, you should also install either Mercurial or Git on your system. The Python project itself uses Mercurial.

Version control is obviously useful for collaboration and protecting your code against loss. In addition, it is key to efficient testing and deployment, particularly for server applications. Once code is version controlled you can synchronize copies across multiple virtualenv environments and computers, deploying or rolling back versions as you require.

Other Popular Tools

A number of other tools are commonly used in Python development, such as Nose for running unit test suites, pychecker for analyzing code quality, and Sphinx for building documentation. You should probably install and learn these as you need them.

Setting up an IDE: Eclipse and PyDev

Eclipse provides a mature and free general-purpose IDE that supports multiple tasks and languages through plugins. The PyDev plugin enables Eclipse to support Python. PyDev itself is also free, but the separate PyDev Extensions plugin is a commercial product. PyDev Extensions provides remote debugging and other advanced capabilities, so it is useful, but not essential.

To run Eclipse you must have a Java runtime (JRE) on your system. If you don’t already have a JRE installed, just go to theJava Website, and follow the prompts.

Next, download a copy of Eclipse from the Eclipse Website. Each of the versions are bundled with particular sets of Java development plugins, but are otherwise the same. There is no installer for Eclipse, you just unzip the provided archive file to a convenient location, such as C:\Program Files\Eclipse\.

To add PyDev, use the plugin management built-in to Eclipse:

1. Choose Help > Software Updates.
2. Select the Available Software tab.
3. Choose Add Site…
4. Enter http://pydev.org/updates in the Location field of the dialog box, and choose OK.
5. Select PyDev for Eclipse, which is listed in the new site entry, under PyDev.
6. Choose Install…
7. Follow the prompts, and agree to the license.

Once you have installed PyDev, specify the Python interpreter:

1. Choose Window > Preferences > Pydev > Interpreter – Python
2. Select New…
3. Browse to the directory that holds your Python interpreter, e.g. C:\Python27\, and select the python.exe file.
4. When the Selection Needed window appears, choose OK.

To run the Python interactive shell within Eclipse, define it as an External Tool:

1. Select Run > External Tools > External Tools…
2. Choose Program > New Launch Configuration…
3. Specify the Python interpreter in the Location field, and enter ${container_loc} in the Working Directory field. Add -i as anArgument.

For a more detailed guide to configuring PyDev, with screenshots, use this page.

Watch this video to get a very quick introduction to using PyDev.

The standard Eclipse builds include support for the CVS version control system. Third-party plugins exist to integrate both Bazaar and Mercurial:

Accessing Databases

The simplest answer: you do not need to install anything or setup a service to create a SQL database for a new Python application, because the Python standard library includes a version of SQLite. To access a SQL database service such as MySQL or Oracle you will need to install the standard client software for that product, along with a separate Python adapter. Both the SQLite library and third-party database adapters follow the Python DB-API specification, which means that you can program with them all in the same way.

Connecting to Microsoft SQL Server: Use the generic ODBC adapter to connect to Microsoft SQL Server databases.

In practice, most Python applications interact with databases through higher level libraries, rather than using the DB-APIadapters directly. SQLAlchemy has become the standard Python library for database access, and it is probably one of the best database toolkit libraries available for any programming language. You may use the declarative portion of SQLAlchemy like a standard Object-Relational Mapper (ORM), but it has many more capabilities.

If you install the Pylons or TurboGears 2 frameworks, SQLAlchemy will automatically be included. To install SQLAlchemy separately, use easy_install:

easy_install sqlalchemy

In either case, SQLAlchemy ultimately relies on the standard client and DB-API adapter for your database. When you use a database other than SQLite, ensure that these are present and current, as low level configuration errors are harder to diagnose when working with SQLAlchemy.

Web Applications

Today, most Python Web applications are built on a framework. Python Web frameworks follow the WSGI standard, which provides consistent interfaces between individual components, and between the components and the host Web server. Any Web server that supports WSGI can host compliant applications.

For small applications, use Flask or Bottle. If you need a full Model-View-Controller framework, try one of the following:

Django is intended for building and managing groups of content-driven sites, rather than for producing individual Web applications. It incorporates custom libraries developed specifically for Django that closely integrate together, rather than using popular Python libraries, such as SQLAlchemy. Pyramid offers a well-engineered and modular framework based on Python standards. TurboGears is a distribution of the same components as Pyramid with additional features for ease-of-use.

If you work with more than one framework, I strongly recommend that you install each framework into a separate virtualenv environment, to avoid conflicts.

Windows Integration

Python is specifically designed to be portable and consistent across operating systems, including obscure platforms. This means that the Python standard library provides support for cross-platform operations, but does not include a full set of specialized features for popular operating systems. To get support for features that are specific to Microsoft Windows, such asCOM and the Registry, install the win32 Extensions.

Similarly, the distutils package in the standard library provides a general-purpose system for building distributable Python packages. To build fully self-contained executable installers for Windows, use py2exe. This method includes a Python interpreter within your application, removing the need to have a separate copy of Python installed on the target system.

If you are specifically interested in developing desktop applications, consider using wxPython, rather than the basic Tk interface toolkit supplied with the standard library. Alternatively, use the GTK+ toolkit.

Learning Resources

The supplied tutorial is pretty good, and enough for a committed student or someone with previous programming experience to learn the essentials. It also has the virtue of always being up to date, whilst third-party documentation becomes obsolete over time.

For a really easy introduction to Python, I would recommend Club ShowMeDo, a service that provides several series of video tutorials.

At the time of writing, I know of two excellent books for Python 2 that are intended for learners:

Core Python Programming is probably the definitive work on Python 2, and effectively explains even the most advanced features and concepts. If you have not programmed before, or are intimidated by thick books, you should probably start with Beginning Python.

If you are using Python 3, try Dive Into Python 3 by Mark Pilgrim.

However you learn Python, you should probably buy Expert Python Programming. This book demonstrates how to develop, test, document, and deploy Python applications using current tools and best practices.

Expert Python Programming includes a chapter on version control with Mercurial. For more information on Mercurial, read theHgInit tutorial, or the more in-depth Definitive Guide To Mercurial.

Leave a Reply

Your email address will not be published. Required fields are marked *