Python Documentation

Table of Contents

  • What’s New in Python 2.7
    • 1. The Future for Python 2.x
    • 2. Changes to the Handling of Deprecation Warnings
    • 3. Python 3.1 Features
    • 4. PEP 372: Adding an Ordered Dictionary to collections
    • 5. PEP 378: Format Specifier for Thousands Separator
    • 6. PEP 389: The argparse Module for Parsing Command Lines
    • 7. PEP 391: Dictionary-Based Configuration For Logging
    • 8. PEP 3106: Dictionary Views
    • 9. PEP 3137: The memoryview Object
    • 10. Other Language Changes
      • 10.1. Interpreter Changes
      • 10.2. Optimizations
    • 11. New and Improved Modules
      • 11.1. New module: importlib
      • 11.2. New module: sysconfig
      • 11.3. ttk: Themed Widgets for Tk
      • 11.4. Updated module: unittest
      • 11.5. Updated module: ElementTree 1.3
    • 12. Build and C API Changes
      • 12.1. Capsules
      • 12.2. Port-Specific Changes: Windows
      • 12.3. Port-Specific Changes: Mac OS X
      • 12.4. Port-Specific Changes: FreeBSD
    • 13. Other Changes and Fixes
    • 14. Porting to Python 2.7
    • 15. New Features Added to Python 2.7 Maintenance Releases
      • 15.1. PEP 434: IDLE Enhancement Exception for All Branches
      • 15.2. PEP 466: Network Security Enhancements for Python 2.7
      • 15.3. PEP 477: Backport ensurepip (PEP 453) to Python 2.7
        • 15.3.1. Bootstrapping pip By Default
        • 15.3.2. Documentation Changes
      • 15.4. PEP 476: Enabling certificate verification by default for stdlib http clients
      • 15.5. PEP 493: HTTPS verification migration tools for Python 2.7
    • 16. Acknowledgements
  • The Python Tutorial
    • 1. Whetting Your Appetite
    • 2. Using the Python Interpreter
      • 2.1. Invoking the Interpreter
        • 2.1.1. Argument Passing
        • 2.1.2. Interactive Mode
      • 2.2. The Interpreter and Its Environment
        • 2.2.1. Source Code Encoding
    • 3. An Informal Introduction to Python
      • 3.1. Using Python as a Calculator
        • 3.1.1. Numbers
        • 3.1.2. Strings
        • 3.1.3. Unicode Strings
        • 3.1.4. Lists
      • 3.2. First Steps Towards Programming
    • 4. More Control Flow Tools
      • 4.1. if Statements
      • 4.2. for Statements
      • 4.3. The range() Function
      • 4.4. break and continue Statements, and else Clauses on Loops
      • 4.5. pass Statements
      • 4.6. Defining Functions
      • 4.7. More on Defining Functions
        • 4.7.1. Default Argument Values
        • 4.7.2. Keyword Arguments
        • 4.7.3. Arbitrary Argument Lists
        • 4.7.4. Unpacking Argument Lists
        • 4.7.5. Lambda Expressions
        • 4.7.6. Documentation Strings
      • 4.8. Intermezzo: Coding Style
    • 5. Data Structures
      • 5.1. More on Lists
        • 5.1.1. Using Lists as Stacks
        • 5.1.2. Using Lists as Queues
        • 5.1.3. Functional Programming Tools
        • 5.1.4. List Comprehensions
      • 5.2. The del statement
      • 5.3. Tuples and Sequences
      • 5.4. Sets
      • 5.5. Dictionaries
      • 5.6. Looping Techniques
      • 5.7. More on Conditions
      • 5.8. Comparing Sequences and Other Types
    • 6. Modules
      • 6.1. More on Modules
        • 6.1.1. Executing modules as scripts
        • 6.1.2. The Module Search Path
        • 6.1.3. “Compiled” Python files
      • 6.2. Standard Modules
      • 6.3. The dir() Function
      • 6.4. Packages
        • 6.4.1. Importing * From a Package
        • 6.4.2. Intra-package References
        • 6.4.3. Packages in Multiple Directories
    • 7. Input and Output
      • 7.1. Fancier Output Formatting
        • 7.1.1. Old string formatting
      • 7.2. Reading and Writing Files
        • 7.2.1. Methods of File Objects
        • 7.2.2. Saving structured data with json
    • 8. Errors and Exceptions
      • 8.1. Syntax Errors
      • 8.2. Exceptions
      • 8.3. Handling Exceptions
      • 8.4. Raising Exceptions
      • 8.5. User-defined Exceptions
      • 8.6. Defining Clean-up Actions
      • 8.7. Predefined Clean-up Actions
    • 9. Classes
      • 9.1. A Word About Names and Objects
      • 9.2. Python Scopes and Namespaces
      • 9.3. A First Look at Classes
        • 9.3.1. Class Definition Syntax
        • 9.3.2. Class Objects
        • 9.3.3. Instance Objects
        • 9.3.4. Method Objects
        • 9.3.5. Class and Instance Variables
      • 9.4. Random Remarks
      • 9.5. Inheritance
        • 9.5.1. Multiple Inheritance
      • 9.6. Private Variables and Class-local References
      • 9.7. Odds and Ends
      • 9.8. Exceptions Are Classes Too
      • 9.9. Iterators
      • 9.10. Generators
      • 9.11. Generator Expressions
    • 10. Brief Tour of the Standard Library
      • 10.1. Operating System Interface
      • 10.2. File Wildcards
      • 10.3. Command Line Arguments
      • 10.4. Error Output Redirection and Program Termination
      • 10.5. String Pattern Matching
      • 10.6. Mathematics
      • 10.7. Internet Access
      • 10.8. Dates and Times
      • 10.9. Data Compression
      • 10.10. Performance Measurement
      • 10.11. Quality Control
      • 10.12. Batteries Included
    • 11. Brief Tour of the Standard Library – Part II
      • 11.1. Output Formatting
      • 11.2. Templating
      • 11.3. Working with Binary Data Record Layouts
      • 11.4. Multi-threading
      • 11.5. Logging
      • 11.6. Weak References
      • 11.7. Tools for Working with Lists
      • 11.8. Decimal Floating Point Arithmetic
    • 12. What Now?
    • 13. Interactive Input Editing and History Substitution
      • 13.1. Line Editing
      • 13.2. History Substitution
      • 13.3. Key Bindings
      • 13.4. Alternatives to the Interactive Interpreter
    • 14. Floating Point Arithmetic: Issues and Limitations
      • 14.1. Representation Error
    • 15. Appendix
      • 15.1. Interactive Mode
        • 15.1.1. Error Handling
        • 15.1.2. Executable Python Scripts
        • 15.1.3. The Interactive Startup File
        • 15.1.4. The Customization Modules
  • The Python Language Reference
    • 1. Introduction
      • 1.1. Alternate Implementations
      • 1.2. Notation
    • 2. Lexical analysis
      • 2.1. Line structure
        • 2.1.1. Logical lines
        • 2.1.2. Physical lines
        • 2.1.3. Comments
        • 2.1.4. Encoding declarations
        • 2.1.5. Explicit line joining
        • 2.1.6. Implicit line joining
        • 2.1.7. Blank lines
        • 2.1.8. Indentation
        • 2.1.9. Whitespace between tokens
      • 2.2. Other tokens
      • 2.3. Identifiers and keywords
        • 2.3.1. Keywords
        • 2.3.2. Reserved classes of identifiers
      • 2.4. Literals
        • 2.4.1. String literals
        • 2.4.2. String literal concatenation
        • 2.4.3. Numeric literals
        • 2.4.4. Integer and long integer literals
        • 2.4.5. Floating point literals
        • 2.4.6. Imaginary literals
      • 2.5. Operators
      • 2.6. Delimiters
    • 3. Data model
      • 3.1. Objects, values and types
      • 3.2. The standard type hierarchy
      • 3.3. New-style and classic classes
      • 3.4. Special method names
        • 3.4.1. Basic customization
        • 3.4.2. Customizing attribute access
        • 3.4.3. Customizing class creation
        • 3.4.4. Customizing instance and subclass checks
        • 3.4.5. Emulating callable objects
        • 3.4.6. Emulating container types
        • 3.4.7. Additional methods for emulation of sequence types
        • 3.4.8. Emulating numeric types
        • 3.4.9. Coercion rules
        • 3.4.10. With Statement Context Managers
        • 3.4.11. Special method lookup for old-style classes
        • 3.4.12. Special method lookup for new-style classes
    • 4. Execution model
      • 4.1. Naming and binding
        • 4.1.1. Interaction with dynamic features
      • 4.2. Exceptions
    • 5. Expressions
      • 5.1. Arithmetic conversions
      • 5.2. Atoms
        • 5.2.1. Identifiers (Names)
        • 5.2.2. Literals
        • 5.2.3. Parenthesized forms
        • 5.2.4. List displays
        • 5.2.5. Displays for sets and dictionaries
        • 5.2.6. Generator expressions
        • 5.2.7. Dictionary displays
        • 5.2.8. Set displays
        • 5.2.9. String conversions
        • 5.2.10. Yield expressions
      • 5.3. Primaries
        • 5.3.1. Attribute references
        • 5.3.2. Subscriptions
        • 5.3.3. Slicings
        • 5.3.4. Calls
      • 5.4. The power operator
      • 5.5. Unary arithmetic and bitwise operations
      • 5.6. Binary arithmetic operations
      • 5.7. Shifting operations
      • 5.8. Binary bitwise operations
      • 5.9. Comparisons
      • 5.10. Boolean operations
      • 5.11. Conditional Expressions
      • 5.12. Lambdas
      • 5.13. Expression lists
      • 5.14. Evaluation order
      • 5.15. Operator precedence
    • 6. Simple statements
      • 6.1. Expression statements
      • 6.2. Assignment statements
        • 6.2.1. Augmented assignment statements
      • 6.3. The assert statement
      • 6.4. The pass statement
      • 6.5. The del statement
      • 6.6. The print statement
      • 6.7. The return statement
      • 6.8. The yield statement
      • 6.9. The raise statement
      • 6.10. The break statement
      • 6.11. The continue statement
      • 6.12. The import statement
        • 6.12.1. Future statements
      • 6.13. The global statement
      • 6.14. The exec statement
    • 7. Compound statements
      • 7.1. The if statement
      • 7.2. The while statement
      • 7.3. The for statement
      • 7.4. The try statement
      • 7.5. The with statement
      • 7.6. Function definitions
      • 7.7. Class definitions
    • 8. Top-level components
      • 8.1. Complete Python programs
      • 8.2. File input
      • 8.3. Interactive input
      • 8.4. Expression input
    • 9. Full Grammar specification
  • Python Setup and Usage
    • 1. Command line and environment
      • 1.1. Command line
        • 1.1.1. Interface options
        • 1.1.2. Generic options
        • 1.1.3. Miscellaneous options
        • 1.1.4. Options you shouldn’t use
      • 1.2. Environment variables
        • 1.2.1. Debug-mode variables
    • 2. Using Python on Unix platforms
      • 2.1. Getting and installing the latest version of Python
        • 2.1.1. On Linux
        • 2.1.2. On FreeBSD and OpenBSD
        • 2.1.3. On OpenSolaris
      • 2.2. Building Python
      • 2.3. Python-related paths and files
      • 2.4. Miscellaneous
      • 2.5. Editors
    • 3. Using Python on Windows
      • 3.1. Installing Python
      • 3.2. Alternative bundles
      • 3.3. Configuring Python
        • 3.3.1. Excursus: Setting environment variables
        • 3.3.2. Finding the Python executable
        • 3.3.3. Finding modules
        • 3.3.4. Executing scripts
      • 3.4. Additional modules
        • 3.4.1. PyWin32
        • 3.4.2. Py2exe
        • 3.4.3. WConio
      • 3.5. Compiling Python on Windows
      • 3.6. Other resources
    • 4. Using Python on a Macintosh
      • 4.1. Getting and Installing MacPython
        • 4.1.1. How to run a Python script
        • 4.1.2. Running scripts with a GUI
        • 4.1.3. Configuration
      • 4.2. The IDE
      • 4.3. Installing Additional Python Packages
      • 4.4. GUI Programming on the Mac
      • 4.5. Distributing Python Applications on the Mac
      • 4.6. Other Resources
  • Python HOWTOs
    • 1. Porting Python 2 Code to Python 3
      • 1.1. The Short Explanation
      • 1.2. Details
        • 1.2.1. Drop support for Python 2.5 and older (at least)
        • 1.2.2. Make sure you specify the proper version support in your setup.py file
        • 1.2.3. Have good test coverage
        • 1.2.4. Learn the differences between Python 2 & 3
        • 1.2.5. Update your code
        • 1.2.6. Prevent compatibility regressions
        • 1.2.7. Check which dependencies block your transition
        • 1.2.8. Update your setup.py file to denote Python 3 compatibility
        • 1.2.9. Use continuous integration to stay compatible
      • 1.3. Dropping Python 2 support completely
    • 2. Porting Extension Modules to Python 3
      • 2.1. Conditional compilation
      • 2.2. Changes to Object APIs
        • 2.2.1. str/unicode Unification
        • 2.2.2. long/int Unification
      • 2.3. Module initialization and state
      • 2.4. CObject replaced with Capsule
      • 2.5. Other options
    • 3. Curses Programming with Python
      • 3.1. What is curses?
        • 3.1.1. The Python curses module
      • 3.2. Starting and ending a curses application
      • 3.3. Windows and Pads
      • 3.4. Displaying Text
        • 3.4.1. Attributes and Color
      • 3.5. User Input
      • 3.6. For More Information
    • 4. Descriptor HowTo Guide
      • 4.1. Abstract
      • 4.2. Definition and Introduction
      • 4.3. Descriptor Protocol
      • 4.4. Invoking Descriptors
      • 4.5. Descriptor Example
      • 4.6. Properties
      • 4.7. Functions and Methods
      • 4.8. Static Methods and Class Methods
    • 5. Idioms and Anti-Idioms in Python
      • 5.1. Language Constructs You Should Not Use
        • 5.1.1. from module import *
        • 5.1.2. Unadorned exec, execfile() and friends
        • 5.1.3. from module import name1, name2
        • 5.1.4. except:
      • 5.2. Exceptions
      • 5.3. Using the Batteries
      • 5.4. Using Backslash to Continue Statements
    • 6. Functional Programming HOWTO
      • 6.1. Introduction
        • 6.1.1. Formal provability
        • 6.1.2. Modularity
        • 6.1.3. Ease of debugging and testing
        • 6.1.4. Composability
      • 6.2. Iterators
        • 6.2.1. Data Types That Support Iterators
      • 6.3. Generator expressions and list comprehensions
      • 6.4. Generators
        • 6.4.1. Passing values into a generator
      • 6.5. Built-in functions
      • 6.6. Small functions and the lambda expression
      • 6.7. The itertools module
        • 6.7.1. Creating new iterators
        • 6.7.2. Calling functions on elements
        • 6.7.3. Selecting elements
        • 6.7.4. Grouping elements
      • 6.8. The functools module
        • 6.8.1. The operator module
      • 6.9. Revision History and Acknowledgements
      • 6.10. References
        • 6.10.1. General
        • 6.10.2. Python-specific
        • 6.10.3. Python documentation
    • 7. Logging HOWTO
      • 7.1. Basic Logging Tutorial
        • 7.1.1. When to use logging
        • 7.1.2. A simple example
        • 7.1.3. Logging to a file
        • 7.1.4. Logging from multiple modules
        • 7.1.5. Logging variable data
        • 7.1.6. Changing the format of displayed messages
        • 7.1.7. Displaying the date/time in messages
        • 7.1.8. Next Steps
      • 7.2. Advanced Logging Tutorial
        • 7.2.1. Logging Flow
        • 7.2.2. Loggers
        • 7.2.3. Handlers
        • 7.2.4. Formatters
        • 7.2.5. Configuring Logging
        • 7.2.6. What happens if no configuration is provided
        • 7.2.7. Configuring Logging for a Library
      • 7.3. Logging Levels
        • 7.3.1. Custom Levels
      • 7.4. Useful Handlers
      • 7.5. Exceptions raised during logging
      • 7.6. Using arbitrary objects as messages
      • 7.7. Optimization
    • 8. Logging Cookbook
      • 8.1. Using logging in multiple modules
      • 8.2. Logging from multiple threads
      • 8.3. Multiple handlers and formatters
      • 8.4. Logging to multiple destinations
      • 8.5. Configuration server example
      • 8.6. Sending and receiving logging events across a network
      • 8.7. Adding contextual information to your logging output
        • 8.7.1. Using LoggerAdapters to impart contextual information
        • 8.7.2. Using Filters to impart contextual information
      • 8.8. Logging to a single file from multiple processes
      • 8.9. Using file rotation
      • 8.10. An example dictionary-based configuration
      • 8.11. Inserting a BOM into messages sent to a SysLogHandler
      • 8.12. Implementing structured logging
      • 8.13. Customizing handlers with dictConfig()
      • 8.14. Configuring filters with dictConfig()
      • 8.15. Customized exception formatting
      • 8.16. Speaking logging messages
      • 8.17. Buffering logging messages and outputting them conditionally
      • 8.18. Formatting times using UTC (GMT) via configuration
      • 8.19. Using a context manager for selective logging
    • 9. Regular Expression HOWTO
      • 9.1. Introduction
      • 9.2. Simple Patterns
        • 9.2.1. Matching Characters
        • 9.2.2. Repeating Things
      • 9.3. Using Regular Expressions
        • 9.3.1. Compiling Regular Expressions
        • 9.3.2. The Backslash Plague
        • 9.3.3. Performing Matches
        • 9.3.4. Module-Level Functions
        • 9.3.5. Compilation Flags
      • 9.4. More Pattern Power
        • 9.4.1. More Metacharacters
        • 9.4.2. Grouping
        • 9.4.3. Non-capturing and Named Groups
        • 9.4.4. Lookahead Assertions
      • 9.5. Modifying Strings
        • 9.5.1. Splitting Strings
        • 9.5.2. Search and Replace
      • 9.6. Common Problems
        • 9.6.1. Use String Methods
        • 9.6.2. match() versus search()
        • 9.6.3. Greedy versus Non-Greedy
        • 9.6.4. Using re.VERBOSE
      • 9.7. Feedback
    • 10. Socket Programming HOWTO
      • 10.1. Sockets
        • 10.1.1. History
      • 10.2. Creating a Socket
        • 10.2.1. IPC
      • 10.3. Using a Socket
        • 10.3.1. Binary Data
      • 10.4. Disconnecting
        • 10.4.1. When Sockets Die
      • 10.5. Non-blocking Sockets
        • 10.5.1. Performance
    • 11. Sorting HOW TO
      • 11.1. Sorting Basics
      • 11.2. Key Functions
      • 11.3. Operator Module Functions
      • 11.4. Ascending and Descending
      • 11.5. Sort Stability and Complex Sorts
      • 11.6. The Old Way Using Decorate-Sort-Undecorate
      • 11.7. The Old Way Using the cmp Parameter
      • 11.8. Odd and Ends
    • 12. Unicode HOWTO
      • 12.1. Introduction to Unicode
        • 12.1.1. History of Character Codes
        • 12.1.2. Definitions
        • 12.1.3. Encodings
        • 12.1.4. References
      • 12.2. Python 2.x’s Unicode Support
        • 12.2.1. The Unicode Type
        • 12.2.2. Unicode Literals in Python Source Code
        • 12.2.3. Unicode Properties
        • 12.2.4. References
      • 12.3. Reading and Writing Unicode Data
        • 12.3.1. Unicode filenames
        • 12.3.2. Tips for Writing Unicode-aware Programs
        • 12.3.3. References
      • 12.4. Revision History and Acknowledgements
    • 13. HOWTO Fetch Internet Resources Using urllib2
      • 13.1. Introduction
      • 13.2. Fetching URLs
        • 13.2.1. Data
        • 13.2.2. Headers
      • 13.3. Handling Exceptions
        • 13.3.1. URLError
        • 13.3.2. HTTPError
        • 13.3.3. Wrapping it Up
      • 13.4. info and geturl
      • 13.5. Openers and Handlers
      • 13.6. Basic Authentication
      • 13.7. Proxies
      • 13.8. Sockets and Layers
      • 13.9. Footnotes
    • 14. HOWTO Use Python in the web
      • 14.1. The Low-Level View
        • 14.1.1. Common Gateway Interface
        • 14.1.2. mod_python
        • 14.1.3. FastCGI and SCGI
        • 14.1.4. mod_wsgi
      • 14.2. Step back: WSGI
        • 14.2.1. WSGI Servers
        • 14.2.2. Case study: MoinMoin
      • 14.3. Model-View-Controller
      • 14.4. Ingredients for Websites
        • 14.4.1. Templates
        • 14.4.2. Data persistence
      • 14.5. Frameworks
        • 14.5.1. Some notable frameworks
    • 15. Argparse Tutorial
      • 15.1. Concepts
      • 15.2. The basics
      • 15.3. Introducing Positional arguments
      • 15.4. Introducing Optional arguments
        • 15.4.1. Short options
      • 15.5. Combining Positional and Optional arguments
      • 15.6. Getting a little more advanced
        • 15.6.1. Conflicting options
      • 15.7. Conclusion
  • Installing Python Modules
    • Key terms
    • Basic usage
    • How do I ...?
      • ... install pip in versions of Python prior to Python 2.7.9?
      • ... install packages just for the current user?
      • ... install scientific Python packages?
      • ... work with multiple versions of Python installed in parallel?
    • Common installation issues
      • Installing into the system Python on Linux
      • Installing binary extensions
  • Distributing Python Modules
    • Key terms
    • Open source licensing and collaboration
    • Installing the tools
    • Reading the guide
    • How do I...?
      • ... choose a name for my project?
      • ... create and distribute binary extensions?
  • Extending and Embedding the Python Interpreter
    • 1. Extending Python with C or C++
      • 1.1. A Simple Example
      • 1.2. Intermezzo: Errors and Exceptions
      • 1.3. Back to the Example
      • 1.4. The Module’s Method Table and Initialization Function
      • 1.5. Compilation and Linkage
      • 1.6. Calling Python Functions from C
      • 1.7. Extracting Parameters in Extension Functions
      • 1.8. Keyword Parameters for Extension Functions
      • 1.9. Building Arbitrary Values
      • 1.10. Reference Counts
        • 1.10.1. Reference Counting in Python
        • 1.10.2. Ownership Rules
        • 1.10.3. Thin Ice
        • 1.10.4. NULL Pointers
      • 1.11. Writing Extensions in C++
      • 1.12. Providing a C API for an Extension Module
    • 2. Defining New Types
      • 2.1. The Basics
        • 2.1.1. Adding data and methods to the Basic example
        • 2.1.2. Providing finer control over data attributes
        • 2.1.3. Supporting cyclic garbage collection
        • 2.1.4. Subclassing other types
      • 2.2. Type Methods
        • 2.2.1. Finalization and De-allocation
        • 2.2.2. Object Presentation
        • 2.2.3. Attribute Management
        • 2.2.4. Object Comparison
        • 2.2.5. Abstract Protocol Support
        • 2.2.6. Weak Reference Support
        • 2.2.7. More Suggestions
    • 3. Building C and C++ Extensions with distutils
      • 3.1. Distributing your extension modules
    • 4. Building C and C++ Extensions on Windows
      • 4.1. A Cookbook Approach
      • 4.2. Differences Between Unix and Windows
      • 4.3. Using DLLs in Practice
    • 5. Embedding Python in Another Application
      • 5.1. Very High Level Embedding
      • 5.2. Beyond Very High Level Embedding: An overview
      • 5.3. Pure Embedding
      • 5.4. Extending Embedded Python
      • 5.5. Embedding Python in C++
      • 5.6. Compiling and Linking under Unix-like systems
  • Python/C API Reference Manual
    • 1. Introduction
      • 1.1. Include Files
      • 1.2. Objects, Types and Reference Counts
        • 1.2.1. Reference Counts
        • 1.2.2. Types
      • 1.3. Exceptions
      • 1.4. Embedding Python
      • 1.5. Debugging Builds
    • 2. The Very High Level Layer
    • 3. Reference Counting
    • 4. Exception Handling
      • 4.1. Unicode Exception Objects
      • 4.2. Recursion Control
      • 4.3. Standard Exceptions
      • 4.4. String Exceptions
    • 5. Utilities
      • 5.1. Operating System Utilities
      • 5.2. System Functions
      • 5.3. Process Control
      • 5.4. Importing Modules
      • 5.5. Data marshalling support
      • 5.6. Parsing arguments and building values
      • 5.7. String conversion and formatting
      • 5.8. Reflection
      • 5.9. Codec registry and support functions
        • 5.9.1. Codec lookup API
        • 5.9.2. Registry API for Unicode encoding error handlers
    • 6. Abstract Objects Layer
      • 6.1. Object Protocol
      • 6.2. Number Protocol
      • 6.3. Sequence Protocol
      • 6.4. Mapping Protocol
      • 6.5. Iterator Protocol
      • 6.6. Old Buffer Protocol
    • 7. Concrete Objects Layer
      • 7.1. Fundamental Objects
        • 7.1.1. Type Objects
        • 7.1.2. The None Object
      • 7.2. Numeric Objects
        • 7.2.1. Plain Integer Objects
        • 7.2.2. Boolean Objects
        • 7.2.3. Long Integer Objects
        • 7.2.4. Floating Point Objects
        • 7.2.5. Complex Number Objects
      • 7.3. Sequence Objects
        • 7.3.1. Byte Array Objects
        • 7.3.2. String/Bytes Objects
        • 7.3.3. Unicode Objects and Codecs
        • 7.3.4. Buffers and Memoryview Objects
        • 7.3.5. Tuple Objects
        • 7.3.6. List Objects
      • 7.4. Mapping Objects
        • 7.4.1. Dictionary Objects
      • 7.5. Other Objects
        • 7.5.1. Class and Instance Objects
        • 7.5.2. Function Objects
        • 7.5.3. Method Objects
        • 7.5.4. File Objects
        • 7.5.5. Module Objects
        • 7.5.6. Iterator Objects
        • 7.5.7. Descriptor Objects
        • 7.5.8. Slice Objects
        • 7.5.9. Weak Reference Objects
        • 7.5.10. Capsules
        • 7.5.11. CObjects
        • 7.5.12. Cell Objects
        • 7.5.13. Generator Objects
        • 7.5.14. DateTime Objects
        • 7.5.15. Set Objects
        • 7.5.16. Code Objects
    • 8. Initialization, Finalization, and Threads
      • 8.1. Initializing and finalizing the interpreter
      • 8.2. Process-wide parameters
      • 8.3. Thread State and the Global Interpreter Lock
        • 8.3.1. Releasing the GIL from extension code
        • 8.3.2. Non-Python created threads
        • 8.3.3. High-level API
        • 8.3.4. Low-level API
      • 8.4. Sub-interpreter support
        • 8.4.1. Bugs and caveats
      • 8.5. Asynchronous Notifications
      • 8.6. Profiling and Tracing
      • 8.7. Advanced Debugger Support
    • 9. Memory Management
      • 9.1. Overview
      • 9.2. Memory Interface
      • 9.3. Examples
    • 10. Object Implementation Support
      • 10.1. Allocating Objects on the Heap
      • 10.2. Common Object Structures
      • 10.3. Type Objects
      • 10.4. Number Object Structures
      • 10.5. Mapping Object Structures
      • 10.6. Sequence Object Structures
      • 10.7. Buffer Object Structures
      • 10.8. Supporting Cyclic Garbage Collection
  • Glossary
    • symbols
    • a-e
    • f-h
    • i-k
    • l-n
    • o-z
  • Python Frequently Asked Questions
    • 1. General Python FAQ
      • 1.1. General Information
        • 1.1.1. What is Python?
        • 1.1.2. What is the Python Software Foundation?
        • 1.1.3. Are there copyright restrictions on the use of Python?
        • 1.1.4. Why was Python created in the first place?
        • 1.1.5. What is Python good for?
        • 1.1.6. How does the Python version numbering scheme work?
        • 1.1.7. How do I obtain a copy of the Python source?
        • 1.1.8. How do I get documentation on Python?
        • 1.1.9. I’ve never programmed before. Is there a Python tutorial?
        • 1.1.10. Is there a newsgroup or mailing list devoted to Python?
        • 1.1.11. How do I get a beta test version of Python?
        • 1.1.12. How do I submit bug reports and patches for Python?
        • 1.1.13. Are there any published articles about Python that I can reference?
        • 1.1.14. Are there any books on Python?
        • 1.1.15. Where in the world is www.python.org located?
        • 1.1.16. Why is it called Python?
        • 1.1.17. Do I have to like “Monty Python’s Flying Circus”?
      • 1.2. Python in the real world
        • 1.2.1. How stable is Python?
        • 1.2.2. How many people are using Python?
        • 1.2.3. Have any significant projects been done in Python?
        • 1.2.4. What new developments are expected for Python in the future?
        • 1.2.5. Is it reasonable to propose incompatible changes to Python?
        • 1.2.6. Is Python a good language for beginning programmers?
      • 1.3. Upgrading Python
        • 1.3.1. What is this bsddb185 module my application keeps complaining about?
    • 2. Programming FAQ
      • 2.1. General Questions
        • 2.1.1. Is there a source code level debugger with breakpoints, single-stepping, etc.?
        • 2.1.2. Is there a tool to help find bugs or perform static analysis?
        • 2.1.3. How can I create a stand-alone binary from a Python script?
        • 2.1.4. Are there coding standards or a style guide for Python programs?
        • 2.1.5. My program is too slow. How do I speed it up?
      • 2.2. Core Language
        • 2.2.1. Why am I getting an UnboundLocalError when the variable has a value?
        • 2.2.2. What are the rules for local and global variables in Python?
        • 2.2.3. Why do lambdas defined in a loop with different values all return the same result?
        • 2.2.4. How do I share global variables across modules?
        • 2.2.5. What are the “best practices” for using import in a module?
        • 2.2.6. Why are default values shared between objects?
        • 2.2.7. How can I pass optional or keyword parameters from one function to another?
        • 2.2.8. What is the difference between arguments and parameters?
        • 2.2.9. Why did changing list ‘y’ also change list ‘x’?
        • 2.2.10. How do I write a function with output parameters (call by reference)?
        • 2.2.11. How do you make a higher order function in Python?
        • 2.2.12. How do I copy an object in Python?
        • 2.2.13. How can I find the methods or attributes of an object?
        • 2.2.14. How can my code discover the name of an object?
        • 2.2.15. What’s up with the comma operator’s precedence?
        • 2.2.16. Is there an equivalent of C’s ”?:” ternary operator?
        • 2.2.17. Is it possible to write obfuscated one-liners in Python?
      • 2.3. Numbers and strings
        • 2.3.1. How do I specify hexadecimal and octal integers?
        • 2.3.2. Why does -22 // 10 return -3?
        • 2.3.3. How do I convert a string to a number?
        • 2.3.4. How do I convert a number to a string?
        • 2.3.5. How do I modify a string in place?
        • 2.3.6. How do I use strings to call functions/methods?
        • 2.3.7. Is there an equivalent to Perl’s chomp() for removing trailing newlines from strings?
        • 2.3.8. Is there a scanf() or sscanf() equivalent?
        • 2.3.9. What does ‘UnicodeError: ASCII [decoding,encoding] error: ordinal not in range(128)’ mean?
      • 2.4. Sequences (Tuples/Lists)
        • 2.4.1. How do I convert between tuples and lists?
        • 2.4.2. What’s a negative index?
        • 2.4.3. How do I iterate over a sequence in reverse order?
        • 2.4.4. How do you remove duplicates from a list?
        • 2.4.5. How do you make an array in Python?
        • 2.4.6. How do I create a multidimensional list?
        • 2.4.7. How do I apply a method to a sequence of objects?
        • 2.4.8. Why does a_tuple[i] += [‘item’] raise an exception when the addition works?
      • 2.5. Dictionaries
        • 2.5.1. How can I get a dictionary to display its keys in a consistent order?
        • 2.5.2. I want to do a complicated sort: can you do a Schwartzian Transform in Python?
        • 2.5.3. How can I sort one list by values from another list?
      • 2.6. Objects
        • 2.6.1. What is a class?
        • 2.6.2. What is a method?
        • 2.6.3. What is self?
        • 2.6.4. How do I check if an object is an instance of a given class or of a subclass of it?
        • 2.6.5. What is delegation?
        • 2.6.6. How do I call a method defined in a base class from a derived class that overrides it?
        • 2.6.7. How can I organize my code to make it easier to change the base class?
        • 2.6.8. How do I create static class data and static class methods?
        • 2.6.9. How can I overload constructors (or methods) in Python?
        • 2.6.10. I try to use __spam and I get an error about _SomeClassName__spam.
        • 2.6.11. My class defines __del__ but it is not called when I delete the object.
        • 2.6.12. How do I get a list of all instances of a given class?
        • 2.6.13. Why does the result of id() appear to be not unique?
      • 2.7. Modules
        • 2.7.1. How do I create a .pyc file?
        • 2.7.2. How do I find the current module name?
        • 2.7.3. How can I have modules that mutually import each other?
        • 2.7.4. __import__(‘x.y.z’) returns <module ‘x’>; how do I get z?
        • 2.7.5. When I edit an imported module and reimport it, the changes don’t show up. Why does this happen?
    • 3. Design and History FAQ
      • 3.1. Why does Python use indentation for grouping of statements?
      • 3.2. Why am I getting strange results with simple arithmetic operations?
      • 3.3. Why are floating point calculations so inaccurate?
      • 3.4. Why are Python strings immutable?
      • 3.5. Why must ‘self’ be used explicitly in method definitions and calls?
      • 3.6. Why can’t I use an assignment in an expression?
      • 3.7. Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?
      • 3.8. Why is join() a string method instead of a list or tuple method?
      • 3.9. How fast are exceptions?
      • 3.10. Why isn’t there a switch or case statement in Python?
      • 3.11. Can’t you emulate threads in the interpreter instead of relying on an OS-specific thread implementation?
      • 3.12. Why can’t lambda expressions contain statements?
      • 3.13. Can Python be compiled to machine code, C or some other language?
      • 3.14. How does Python manage memory?
      • 3.15. Why isn’t all memory freed when Python exits?
      • 3.16. Why are there separate tuple and list data types?
      • 3.17. How are lists implemented?
      • 3.18. How are dictionaries implemented?
      • 3.19. Why must dictionary keys be immutable?
      • 3.20. Why doesn’t list.sort() return the sorted list?
      • 3.21. How do you specify and enforce an interface spec in Python?
      • 3.22. Why is there no goto?
      • 3.23. Why can’t raw strings (r-strings) end with a backslash?
      • 3.24. Why doesn’t Python have a “with” statement for attribute assignments?
      • 3.25. Why are colons required for the if/while/def/class statements?
      • 3.26. Why does Python allow commas at the end of lists and tuples?
    • 4. Library and Extension FAQ
      • 4.1. General Library Questions
        • 4.1.1. How do I find a module or application to perform task X?
        • 4.1.2. Where is the math.py (socket.py, regex.py, etc.) source file?
        • 4.1.3. How do I make a Python script executable on Unix?
        • 4.1.4. Is there a curses/termcap package for Python?
        • 4.1.5. Is there an equivalent to C’s onexit() in Python?
        • 4.1.6. Why don’t my signal handlers work?
      • 4.2. Common tasks
        • 4.2.1. How do I test a Python program or component?
        • 4.2.2. How do I create documentation from doc strings?
        • 4.2.3. How do I get a single keypress at a time?
      • 4.3. Threads
        • 4.3.1. How do I program using threads?
        • 4.3.2. None of my threads seem to run: why?
        • 4.3.3. How do I parcel out work among a bunch of worker threads?
        • 4.3.4. What kinds of global value mutation are thread-safe?
        • 4.3.5. Can’t we get rid of the Global Interpreter Lock?
      • 4.4. Input and Output
        • 4.4.1. How do I delete a file? (And other file questions...)
        • 4.4.2. How do I copy a file?
        • 4.4.3. How do I read (or write) binary data?
        • 4.4.4. I can’t seem to use os.read() on a pipe created with os.popen(); why?
        • 4.4.5. How do I run a subprocess with pipes connected to both input and output?
        • 4.4.6. How do I access the serial (RS232) port?
        • 4.4.7. Why doesn’t closing sys.stdout (stdin, stderr) really close it?
      • 4.5. Network/Internet Programming
        • 4.5.1. What WWW tools are there for Python?
        • 4.5.2. How can I mimic CGI form submission (METHOD=POST)?
        • 4.5.3. What module should I use to help with generating HTML?
        • 4.5.4. How do I send mail from a Python script?
        • 4.5.5. How do I avoid blocking in the connect() method of a socket?
      • 4.6. Databases
        • 4.6.1. Are there any interfaces to database packages in Python?
        • 4.6.2. How do you implement persistent objects in Python?
        • 4.6.3. Why is cPickle so slow?
        • 4.6.4. If my program crashes with a bsddb (or anydbm) database open, it gets corrupted. How come?
        • 4.6.5. I tried to open Berkeley DB file, but bsddb produces bsddb.error: (22, ‘Invalid argument’). Help! How can I restore my data?
      • 4.7. Mathematics and Numerics
        • 4.7.1. How do I generate random numbers in Python?
    • 5. Extending/Embedding FAQ
      • 5.1. Can I create my own functions in C?
      • 5.2. Can I create my own functions in C++?
      • 5.3. Writing C is hard; are there any alternatives?
      • 5.4. How can I execute arbitrary Python statements from C?
      • 5.5. How can I evaluate an arbitrary Python expression from C?
      • 5.6. How do I extract C values from a Python object?
      • 5.7. How do I use Py_BuildValue() to create a tuple of arbitrary length?
      • 5.8. How do I call an object’s method from C?
      • 5.9. How do I catch the output from PyErr_Print() (or anything that prints to stdout/stderr)?
      • 5.10. How do I access a module written in Python from C?
      • 5.11. How do I interface to C++ objects from Python?
      • 5.12. I added a module using the Setup file and the make fails; why?
      • 5.13. How do I debug an extension?
      • 5.14. I want to compile a Python module on my Linux system, but some files are missing. Why?
      • 5.15. What does “SystemError: _PyImport_FixupExtension: module yourmodule not loaded” mean?
      • 5.16. How do I tell “incomplete input” from “invalid input”?
      • 5.17. How do I find undefined g++ symbols __builtin_new or __pure_virtual?
      • 5.18. Can I create an object class with some methods implemented in C and others in Python (e.g. through inheritance)?
      • 5.19. When importing module X, why do I get “undefined symbol: PyUnicodeUCS2*”?
    • 6. Python on Windows FAQ
      • 6.1. How do I run a Python program under Windows?
      • 6.2. How do I make Python scripts executable?
      • 6.3. Why does Python sometimes take so long to start?
      • 6.4. How do I make an executable from a Python script?
      • 6.5. Is a *.pyd file the same as a DLL?
      • 6.6. How can I embed Python into a Windows application?
      • 6.7. How do I keep editors from inserting tabs into my Python source?
      • 6.8. How do I check for a keypress without blocking?
      • 6.9. How do I emulate os.kill() in Windows?
      • 6.10. How do I extract the downloaded documentation on Windows?
    • 7. Graphic User Interface FAQ
      • 7.1. What platform-independent GUI toolkits exist for Python?
        • 7.1.1. Tkinter
        • 7.1.2. wxWidgets
        • 7.1.3. Qt
        • 7.1.4. Gtk+
        • 7.1.5. FLTK
        • 7.1.6. FOX
        • 7.1.7. OpenGL
      • 7.2. What platform-specific GUI toolkits exist for Python?
      • 7.3. Tkinter questions
        • 7.3.1. How do I freeze Tkinter applications?
        • 7.3.2. Can I have Tk events handled while waiting for I/O?
        • 7.3.3. I can’t get key bindings to work in Tkinter: why?
    • 8. “Why is Python Installed on my Computer?” FAQ
      • 8.1. What is Python?
      • 8.2. Why is Python installed on my machine?
      • 8.3. Can I delete Python?
Python Documentation
  • Docs »
  • Python Setup and Usage
  • View page source

Python Setup and UsageΒΆ

This part of the documentation is devoted to general information on the setup of the Python environment on different platform, the invocation of the interpreter and things that make working with Python easier.

  • 1. Command line and environment
    • 1.1. Command line
    • 1.2. Environment variables
  • 2. Using Python on Unix platforms
    • 2.1. Getting and installing the latest version of Python
    • 2.2. Building Python
    • 2.3. Python-related paths and files
    • 2.4. Miscellaneous
    • 2.5. Editors
  • 3. Using Python on Windows
    • 3.1. Installing Python
    • 3.2. Alternative bundles
    • 3.3. Configuring Python
    • 3.4. Additional modules
    • 3.5. Compiling Python on Windows
    • 3.6. Other resources
  • 4. Using Python on a Macintosh
    • 4.1. Getting and Installing MacPython
    • 4.2. The IDE
    • 4.3. Installing Additional Python Packages
    • 4.4. GUI Programming on the Mac
    • 4.5. Distributing Python Applications on the Mac
    • 4.6. Other Resources
Next Previous

© Copyright 2016.

Built with Sphinx using a theme provided by Read the Docs.