< BACKMake Note | BookmarkCONTINUE >
152015024128143245168232148039196038240039088173205162105045222218065012140210174141027

Compiling Python

The Python source code distribution comes with complete instructions about how to compile the code for both Windows and UNIX platforms. The instructions about compiling for Windows are stored in the subdirectories /PCbuild and /PC—the UNIX instructions are separate. The /PC subdirectory contains complete project files to make several PC ports of Python.

If you want to compile Python on Windows, you need to start the Integrated Development Environment of your compiler and read in the native project file (or makefile) provided. This enables you to change any source files or build settings so you can make custom builds.

The following are important files that can help you configure the port of the source code to your favorite platform (in case it hasn't been ported yet).

config.h—  An important configuration file specific to PCs. This is generated by the configure script on UNIX systems.

config.c—  The list of C modules to include in the Python PC version. Manually edit this file to add or remove Python modules. This is auto-generated by the makesetup script on UNIX systems.

testpy.py—  A Python test program. Run this to test your Python port. It should produce copious output, ending in a report on how many tests were OK, how many failed, and how many were skipped. Don't worry about skipped tests (these test unavailable optional features).

Windows

The Python distribution comes with instructions to compile the source code using many different tools, such as Microsoft Visual C++ Version 1.5 (16-bit Windows), Watcom C++ compiler, and IBM VisualAge C/C++ for OS/2. See the /PC/readme.txt file in the distribution tree for detailed instructions.

The instructions here cover the Microsoft Visual C++ Versions 5.0 and 6.0.

In order to build Python using this tool, the first thing you need to do is open the workspace pcbuild.dsw that is located under the /PCbuild directory. This directory also contains a readme.txt file with more instructions. Note that all files in this directory are used to build Python for Win32 platforms, which includes Windows 95, 98, and NT platforms.

Then, you just need to select the Debug or Release setting (using Set Active Configuration in the Build menu) and build the projects in the proper order, which is

  1. python15 (this builds python15.dll and python15.lib)

  2. python (this builds python.exe)

  3. The other subprojects

If you select the Debug option, the files will be generated carrying a _d at the end of their name, such as python15_d.dll, python_d.exe, and parser_d.pyd.

We call subprojects—the other applications that don't come as part of the Python application—that include for example: Tcl/Tk, bsddb, and zlib. In order to build these projects, you might have to change some of the settings on your compiler, such as the include and library paths.

The following files and subdirectories of the distribution are useful and helpful when building Win32 versions of Python:

python_nt.rc—  Resource compiler input for python15.dll.

dl_nt.c, import_nt.c—  Additional sources used for 32-bit Windows features.

getpathp.c—  Default sys.path calculations (for all PC platforms).

dllbase_nt.txt—  A (manually maintained) list of base addresses for various DLLs, to avoid runtime relocation.

example_nt—  This is a subdirectory that shows how to build an extension as a DLL.

UNIX

Binary distributions for the UNIX platforms are not made available by the Python Web site because the compilation process depends much on your system. In order to compile Python on a UNIX system, you first need to obtain the source code distribution. Note that the next guidelines are just a summary of the operations that you need to perform. This installation process, by default, installs Python under the /usr/local directory and presumes that you want to provide access to the Tkinter module too. If that's not the case, you can just skip the whole Tcl/Tk/Tkinter steps and install just the core Python environment. You can skip building Tcl and Tk if they are already installed on your system.

  1. Create a root directory with three subdirectories underneath: Python, Tcl, and Tk.

  2. Download the latest versions of these three programs, and save them on the respective subdirectories.

  3. In each directory, you need to extract the files out of the package using a command such as this:

    								
    gunzip -c filename | tar xf -
    
    							
  4. Now, we need to configure and install Tcl and Tk. The actions below (steps 5, 6, and 7) need to be performed in both subdirectories, first for Tcl and later for Tk.

  5. Go to the specific subdirectory.

  6. Define the compiler and system options for the building process. You need to type the following command:

    								
    ./configure
    
    							
  7. Run the utility commands that create and install the binary and library files.

    								
    make
    make install
    
    							
  8. Now, we are ready to install Python. Go to the Python subdirectory.

  9. Define the compiler and system options for the building process. You need to type the following command. Note that optionally, you can enable the threading option by passing the argument --with-thread to the configure command. Also, you might want to enable floating point exception support by passing the argument --enable-fpectl.

    								
    ./configure
    
    							
  10. Copy the Modules/Setup.in file to the subdirectory Modules/Setup.

  11. In order to enable the Tkinter module (because it is not enabled by default), you need to modify this file. This file is responsible for deciding the built-in modules that must be built along with Python. Modifying this file is not difficult, as you can see next. The following piece of file code shows where you need to change in order to enable Tkinter. This example shows what you might want to uncomment because it can vary for different UNIX systems (what you see here should be fine for most systems though).

    								
    # The _tkinter module.
    #
    # The TKPATH variable is always enabled, to save you the effort.
    TKPATH=:lib-tk
    
    # The command for _tkinter is long and site specific.  Please
    # uncomment and/or edit those parts as indicated.  If you don't have a
    # specific extension (e.g. Tix or BLT), leave the corresponding line
    # commented out.  (Leave the trailing backslashes in!  If you
    # experience strange errors, you may want to join all uncommented
    # lines and remove the backslashes -- the backslash interpretation is
    # done by the shell's "read" command and it may not be implemented on
    # every system.
    #***Always uncomment this (leave the leading underscore in!):
    _tkinter _tkinter.c tkappinit.c -DWITH_APPINIT \ 
    #***Uncomment and edit to reflect where your Tcl/Tk headers are:
    -I/usr/local/include \
    #***Uncomment and edit to reflect where your X11 header files are:
    -I/usr/X11R6/include \
    #***Or uncomment this for Solaris:
    #    -I/usr/openwin/include \
    #***Uncomment and edit for Tix extension only:
    #    -DWITH_TIX -ltix4.1.8.0 \
    #***Uncomment and edit for BLT extension only:
    #    -DWITH_BLT -I/usr/local/blt/blt8.0-unoff/include -lBLT8.0 \
    #***Uncomment and edit for PIL (TkImaging) extension only:
    #    -DWITH_PIL -I../Extensions/Imaging/libImaging  tkImaging.c \
    #***Uncomment and edit for TOGL extension only:
    #    -DWITH_TOGL togl.c \
    #***Uncomment and edit to reflect where your Tcl/Tk libraries are:
    -L/usr/local/lib \
    #***Uncomment and edit to reflect your Tcl/Tk versions:
    -ltk8.0 -ltcl8.0 \
    #***Uncomment and edit to reflect where your X11 libraries are:
    -L/usr/X11R6/lib \
    #***Or uncomment this for Solaris:
    #    -L/usr/openwin/lib \
    #***Uncomment these for TOGL extension only:
    #    -lGL -lGLU -lXext -lXmu \
    #***Uncomment for AIX:
    #    -lld  \
    #***Always uncomment this; X11 libraries to link with:
    -lX11
    
    							
  12. Optionally, you can choose for building the modules as shared libraries. It is a good idea to build shared libraries because it will decrease the size of the Python binary, reduce the number of libraries it is linked to, and probably reduce its memory consumption. You need to look for the following lines in the Setup.in file, and make the small change that is indicated there.

    								
    # Uncommenting the following line tells makesetup that all following
    # modules are to be built as shared libraries (see above for more
    # detail; also note that *static* reverses this effect):
    #*shared*
    
    							

    Note

    Note that you can uncomment lines for other modules you want to build, as well.



  13. Now, you just have to run the utility commands that creates and installs the Python executable and library files.

    								
    make
    make install
    
    							
  14. Finally, sometimes you need to set up the environment variables: PATH, PYTHONPATH, TCL_LIBRARY, and TK_LIBRARY to the correct values. In most cases, this shouldn't be necessary because the programs are installed where they expect to be installed. Possibly having to add /usr/local/bin to PATH is probably all that is needed.


Last updated on 1/30/2002
Python Developer's Handbook, © 2002 Sams Publishing

< BACKMake Note | BookmarkCONTINUE >

Index terms contained in this section

/PCbuild directory
/usr/local directory
applications
     source code
            compiling 2nd 3rd
Build menu
      Debug option
building
      modules as shared libraries
code
     source
            compiling 2nd 3rd
compiling
      source code 2nd 3rd
config.c file
config.h file
creating
      modules as shared libraries
Debug option
      Build menu
directories
      /PCbuild
      /usr/local
dl_nt.c file
dllbase_nt.txt file
enabling
      Tkinter module
example_nt subdirectory
files
      config.c
      config.h
      dl_nt.c
      dllbase_nt.txt
      getpathp.c
      import_nt.c
      project
      python_nt.rc
      testpy.py
getpathp.c file
import_nt.c file
libraries
     shared
            building modules as
menus
     Build
            Debug option
modules
      building as shared libraries
      Tkinter 2nd
ports
      testing
programs
     source code
            compiling 2nd 3rd
project file
python_nt.rc file
shared libraries
      building modules as
software
     source code
            compiling 2nd 3rd
source code
      compiling 2nd 3rd
subdirectories
      example_nt
subprojects
testing
      ports
testpy.py file
Tkinter module 2nd
turning on
      Tkinter module
UNIX
     compiling
            source code

© 2002, O'Reilly & Associates, Inc.