Welcome to Knowledge Base!

KB at your finger tips

This is one stop global knowledge base where you can learn about all the products, solutions and support features.

Categories
All
Programming-Python
16. Appendix





16. Appendix¶



16.1. Interactive Mode¶



16.1.1. Error Handling¶


When an error occurs, the interpreter prints an error message and a stack trace.
In interactive mode, it then returns to the primary prompt; when input came from
a file, it exits with a nonzero exit status after printing the stack trace.
(Exceptions handled by an except clause in a try statement
are not errors in this context.) Some errors are unconditionally fatal and
cause an exit with a nonzero exit; this applies to internal inconsistencies and
some cases of running out of memory. All error messages are written to the
standard error stream; normal output from executed commands is written to
standard output.


Typing the interrupt character (usually Control - C or Delete ) to the primary or
secondary prompt cancels the input and returns to the primary prompt. 1
Typing an interrupt while a command is executing raises the
KeyboardInterrupt exception, which may be handled by a try
statement.




16.1.2. Executable Python Scripts¶


On BSD’ish Unix systems, Python scripts can be made directly executable, like
shell scripts, by putting the line


#!/usr/bin/env python3.5


(assuming that the interpreter is on the user’s PATH ) at the beginning
of the script and giving the file an executable mode. The #! must be the
first two characters of the file. On some platforms, this first line must end
with a Unix-style line ending ( '\n' ), not a Windows ( '\r\n' ) line
ending. Note that the hash, or pound, character, '#' , is used to start a
comment in Python.


The script can be given an executable mode, or permission, using the
chmod command.


$ chmod +x myscript.py


On Windows systems, there is no notion of an “executable mode”. The Python
installer automatically associates .py files with python.exe so that
a double-click on a Python file will run it as a script. The extension can
also be .pyw , in that case, the console window that normally appears is
suppressed.




16.1.3. The Interactive Startup File¶


When you use Python interactively, it is frequently handy to have some standard
commands executed every time the interpreter is started. You can do this by
setting an environment variable named PYTHONSTARTUP to the name of a
file containing your start-up commands. This is similar to the .profile
feature of the Unix shells.


This file is only read in interactive sessions, not when Python reads commands
from a script, and not when /dev/tty is given as the explicit source of
commands (which otherwise behaves like an interactive session). It is executed
in the same namespace where interactive commands are executed, so that objects
that it defines or imports can be used without qualification in the interactive
session. You can also change the prompts sys.ps1 and sys.ps2 in this
file.


If you want to read an additional start-up file from the current directory, you
can program this in the global start-up file using code like if
os.path.isfile('.pythonrc.py'): exec(open('.pythonrc.py').read())
.
If you want to use the startup file in a script, you must do this explicitly
in the script:


import os
filename = os.environ.get('PYTHONSTARTUP')
if filename and os.path.isfile(filename):
with open(filename) as fobj:
startup_file = fobj.read()
exec(startup_file)




16.1.4. The Customization Modules¶


Python provides two hooks to let you customize it: sitecustomize and
usercustomize . To see how it works, you need first to find the location
of your user site-packages directory. Start Python and run this code:


>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.5/site-packages'


Now you can create a file named usercustomize.py in that directory and
put anything you want in it. It will affect every invocation of Python, unless
it is started with the -s option to disable the automatic import.


sitecustomize works in the same way, but is typically created by an
administrator of the computer in the global site-packages directory, and is
imported before usercustomize . See the documentation of the site
module for more details.


Footnotes



1

A problem with the GNU Readline package may prevent this.












Python Setup and Usage





Python Setup and Usage¶


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




  • 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. Custom OpenSSL



  • 3. Configure Python

    • 3.1. Configure Options

      • 3.1.1. General Options

      • 3.1.2. WebAssembly Options

      • 3.1.3. Install Options

      • 3.1.4. Performance options

      • 3.1.5. Python Debug Build

      • 3.1.6. Debug options

      • 3.1.7. Linker options

      • 3.1.8. Libraries options

      • 3.1.9. Security Options

      • 3.1.10. macOS Options

      • 3.1.11. Cross Compiling Options



    • 3.2. Python Build System

      • 3.2.1. Main files of the build system

      • 3.2.2. Main build steps

      • 3.2.3. Main Makefile targets

      • 3.2.4. C extensions



    • 3.3. Compiler and linker flags

      • 3.3.1. Preprocessor flags

      • 3.3.2. Compiler flags

      • 3.3.3. Linker flags





  • 4. Using Python on Windows

    • 4.1. The full installer

      • 4.1.1. Installation steps

      • 4.1.2. Removing the MAX_PATH Limitation

      • 4.1.3. Installing Without UI

      • 4.1.4. Installing Without Downloading

      • 4.1.5. Modifying an install



    • 4.2. The Microsoft Store package

      • 4.2.1. Known issues

        • 4.2.1.1. Redirection of local data, registry, and temporary paths





    • 4.3. The nuget.org packages

    • 4.4. The embeddable package

      • 4.4.1. Python Application

      • 4.4.2. Embedding Python



    • 4.5. Alternative bundles

    • 4.6. Configuring Python

      • 4.6.1. Excursus: Setting environment variables

      • 4.6.2. Finding the Python executable



    • 4.7. UTF-8 mode

    • 4.8. Python Launcher for Windows

      • 4.8.1. Getting started

        • 4.8.1.1. From the command-line

        • 4.8.1.2. Virtual environments

        • 4.8.1.3. From a script

        • 4.8.1.4. From file associations



      • 4.8.2. Shebang Lines

      • 4.8.3. Arguments in shebang lines

      • 4.8.4. Customization

        • 4.8.4.1. Customization via INI files

        • 4.8.4.2. Customizing default Python versions



      • 4.8.5. Diagnostics

      • 4.8.6. Dry Run

      • 4.8.7. Install on demand

      • 4.8.8. Return codes



    • 4.9. Finding modules

    • 4.10. Additional modules

      • 4.10.1. PyWin32

      • 4.10.2. cx_Freeze



    • 4.11. Compiling Python on Windows

    • 4.12. Other Platforms



  • 5. Using Python on a Mac

    • 5.1. Getting and Installing MacPython

      • 5.1.1. How to run a Python script

      • 5.1.2. Running scripts with a GUI

      • 5.1.3. Configuration



    • 5.2. The IDE

    • 5.3. Installing Additional Python Packages

    • 5.4. GUI Programming on the Mac

    • 5.5. Distributing Python Applications on the Mac

    • 5.6. Other Resources



  • 6. Editors and IDEs









Read article