Python Programming/Getting Python

From Wikibooks, open books for an open world
Jump to: navigation, search

In order to program in Python you need the Python interpreter. If it is not already installed or if the version you are using is obsolete, you will need to obtain and install Python using the methods below:

Contents

[edit] Python 2 vs Python 3

In 2008, a new version of Python (version 3) was published that was not entirely backward compatible. Developers were asked to switch to the new version as soon as possible but many of the common external modules are not yet (as of Aug 2010) available for Python 3. There is a program called 2to3 to convert the source code of a Python 2 program to the source code of a Python 3 program. Consider this fact before you start working with Python.

[edit] Installing Python in Windows

Go to the Python Homepage or the ActiveState website and get the proper version for your platform. Download it, read the instructions and get it installed.

In order to run Python from the command line, you will need to have the python directory in your PATH. Alternatively, you could use an Integrated Development Environment (IDE) for Python like DrPython[1], eric[2], PyScripter[3], or Python's own IDLE (which ships with every version of Python since 2.3).

The PATH variable can be modified from the Window's System control panel. The advanced tab will contain the button labelled Environment Variables, where you can append the newly created folder to the search path.

If you prefer having a temporary environment, you can create a new command prompt short-cut that automatically executes the following statement:

PATH %PATH%;c:\python26

If you downloaded a different version (such as Python 3.1), change the "26" for the version of Python you have (26 is 2.6.x, the current version of Python 2.)

[edit] Cygwin

By default, the Cygwin installer for Windows does not include Python in the downloads. However, it can be selected from the list of packages.

[edit] Installing Python on Mac

Users on Apple Mac OS X will find that it already ships with Python 2.3 (OS X 10.4 Tiger) or Python 2.6.1 (OS X Snow Leopard), but if you want the more recent version head to Python Download Page follow the instruction on the page and in the installers. As a bonus you will also install the Python IDE.

[edit] Installing Python on Unix environments

Python is available as a package for some Linux distributions. In some cases, the distribution CD will contain the python package for installation, while other distributions require downloading the source code and using the compilation scripts.

[edit] Gentoo GNU/Linux

Gentoo is an example of a distribution that installs Python by default - the package system Portage depends on Python.

[edit] Ubuntu GNU/Linux

Users of Ubuntu will notice that Python comes installed by default, only it sometimes is not the latest version. If you would like to update it, click here.

[edit] Arch GNU/Linux

Arch does not install python by default, but is easily available for installation through the package manager to pacman. As root (or using sudo if you've installed and configured it), type:

$ pacman -Sy python

This will be update package databases and install python. Other versions can be built from source from the Arch User Repository.

[edit] Source code installations

Some platforms do not have a version of Python installed, and do not have pre-compiled binaries. In these cases, you will need to download the source code from the official site. Once the download is complete, you will need to unpack the compressed archive into a folder.

To build Python, simply run the configure script (requires the Bash shell) and compile using make.

[edit] Other Distributions

Python, which is also referred to as CPython, is written in the C Programming language. The C source code is generally portable, that means CPython can run on various platforms. More precisely, CPython can be made available on all platforms that provide a compiler to translate the C source code to binary code for that platform.

Apart from CPython there are also other implementations that run on top of a virtual machine. For example, on Java's JRE (Java Runtime Environment) or Microsoft's .NET CLR (Common Language Runtime). Both can access and use the libraries available on their platform. Specifically, they make use of reflection that allows complete inspection and use of all classes and objects for their very technology.

Python Implementations (Platforms)

Environment Description Get From
Jython Java Version of Python Jython
IronPython C# Version of Python IronPython

[edit] Integrated Development Environments (IDE)

CPython ships with IDLE, an Integrated Development Environment built with the tkinter GUI toolkit. IDLE is a is multi-window text editor and debugger, provides syntax highlighting and an interactive shell window, is coded in 100% pure Python and therefore cross-platform (i.e. works on Windows and Unix). The table below lists some IDLE alternatives.

Some Integrated Development Environments (IDEs) for Python

Environment Description Get From
Eclipse Open Source IDE Eclipse
KDevelop Cross Language IDE for KDE KDevelop
ActivePython Highly Flexible, Pythonwin IDE ActivePython
Anjuta IDE Linux/Unix Anjuta
Pythonwin Windows Oriented Environment Pythonwin
VisualWx Free GUI Builder VisualWx
Komodo A Commercial IDE Komodo
BlackAdder Commercial IDE & GUI Builder BlackAdder
Code Crusader Commercial IDE Code Crusader
Code Forge Commercial IDE Code Forge
PyCharm Commercial IDE PyCharm

[edit] Keeping Up to Date

Python has a very active community and language itself evolves continuously. Do frequently visit Python.Org for recent releases and relevant tools. The website is an invaluable asset.

If you want to keep up with newly released third party-modules or software for Python, have a look at Python email list python-announce-list. General discussion can be found at python-list, both of these lists can be found at Python Mail. Usenet users can easily user the newsgroups comp.lang.python.announce & comp.lang.python.

Personal tools
Namespaces

Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export