/usr/share/doc/diveintopython/html/object_oriented_framework/importing_modules.html is in diveintopython 5.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | <!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>5.2. Importing Modules Using from module import</title>
<link rel="stylesheet" href="../diveintopython.css" type="text/css">
<link rev="made" href="mailto:f8dy@diveintopython.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
<meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
<meta name="description" content="Python from novice to pro">
<link rel="home" href="../toc/index.html" title="Dive Into Python">
<link rel="up" href="index.html" title="Chapter 5. Objects and Object-Orientation">
<link rel="previous" href="index.html" title="Chapter 5. Objects and Object-Orientation">
<link rel="next" href="defining_classes.html" title="5.3. Defining Classes">
</head>
<body>
<table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
<tr>
<td id="breadcrumb" colspan="5" align="left" valign="top">You are here: <a href="../index.html">Home</a> > <a href="../toc/index.html">Dive Into Python</a> > <a href="index.html">Objects and Object-Orientation</a> > <span class="thispage">Importing Modules Using from module import</span></td>
<td id="navigation" align="right" valign="top"> <a href="index.html" title="Prev: “Objects and Object-Orientation”"><<</a> <a href="defining_classes.html" title="Next: “Defining Classes”">>></a></td>
</tr>
<tr>
<td colspan="3" id="logocontainer">
<h1 id="logo"><a href="../index.html" accesskey="1">Dive Into Python</a></h1>
<p id="tagline">Python from novice to pro</p>
</td>
<td colspan="3" align="right">
<form id="search" method="GET" action="http://www.google.com/custom">
<p><label for="q" accesskey="4">Find: </label><input type="text" id="q" name="q" size="20" maxlength="255" value=" "> <input type="submit" value="Search"><input type="hidden" name="cof" value="LW:752;L:http://diveintopython.org/images/diveintopython.png;LH:42;AH:left;GL:0;AWFID:3ced2bb1f7f1b212;"><input type="hidden" name="domains" value="diveintopython.org"><input type="hidden" name="sitesearch" value="diveintopython.org"></p>
</form>
</td>
</tr>
</table>
<!--#include virtual="/inc/ads" -->
<div class="section" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a name="fileinfo.fromimport"></a>5.2. Importing Modules Using <tt class="literal">from <i class="replaceable">module</i> import</tt></h2>
</div>
</div>
<div></div>
</div>
<div class="abstract">
<p><span class="application">Python</span> has two ways of importing modules. Both are useful, and you should know when to use each. One way, <tt class="literal">import <i class="replaceable">module</i></tt>, you've already seen in <a href="../getting_to_know_python/everything_is_an_object.html" title="2.4. Everything Is an Object">Section 2.4, “Everything Is an Object”</a>. The other way accomplishes the same thing, but it has subtle and important differences.
</p>
</div>
<div class="informalexample">
<p>Here is the basic <tt class="literal">from <i class="replaceable">module</i> import</tt> syntax:
</p><pre class="programlisting"><span class='pykeyword'>
from</span> UserDict <span class='pykeyword'>import</span> UserDict
</pre></div>
<p>This is similar to the <a href="../getting_to_know_python/everything_is_an_object.html#odbchelper.import" title="Example 2.3. Accessing the buildConnectionString Function's doc string"><tt class="literal">import <i class="replaceable">module</i></tt></a> syntax that you know and love, but with an important difference: the attributes and methods of the imported module <tt class="filename">types</tt> are imported directly into the local namespace, so they are available directly, without qualification by module name. You
can import individual items or use <tt class="literal">from <i class="replaceable">module</i> import *</tt> to import everything.
</p><a name="compare.fromimport.perl"></a><table class="note" border="0" summary="">
<tr>
<td rowspan="2" align="center" valign="top" width="1%"><img src="../images/note.png" alt="Note" title="" width="24" height="24"></td>
</tr>
<tr>
<td colspan="2" align="left" valign="top" width="99%"><tt class="literal">from <i class="replaceable">module</i> import *</tt> in <span class="application">Python</span> is like <tt class="literal">use <i class="replaceable">module</i></tt> in <span class="application">Perl</span>; <tt class="literal">import <i class="replaceable">module</i></tt> in <span class="application">Python</span> is like <tt class="literal">require <i class="replaceable">module</i></tt> in <span class="application">Perl</span>.
</td>
</tr>
</table><a name="compare.fromimport.java"></a><table class="note" border="0" summary="">
<tr>
<td rowspan="2" align="center" valign="top" width="1%"><img src="../images/note.png" alt="Note" title="" width="24" height="24"></td>
</tr>
<tr>
<td colspan="2" align="left" valign="top" width="99%"><tt class="literal">from <i class="replaceable">module</i> import *</tt> in <span class="application">Python</span> is like <tt class="literal">import <i class="replaceable">module</i>.*</tt> in <span class="application">Java</span>; <tt class="literal">import <i class="replaceable">module</i></tt> in <span class="application">Python</span> is like <tt class="literal">import <i class="replaceable">module</i></tt> in <span class="application">Java</span>.
</td>
</tr>
</table>
<div class="example"><a name="d0e11355"></a><h3 class="title">Example 5.2. <tt class="literal">import <i class="replaceable">module</i></tt> <span class="foreignphrase"><i class="foreignphrase"><span class="acronym">vs.</span></i></span> <tt class="literal">from <i class="replaceable">module</i> import</tt></h3><pre class="screen"><tt class="prompt">>>> </tt><span class="userinput"><span class='pykeyword'>import</span> types</span>
<tt class="prompt">>>> </tt><span class="userinput">types.FunctionType</span> <a name="fileinfo.import.1.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="computeroutput"><type 'function'></span>
<tt class="prompt">>>> </tt><span class="userinput">FunctionType</span> <a name="fileinfo.import.1.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="traceback">Traceback (innermost last):
File "<interactive input>", line 1, in ?
NameError: There is no variable named 'FunctionType'</span>
<tt class="prompt">>>> </tt><span class="userinput"><span class='pykeyword'>from</span> types <span class='pykeyword'>import</span> FunctionType</span> <a name="fileinfo.import.1.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<tt class="prompt">>>> </tt><span class="userinput">FunctionType</span> <a name="fileinfo.import.1.4"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12">
<span class="computeroutput"><type 'function'></span></pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href="#fileinfo.import.1.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">The <tt class="filename">types</tt> module contains no methods; it just has attributes for each <span class="application">Python</span> object type. Note that the attribute, <tt class="constant">FunctionType</tt>, must be qualified by the module name, <tt class="filename">types</tt>.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#fileinfo.import.1.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left"><tt class="constant">FunctionType</tt> by itself has not been defined in this namespace; it exists only in the context of <tt class="filename">types</tt>.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#fileinfo.import.1.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">This syntax imports the attribute <tt class="constant">FunctionType</tt> from the <tt class="filename">types</tt> module directly into the local namespace.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#fileinfo.import.1.4"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Now <tt class="constant">FunctionType</tt> can be accessed directly, without reference to <tt class="filename">types</tt>.
</td>
</tr>
</table>
</div>
</div>
<p>When should you use <tt class="literal">from <i class="replaceable">module</i> import</tt>?
</p>
<div class="itemizedlist">
<ul>
<li>If you will be accessing attributes and methods often and don't want to type the module name over and over, use <tt class="literal">from <i class="replaceable">module</i> import</tt>.
</li>
<li>If you want to selectively import some attributes and methods but not others, use <tt class="literal">from <i class="replaceable">module</i> import</tt>.
</li>
<li>If the module contains attributes or functions with the same name as ones in your module, you must use <tt class="literal">import <i class="replaceable">module</i></tt> to avoid name conflicts.
</li>
</ul>
</div>
<p>Other than that, it's just a matter of style, and you will see <span class="application">Python</span> code written both ways.
</p><a name="d0e11495"></a><table class="caution" border="0" summary="">
<tr>
<td rowspan="2" align="center" valign="top" width="1%"><img src="../images/caution.png" alt="Caution" title="" width="24" height="24"></td>
</tr>
<tr>
<td colspan="2" align="left" valign="top" width="99%">Use <tt class="literal">from module import *</tt> sparingly, because it makes it difficult to determine where a particular function or attribute came from, and that makes
debugging and refactoring more difficult.
</td>
</tr>
</table>
<div class="furtherreading">
<h3>Further Reading on Module Importing Techniques</h3>
<ul>
<li><a href="http://www.effbot.org/guides/">eff-bot</a> has more to say on <a href="http://www.effbot.org/guides/import-confusion.htm"><tt class="literal">import <i class="replaceable">module</i></tt> <span class="foreignphrase"><i class="foreignphrase"><span class="acronym">vs.</span></i></span> <tt class="literal">from <i class="replaceable">module</i> import</tt></a>.
</li>
<li><a href="http://www.python.org/doc/current/tut/tut.html"><i class="citetitle"><span class="application">Python</span> Tutorial</i></a> discusses advanced import techniques, including <a href="http://www.python.org/doc/current/tut/node8.html#SECTION008410000000000000000"><tt class="literal">from <i class="replaceable">module</i> import *</tt></a>.
</li>
</ul>
</div>
</div>
<table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
<tr>
<td width="35%" align="left"><br><a class="NavigationArrow" href="index.html"><< Objects and Object-Orientation</a></td>
<td width="30%" align="center"><br> <span class="divider">|</span> <a href="index.html#fileinfo.divein" title="5.1. Diving In">1</a> <span class="divider">|</span> <span class="thispage">2</span> <span class="divider">|</span> <a href="defining_classes.html" title="5.3. Defining Classes">3</a> <span class="divider">|</span> <a href="instantiating_classes.html" title="5.4. Instantiating Classes">4</a> <span class="divider">|</span> <a href="userdict.html" title="5.5. Exploring UserDict: A Wrapper Class">5</a> <span class="divider">|</span> <a href="special_class_methods.html" title="5.6. Special Class Methods">6</a> <span class="divider">|</span> <a href="special_class_methods2.html" title="5.7. Advanced Special Class Methods">7</a> <span class="divider">|</span> <a href="class_attributes.html" title="5.8. Introducing Class Attributes">8</a> <span class="divider">|</span> <a href="private_functions.html" title="5.9. Private Functions">9</a> <span class="divider">|</span> <a href="summary.html" title="5.10. Summary">10</a> <span class="divider">|</span>
</td>
<td width="35%" align="right"><br><a class="NavigationArrow" href="defining_classes.html">Defining Classes >></a></td>
</tr>
<tr>
<td colspan="3"><br></td>
</tr>
</table>
<div class="Footer">
<p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
</div>
</body>
</html>
|