Installation Wiki

Installing Python

From InstallationWiki

Jump to: navigation, search
Python Paste
Official Page
Project Documentation
Download


Source Book
200px-184719494X.jpg
ISBN 978-1-847194-94-7
Publisher Packt Publishing
Author(s) Tarek Ziadé

Contents

[edit] Getting started

Python is good for developers.

No matter what operating system you or your customers are running, it will work. Unless you are coding platform-specific things, or using a platform-specific library, you can work on Linux and deploy on other systems, for example. However, thats not uncommon anymore. (Ruby, Java, and many other languages work in the same way.)

[edit] Installing Python

The Python programming language runs on almost any system such as Linux, Macintosh, and Windows. The distributions are made available by the core team on the main download page of the Python website at: http://www.python.org/download. Other platforms are maintained by the people from the community, and summarized on a dedicated page. (See http://www.python.org/download/other.) Here, you'll probably find the distributions for operating systems that will remind you of your college years, if you are thirty-years old or more.

The main Python implementation is written in the C language and is called CPython. It is the one that majority of people refer to, when they talk about Python. When the language evolves, the C implementation is changed accordingly. Besides C, Python is available in a few other implementations that are trying to keep up with the mainstream. Most of them are a few milestones behind CPython, but provide a great opportunity to use and promote the language in a specific environment.

This tutorial will focus on installing the CPython implementation on Linux, Windows, and Mac OS X.

[edit] Linux Installation

