By James Braunsberg. What are Modules? Modules are files containing Python definitions and statements (ex. name.py) A module’s definitions can be imported.

Slides:



Advertisements
Similar presentations
15. Python - Modules A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand.
Advertisements

Chapter Modules CSC1310 Fall Modules Modules Modules are the highest level program organization unit, usually correspond to source files and.
Perkovic, Chapter 7 Functions revisited Python Namespaces
 2005 Pearson Education, Inc. All rights reserved Introduction.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Geography 465 Modules and Functions Writing Custom Functions.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Linux+ Guide to Linux Certification, Second Edition
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
Guide To UNIX Using Linux Third Edition
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.
Review of C++ Programming Part II Sheng-Fang Huang.
7/17/2009 rwjBROOKDALE COMMUNITY COLLEGE1 Unix Comp-145 C HAPTER 2.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
Python Programming Fundamentals
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Introduction to Shell Script Programming
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Python Modules An Introduction. Introduction A module is a file containing Python definitions and statements. The file name is the module name with the.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Scons Writing Solid Code Overview What is scons? scons Basics Other cools scons stuff Resources.
Python From the book “Think Python”
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Introduction to the Windows API n API - Application Programming Interface n an API is the software interface for things such as the OS n an API is the.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Overview Intro to functions What are functions? Why use functions? Defining functions Calling functions Documenting functions Top-down design Variable.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
I Power Higher Computing Software Development Development Languages and Environments.
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
Linux+ Guide to Linux Certification, Third Edition
CS2021 Week 2 Off and Running with Python. Two ways to run Python The Python interpreter – You type one expression at a time – The interpreter evaluates.
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
Chapter Six Introduction to Shell Script Programming.
Python Let’s get started!.
Linux+ Guide to Linux Certification, Second Edition
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 2: The Linux System Part 2.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Modules. Modules Modules are the highest level program organization unit, usually correspond to source files and serve as libraries of tools. Each file.
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
Linux Administration Working with the BASH Shell.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Juancho Datu. What is a Module? File containing Python definitions and statements with the suffix ‘.py’ in the current directory For example file name:
Python’s Modules Noah Black.
G. Pullaiah College of Engineering and Technology
Lecture 2 Python Basics.
Python Let’s get started!.
Chapter 13 - The Preprocessor
Python’s Modules by E. Esin GOKGOZ.
User Defined Functions
Topics Introduction to File Input and Output
Chapter 2: The Linux System Part 2
Programming in Python – Lecture#2
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Lab 4: Introduction to Scripting
Python Modules.
Topics Introduction to File Input and Output
Review We've seen that a module is a file that can contain classes as well as its own variables. We've seen that you need to import it to access the code,
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Python Modules.
© Sangeeta M Chauhan, Gwalior
Presentation transcript:

By James Braunsberg

What are Modules? Modules are files containing Python definitions and statements (ex. name.py) A module’s definitions can be imported into other modules by using “import name” The module’s name is available as a global variable value To access a module’s functions, type “name.function()”

More on Modules Modules can contain executable statements along with function definitions Each module has its own private symbol table used as the global symbol table by all functions in the module Modules can import other modules Each module is imported once per interpreter session reload(name) Can import names from a module into the importing module’s symbol table from mod import m1, m2 (or *) m1()

Executing Modules python name.py Runs code as if it was imported Setting _name_ == “_main_” the file can be used as a script and an importable module

The Module Search Path The interpreter searches for a file named name.py Current directory given by variable sys.path List of directories specified by PYTHONPATH Default path (in UNIX -.:/usr/local/lib/python) Script being run should not have the same name as a standard module or an error will occur when the module is imported

“Compiled” Python Files If files mod.pyc and mod.py are in the same directory, there is a byte-compiled version of the module mod The modification time of the version of mod.py used to create mod.pyc is stored in mod.pyc Normally, the user does not need to do anything to create the.pyc file A compiled.py file is written to the.pyc No error for failed attempt,.pyc is recognized as invalid Contents of the.pyc can be shared by different machines

Some Tips -O flag generates optimized code and stores it in.pyo files Only removes assert statements.pyc files are ignored and.py files are compiled to optimized bytecode Passing two –OO flags Can result in malfunctioning programs _doc_ strings are removed Same speed when read from.pyc,.pyo, or.py files,.pyo and.pyc files are loaded faster Startup time of a script can be reduced by moving its code to a module and importing the module Can have a.pyc or.pyo file without having a.py file for the same module Module compileall creates.pyc or.pyo files for all modules in a directory

Standard Modules Python comes with a library of standard modules described in the Python Library Reference Some are built into interpreter >>> import sys >>> sys.s1 ‘>>> ‘ >>> sys.s1 = ‘c> ‘ c> print ‘Hello’ Hello c> sys.path determines the interpreters’s search path for modules, with the default path taken from PYTHONPATH Can be modified with append() (ex. Sys.path.append(‘SOMEPATH’)

The dir() Function Used to find the names a module defines and returns a sorted list of strings >>> import mod >>> dir(mod) [‘_name_’, ‘m1’, ‘m2’] Without arguments, it lists the names currently defined (variables, modules, functions, etc) Does not list names of built-in functions and variables Use _bulltin_to view all built-in functions and variables

Packages “dotted module names” (ex. a.b) Submodule b in package a Saves authors of multi-module packages from worrying about each other’s module names Python searches through sys.path directories for the package subdirectory Users of the package can import individual modules from the package Ways to import submodules import sound.effects.echo from sound.effects import echo Submodules must be referenced by full name An ImportError exception is raised when the package cannot be found

Importing * From a Package * does not import all submodules from a package Ensures that the package has been imported, only importing the names of the submodules defined in the package import sound.effects.echo import sound.effects.surround from sound.effects import *

Intra-package References Submodules can refer to each other Surround might use echo module import echo also loads surround module import statement first looks in the containing package before looking in the standard module search path Absolute imports refer to submodules of sibling packages sound.filters.vocoder uses echo module from sound.effects import echo Can write explicit relative imports from. import echo from.. import formats from..filters import equalizer

Packages in Multiple Directories _path_ is a list containing the name of the directory holding the package’s _init_.py Changing this variable can affect futute searches for modules and subpackages in the package Can be used to extend the set of modules in a package Not often needed

Sources