< Python Programming

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:

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. Now we are in the era of Python3.6

The latest version of Python 2, i.e. Python 2.7.x, was officially discontinued on 1 January 2020.

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, eric, PyScripter, 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. To add the PATH in Windows 7 :

  1. Go to Start.
  2. Right click on computer.
  3. Click on properties.
  4. Click on 'Advanced System Settings'
  5. Click on 'Environmental Variables'.
  6. In the system variables select Path and edit it, by appending a ';' (without quote) and adding 'C:\python27'(without quote).

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:\python27

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

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.

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.

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.

Gentoo Linux

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

Ubuntu Linux

Users of Ubuntu will notice that Python comes installed by default, only it sometimes is not the latest version. To check which version of Python is installed, type

python -V

into the terminal.

Arch Linux

Arch Linux does not come with Python pre-installed by default, but it 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 -S python

This will be update package databases and install Python 3. Python 2 can be installed with:

pacman -S python2

Other versions can be built from source from the Arch User Repository.

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.

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)

EnvironmentDescriptionGet From
JythonJava Version of PythonJython
IronPythonC# Version of PythonIronPython

Integrated Development Environments (IDE)

CPython ships with IDLE; however, IDLE is not considered user-friendly.[1] For Linux, KDevelop and Spyder are popular. For Windows, PyScripter is free, quick to install, and comes included with PortablePython. The best py used GUI Builder is actually Boa Constructor, but has suffered from bit rot significantly, so it is best to be used in conjunction with LiClipse.

Some Integrated Development Environments (IDEs) for Python

EnvironmentDescriptionGet From
ActivePythonHighly flexible, Pythonwin IDEActivePython
AnjutaIDE Linux/UnixAnjuta
Eclipse (PyDev plugin)Open-source IDEEclipse
EricOpen-source Linux/Windows IDE.Eric
KDevelopCross-language IDE for KDEKDevelop
Ninja-IDECross-platform open-source IDE.Nina-IDE
PyScripterFree Windows IDE (portable)PyScripter
PythonwinWindows-oriented environmentPythonwin
SpyderFree cross-platform IDE (math-oriented)Spyder
VisualWxFree GUI BuilderVisualWx

The Python official wiki has a complete list of IDEs.

There are several commercial IDEs such as Komodo, BlackAdder, Code Crusader, Code Forge, and PyCharm. However, for beginners learning to program, purchasing a commercial IDE is unnecessary.

Trying Python online

You can try Python online, thereby avoiding the need to install. Keywords: REPL.

Links:

Keeping Up to Date

Python has a very active community and the language itself is evolving continuously. Make sure to check python.org for recent releases and relevant tools. The website is an invaluable asset.

Public Python-related mailing lists are hosted at mail.python.org. Two examples of such mailing lists are the Python-announce-list to keep up with newly released third party-modules or software for Python and the general discussion list Python-list. These lists are mirrored to the Usenet newsgroups comp.lang.python.announce & comp.lang.python.

Notes

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.