If you are running Linux, you probably have Python installed. So, try to call it from the shell:

 tarek@dabox:~$ python
 Python 2.3.5 (#1, Jul 4 2007, 17:28:59)
 [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
 Type "help", "copyright", "credits" or "license" for more information.

>>>

If the command is found, you will be placed into the interactive shell that comes with Python, represented by the >>> sign. The information about the compiler used to build Python (here GCC) and the target system (Linux) is displayed. If you are using Windows, you will get Microsoft Visual Studio as the compiler. The Python version is also displayed in the result. Make sure you are running the latest stable release.

If it is not the case, you can install several versions of Python on your system without any unexpected interaction. Each Python version will be reachable with its full name, or with the Python command, depending on your path environment:

 tarek@dabox:~$ which python
 /usr/bin/python
 tarek@dabox:~$ python<tab>
 python python2.3 python2.5

python2.4

If the command is not found, which is very uncommon under Linux, you need to install it using the package-management tools for your Linux system, such as apt for Debian, or rpm for Red Hat, or by compiling the sources.

While it is preferable to stick with a package installation, we will now discuss each of the two installation methods (package-managed installation and source installation) in a little more detail. However, the latest Python version might not always be available in your package-management tools as yet.

[edit] Package Installation

Using the Linux package system of the Linux distribution is the common way to install Python, and to make sure that you can easily upgrade it. Depending on your system, you will have to run one of these commands:

  • apt-get install python for Debian-based distributions, such as Ubuntu
  • urpmi python for rpm-based ones, such as Fedora or Red Hat series
  • emerge python for Gentoo

If the latest version does not show up, a manual installation will be needed.

Finally, some extra packages should be installed in order to have a full installation. They are optional and you can work without them. But they are useful if you want to code C extensions, or to profile your programs. The packages that should be installed in order to have a full installation are:

  • python-dev: It contains Python headers needed when the C modules are compiled.
  • python-profiler: It contains non-GPL modules (Hotshot profiler) for full GPL distributions such as Debian or Ubuntu.
  • gcc: It is used to compile extensions that contain C code.

[edit] Compiling the Sources

A manual installation is done with the cmmi process (configure, make, make install sequence) that performs a compilation of Python and deploys it on the system. The latest Python archive can be found on http://python.org/download.

Using wget for downloads:

The wget program, from the Gnu project, is a command line utility that can perform downloads. It is available under all platforms. Under Windows, you can get a binary distribution at: http://gnuwin32.sourceforge.net/packages/wget.htm. On Linux or Mac OS X, it is installable through the package systems such as apt or MacPorts. See http://www.gnu.org/software/wget.

To build Python, we will use make and gcc.

  • make is a program that is used to read configuration files, usually named Makefile, and check that all requirements to compile the program are met. It is also used to drive the compilation. It is invoked with the configure and make commands.
  • gcc is the GNU C Compiler, an open-source compiler widely used to build programs.

Make sure they are both installed on your system. Under some versions of Linux such as Ubuntu, you can install build tools with the build-essentials package.

To build and install Python, run this sequence:

 cd /tmp
 wget http://python.org/ftp/python/2.5.1/Python-2.5.1.tgz
 tar -xzvf tar -xzvf Python-2.5.1.tgz
 cd Python-2.5.1
 ./configure
 make

sudo make install

This installation will also install the headers provided for binary installations that are usually included in the python-dev package. The Hotshot profiler is also bundled into the source releases. The result should be the same when you are done, that is, Python should be reachable in the shell.

At this point, your system is Python-enabled. So, let's celebrate!

[edit] Windows Installation

Python can be compiled on Windows in the same way as for Linux. But this can be quite painful because you will need to set up a complicated compilation environment. Standard installers are provided in the python.org download section, and the wizard to achieve the installation is pretty straightforward.

[edit] Installing Python

If you leave all the options at default, Python will be installed under c:\Python25, and not under the usual Program Files folder. This prevents any space in the path.

The last step is changing your PATH environment variable, so that we can call Python from the DOS shell.

On most Windows installations, this is done by:

  • Right-clicking on the My Computer icon that is located on the desktop or the start menu, to get to the System Properties dialog box
  • Getting in the Advanced tab
  • Clicking on the Environment Variables button
  • Editing the PATH system variable to add two new paths, separated by ";" (a semi-colon)

The paths to be added are:

  • c:\Python25, to be able to call python.exe
  • c:\Python25\Scripts, to be able to call third-party scripts that are installed in your Python by extensions

You should be able to run Python in the Command Prompt. To get there, open the Run shortcut in the Start menu, open cmd, and then call python:

 <code>C:\</code>> python
 Python 2.5.2 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>>

This is enough to run Python. But this environment is not quite complete, when compared to that of a Linux user. To perform everything that is presented in this tutorial, MinGW needs to be installed.

[edit] Installing MinGW

MinGW is a compiler for Windows platforms. It provides the gcc compiler in all flavors, and a set of libraries and headers. MinGW can be used as a full replacement for Microsoft's Visual C++. You could also choose to keep both compilers on your system and use them for different purposes, depending upon your requirements.

To install MinGW, get the distribution from http://sourceforge.net/project/showfiles.php?group_id=2435&package_id=240780. There you will find a link to Sourceforge. (See http://sourceforge.net, the largest developer website for Open Source projects.) The automated installer is the best choice, as everything will be bundled. Get the installer and run it.

Just as for Python, the PATH environment variable in the system properties needs to be extended with c:\MinGW\bin, in order to be able to invoke its commands. You should be able to run MinGW commands from the shell after the path is set:

 C:\>gcc -v
 Reading specs from c:/MinGW/bin/../lib/gcc-lib/mingw32/3.2.3/specs
 Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as --host=
 mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls --enable
 -languages=c++,f77,objc --disable-win32-registry --disable-shared --enable-sjlj-
 exceptions
 Thread model: win32
 gcc version 3.2.3 (mingw special 20030504-1)

These commands will never be run manually, but are used automatically by Python when a compiler needs to be used.

[edit] Installing MSYS

Another tool that should be installed under Windows is MSYS (Minimal SYStem). It provides a Bourne Shell command-line interpreter environment under Windows that provides all the usual commands Linux or Mac OS X has, such as cp, rm and so on.

This may sound overkill, since Windows has the same set of tools whether they are graphical or available in an MS-DOS prompt. But this helps the developers who work on several systems to have a universal set of commands to work with.

Get the download link for MSYS from http://sourceforge.net/project/showfiles.php?group_id=2435&package_id=240780 and install it on your system.

If you perform a standard installation, MSYS will be installed in c:\msys. You must add C:\msys\1.0\bin in your PATH variable in the same way as you added MinGW.

If you are under Windows, you should install MSYS.

Now that you have MinGW and MSYS, there's no need to be jealous of those with a Linux installation anymore, since they implement in your system the most important parts of a Linux development environment.

[edit] Mac OS X Installation

Mac OS X is based on Darwin, which in turn is based on FreeBSD. This makes the platform quite similar and compatible to Linux. Apple, on the top of it, added a graphical engine (Quartz) and a specific file tree.

From the shell point of view, the major difference is how the system tree is organized. You will not find, for example a /home root folder, but you can find a /Users folder. The applications are also usually installed in /Library. /usr/bin is used though, as it is used on Linux.

Just as for Linux and Windows, there are two ways you can install Python on Mac OS X. You can install it using a package installer, or you can compile it from the source. The package installation is the simplest way, but you might want to build Python yourself. However, the latest version might not be available yet, as a binary distribution.

[edit] Package Installation

The latest Mac OS X version (Leopard at this time) comes with an installed Python. To install an extra Python, get a universal binary at http://www.pythonmac.org/packages for Python 2.5.x. You will get a .dmg file that you can mount. It contains a .pkg file that you can launch.

This will install Python in the /Library folder and create the proper links in the system so you can run it from the shell.

[edit] Compiling the Source

To compile Python, you need to install:

  • The gcc compiler: It is provided in the Xcode Tools, and is available on the install disk or online at: http://developer.apple.com/tools/xcode.
  • MacPorts: This is a package system comparable to Debian's package-management system apt that will help you install dependencies, for instance the same way Linux users can with apt. See http://www.macports.org.

From here, you can follow the same process explained for compiling under Linux.

[edit] The Python Prompt

The Python prompt, which comes when the python command is called, allows you to interact with the interpreter. It is very common, for example, to use it as a small calculator:

 macziade:/home/tziade tziade$ python
 Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
 [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
 Type "help", "copyright", "credits" or "license" for more information.
 >>>1 + 3
 4
 >>>5 * 8
 40

When the enter key is hit, the line is interpreted and the result is immediately displayed. This particularity, inherited from the ABC language, affects the way Python the programmers work. In code documentation, all usage examples are shown in small prompt sessions.

Getting out of the prompt:

To get out of the prompt, use Ctrl+D under Linux or Mac OS X, and Ctrl+Z under Windows.

Since the prompt interactive mode will play an important role in the coding process, we need to make it very easy to use.

[edit] Customizing the Interactive Prompt

The interactive prompt can be configured with a startup file. When it starts, it looks for the PYTHONSTARTUP environment variable and executes the code in the file pointed to by this variable. Some Linux distributions provide a default startup script, which is generally located in your home directory. It is called .pythonstartup. Tab completion and command history are often provided to enhance the prompt, and are based on the the readline module. (You need the readline library.) If you don't have such a file, you can easily create one.

Here's an example of the simplest startup file that adds completion with the <Tab> key, and history:

 # python startup file
 import readline
 import rlcompleter
 import atexit
 import os
 # tab completion
 readline.parse_and_bind('tab: complete')
 # history file
 histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
 try:
  readline.read_history_file(histfile)
 except IOError:
  pass
 atexit.register(readline.write_history_file, histfile)
 del os, histfile, readline, rlcompleter

Create this file in your home directory and call it .pythonstartup. Then add a PYTHONSTARTUP variable in your environment using the path of your file.

The python script is available in the pbp.script package under the 'pythonstartup.py' name. You can get this file at http://pypi.python.org/pypi/pbp.scripts and rename it to '.pythonstartup'
Setting up the PYTHONSTARTUP environment variable:

If you are running Linux or Mac OS X, the simplest way is to create the startup script in your home folder. Then link it with a PYTHONSTARTUP environment variable set into the system shell startup script. For example, Bash and Korn shell use the .profile file, where you can insert a line such as:

export PYTHONSTARTUP=~/.pythonstartup

If you are running Windows, it is easy to set a new environment variable as an administrator in the system preferences, and save the script in a common place instead of using a specific user location.

When the interactive prompt is called for, the .pythonstartup script should be executed, and the new functionalities made available. For instance, tab completion is really useful to recall module contents:

 >>> import md5
 >>> md5.<tab>
 md5.__class__ md5.__file__ md5.__name__ md5.__repr__ md5.digest_size
 md5.__delattr__ md5.__getattribute__ md5.__new__ md5.__setattr__ md5.md5
 md5.__dict__ md5.__hash__ md5.__reduce__ md5.__str__ md5.new
 md5.__doc__ md5.__init__ md5.__reduce_ex__ md5.blocksize

You can adapt the script for more automation, as Python provides an entry point with a module. Further, a module provides the interpreter base classes. (See the code module at: http://docs.python.org/lib/module-code.html.) But if you want an advanced interactive prompt, you can use an existing tool: iPython.

[edit] iPython: An Advanced Prompt

iPython (http://ipython.scipy.org) is a project aiming to provide an extended prompt. Among the features provided, the most interesting ones are:

  • Dynamic object introspection
  • System shell access from the prompt
  • Profiling direct support
  • Debugging facilities

See the full list at: http://ipython.scipy.org/doc/manual/index.html.

To install iPython, go to the download page http://ipython.scipy.org/moin/Download and follow the instructions in accordance with your platform.

The iPython shell in action looks like this:

 tarek@luvdit:~$ ipython
 Python 2.4.4 (#2, Apr 5 2007, 20:11:18)
 Type "copyright", "credits" or "license" for more information.
 IPython 0.7.2 -- An enhanced Interactive Python.
 ? -> Introduction to IPython's features.
 %magic -> Information about IPython's 'magic' % functions.
 help -> Python's own help system.
 object? -> Details about 'object'. ?object also works, ?? prints more.
 In [1]:
iPython and application debugging:

iPython is a friendly prompt when it comes to debugging, especially for server-side code that runs daemonized.

[edit] Installing setuptools

Perl has a great collection of third-party libraries, and a simple way to install them. The Perl CPAN system lets any developer publish a new library with a simple set of commands. A similar technology has been used in the Python world for the past few years, and is becoming the standard way to install extensions. It is based on:

  • A centralized repository on Python's official website called the Python Package Index (PyPI), which was formerly the Cheeseshop (with reference to a Monty Python sketch from the BBC)
  • A packaging system called setuptools that is based on distutils, to deliver the code in archives and interact with PyPI

Before installing these extensions, a few explanations are necessary to get the whole picture.

[edit] Understanding How It Works

Python comes with a module called distutils that provides a set of tools to distribute your Python applications. It provides the following:

  • A skeleton to provide standard metadata fields such as the author name, the license type, and many others
  • A set of helpers who know how to build a distribution over the code of a package (in Python, a package is a system folder containing one or more modules) and let you create either a set of pre-compiled python files, or a real installer for Windows.

But distutils is limited to the package, and doesn't provide a way to define its dependencies over other packages. setuptools enhances this by adding a basic dependency system and a lot of other features. It also provides an automatic package finder that knows how to fetch dependencies, and install them automatically. In other words, setuptools is to Python what apt is to Debian.

This tool has become very popular, and is now almost mandatory when writing Python applications that are meant to be distributed to others. It will hopefully be integrated in the standard library that comes with Python within the next few years. Until then, if you want a fully-enabled Python system for yourself with all the power of setuptools, you will need to separately install setuptools. This is because it is not yet a part of the standard Python install.

[edit] setuptools Installation Using EasyInstall

To install setuptools, you need to install EasyInstall, which is a package downloader and installer. This program is complementary to setuptools because it knows how to handle packages built with it. Installing it will also install setuptools.

Download and run the ez_setup.py script provided on Peak's website. You can find it on http://peak.telecommunity.com/DevCenter/EasyInstall, and its location is usually http://peak.telecommunity.com/dist/ez_setup.py:

 macziade:~ tziade$ wget http://peak.telecommunity.com/dist/ez_setup.py
 08:31:40 (29.26 KB/s) - ez_setup.py saved [8960/8960]
 
 macziade:~ tziade$ python ez_setup.py setuptools
 Searching for setuptools
 Reading http://pypi.python.org/simple/setuptools/
 Best match: setuptools 0.6c7
 ...
 Processing dependencies for setuptools
 Finished processing dependencies for setuptools

If you have a previous installation, you will get a warning, and you will need to use the upgrade option (-U setuptools):

 macziade:~ tziade$ python ez_setup.py
 Setuptools version 0.6c7 or greater has been installed.
 (Run "ez_setup.py -U setuptools" to reinstall or upgrade.)
 
 macziade:~ tziade$ python ez_setup.py -U setuptools
 Searching for setuptools
 Reading http://pypi.python.org/simple/setuptools/
 Best match: setuptools 0.6c7
 ...
 Processing dependencies for setuptools
 Finished processing dependencies for setuptools

When everything is installed, a new command is available on your system called easy_install. Any installation or upgrade of an extension will be done through this command. For example, if the py.test extension (which is a set of tools to practice agile development; see http://codespeak.net/py/dist) needs to be installed, you can run the following code:

 tarek@luvdit:/tmp$ sudo easy_install py
 Searching for py
 Reading http://cheeseshop.python.org/pypi/py/
 Reading http://codespeak.net/py
 Reading http://cheeseshop.python.org/pypi/py/0.9.0
 Best match: py 0.9.0
 Downloading http://codespeak.net/download/py/py-0.9.0.tar.gz
 ...
 Installing pytest.cmd script to /usr/local/bin
 Installed /usr/local/lib/python2.3/site-packages/py-0.9.0-py2.3.egg
 Processing dependencies for py
 Finished processing dependencies for py

If you are under Windows, the script is called easy_install.exe, and is located in the Scripts folder of your Python installation. So as long as this folder, similar to the one configured in the Windows installation section, is in your PATH, you will be able to call it with easy_install as well (without the sudo prefix that is used to have root privileges under Linux and Mac OS X).

This tool makes it really easy to extend Python, as every dependency is automatically installed. If an extension needs to be compiled when you are under Windows, an extra step is needed for MinGW to be automatically called.

[edit] Hooking MinGW into distutils

When a compilation is needed, a compiler can be indicated to Python with a configuration file. This has to be done explicitly under Windows. Create a new file called distutils.cfg, in the python-installation-path\lib\distutils folder (Lib folder comes with a capital L under Windows) with the following content:

 [build]
 compiler = mingw32

This will link MinGW and Python, so that every time Python builds a package that has some C code inside, it will use MinGW transparently.

Lastly, to complete the prompt, the developers can use:

  • A classical code editor such as Vim or Emacs, or any other can be used as long as it provides a friendly mode for Python code. This editor has to be completed with a set of tools.
  • In Integrated Development Environment that integrates everything can be used. Eclipse with PyDev is the best pick at this time.

[edit] Additional References

  • For instructions on Creating template for Python package, click here
  • For instructions on Setting up Working Environment for Python, click here

[edit] Source

The source of this content is Chapter 1: Getting started of Expert Python Programming by Tarek Ziadé (Packt Publishing, 2008).

Personal tools