Python’s Modules Noah Black.

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.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Geography 465 Modules and Functions Writing Custom Functions.
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.
Command Console Tutorial BCIS 3680 Enterprise Programming.
© 2010 IBM Corporation IBM Experience Modeler - Theme Editor Installing Python Image Library Presenter’s Name - Presenter’s Title DD Month Year.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Python Programming Fundamentals
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Python Modules An Introduction. Introduction A module is a file containing Python definitions and statements. The file name is the module name with the.
UCSC All rights reserved. No part of this material may be reproduced and sold. 1 IT1202-Fundamentals Of Programming (Using JAVA) Interacting with.
Scons Writing Solid Code Overview What is scons? scons Basics Other cools scons stuff Resources.
Linux Operations and Administration
CSC 508 – Theory of Programming Languages, Spring, 2009 Week 3: Data Abstraction and Object Orientation.
By James Braunsberg. What are Modules? Modules are files containing Python definitions and statements (ex. name.py) A module’s definitions can be imported.
Intro Python: Variables, Indexing, Numbers, Strings.
Overview Intro to functions What are functions? Why use functions? Defining functions Calling functions Documenting functions Top-down design Variable.
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.
Basic DOS How to get some work done. It’s all a file Everything is a file: OS files, Application files, Data files and Game files Files have 8.3 names:
Building Packages BCIS 3680 Enterprise Programming.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
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.
Perl Subroutines User Input Perl on linux Forks and Pipes.
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
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:
Installation and environment variables. Simple Installation 1 The latest sources can always be obtained from the CDAT website:
Chapter 13 Debugging Strategies Learning by debugging.
Node.js Modules Header Mastering Node.js, Part 2 Eric W. Greene
Development Environment
G. Pullaiah College of Engineering and Technology
Python Lesson 12 Mr. Kalmes.
PL/pgSQL
Lecture 2 Python Basics.
Classes and objects You can define many objects of the same class:
Modules and Packages Damian Gordon.
Topics Introduction to Repetition Structures
Intro to Python Programming – Part II
Java Primer 1: Types, Classes and Operators
Python’s Modules by E. Esin GOKGOZ.
Python Lesson 12 Mr. Kalmes.
CHAPTER FOUR Functions.
Operation System Program 4
© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved.
Topics Introduction to File Input and Output
Programming in Python – Lecture#2
More About Objects and Methods
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Intro to PHP.
Lab 4: Introduction to Scripting
Python Modules.
Functions Imran Rashid CTO at ManiWeber Technologies.
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
Topics Introduction to File Input and Output
EN Software Carpentry Python – A Crash Course Esoteric Sections Compiled Languages.
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.
SPL – PS1 Introduction to C++.
Python Modules.
Presentation transcript:

Python’s Modules Noah Black

Contents What are modules? Importing modules Accessing module components Executing modules as scripts Module search path Compiled Python files Standard modules The dir() function Packages Importing * from a package Intra-package references Packages in multiple directories

What are modules? Definitions of functions and variables are not saved when interpreter is exited Modules allow definitions to be saved for later access Modules also allow statements to be run as executable scripts Modules are files with .py extension

Importing modules Modules are imported by using built-in import command, without the .py extension >>> import example To make access easier, individual definitions within a module may be imported >>> from function import func1, func2 When modules are imported, all statements and definitions will be executed

Accessing module components To use functions defined in module, type module name followed by dot >>> example.func(3) If function in module was imported individually with from, the module name and the dot may be excluded >>> func(3)

Executing modules as scripts Python scripts with .py extension maybe executed in command line with python example.py <arguments> Imported modules and executed modules can be distinguished by accessing global variable __name__: the value of __name__ is “__main__” only when modules are executed.

Modules as scripts, cont’d Control statements can be used as follows: if __name__ == "__main__": All code contained in if statement will not run if module is imported

Module search path Interpreter looks for all imported modules in certain designated places, in the following order, until the module is found: Current directory The list of directories in PYTHONPATH environment variable Installation-dependent default path

Module search path, cont’d Environment variable PYTHONPATH is a list of directories Current directory, PYTHONPATH, and installation-dependent default path are all stored in sys.path variable

Compiled Python files Compiled version of example.py is stored in example.pyc If no .pyc file exists when a module is imported, it is created Upon future imports, the already compiled version is used to save time, unless original .py file has a new modification time

Standard modules There is a standard library of Python modules These modules contain built-in operations that are not actually a part of Python at its core Some modules are only imported if a certain operating system is being used One important library module is sys

The sys module Two variable contained in sys, ps1 and ps2, hold the values of the strings in the primary and secondary prompts. These strings can be changed to your preference. >>> import sys >>> sys.ps1 = 'plurt> ' plurt> print ‘plurt' plurt plurt>

The dir() function The dir() function returns a list of names (variables and functions) that are defined by the module passed to it. >>> dir(sqrt) [‘num', ‘result'] When dir is executed with no arguments, it returns a list of all currently defined names, except those that are built-in

Packages Packages are organized collections of modules, modules within modules Modules are stored in directories, with each directory containing a __init__.py file that tells the interpreter that it is a package directory Each subdirectory of root package directory is considered a subpackage

Packages, cont’d The package or any of its subpackages may then be imported. import currency.conversion.euro Any of euro’s definitions or subpackages can now be accessed, but only using dots: euro.convertfromdollar(10)

Importing * from a package Sometimes you may want to import everything inside a package or subpackage. In this case you may use this command: from example.subexample import * This loads all module names contained in variable __all__, which is itself contained in the package directory’s __init__.py file.

Importing * from a package, cont’d If __all__ variable is not defined, then by default, importing * from a package will import all defined names of the package, and the package itself. Any submodules will not be imported unless they are explicitly imported by __init__.py or by

Intra-package references Any module in a package may import any other modules in the same directory by name without using any dots For importing any modules in a sibling folder, a module may use an absolute import

Packages in multiple directories The location in which a package searches for its __init__.py may be changed, by modifying the variable __path__, which stores the value of the directory the __init__.py is in

Sources http://docs.python.org/library/ Information on Python’s standard library http://docs.python.org/tutorial/modules.html Tutorial on Python’s modules