Movatterモバイル変換


[0]ホーム

URL:


— FREE Email Series —

🐍 Python Tricks 💌

Python Tricks Dictionary Merge

🔒 No spam. Unsubscribe any time.

Browse TopicsGuided Learning Paths
Basics Intermediate Advanced
aialgorithmsapibest-practicescareercommunitydatabasesdata-sciencedata-structuresdata-vizdevopsdjangodockereditorsflaskfront-endgamedevguimachine-learningnewsnumpyprojectspythonstdlibtestingtoolsweb-devweb-scraping

Table of Contents

Recommended Course

Python Best Practices Artwork

Python Modules and Packages: An Introduction

1h 34m · 16 lessons

Python Modules & Packages

Python Modules and Packages – An Introduction

byJohn SturtzReading time estimate 25mbasicspython

Table of Contents

Remove ads

Recommended Course

Python Modules and Packages: An Introduction(1h 34m)

This article explores Pythonmodules and Pythonpackages, two mechanisms that facilitatemodular programming.

Modular programming refers to the process of breaking a large, unwieldy programming task into separate, smaller, more manageable subtasks ormodules. Individual modules can then be cobbled together like building blocks to create a larger application.

There are several advantages tomodularizing code in a large application:

  • Simplicity: Rather than focusing on the entire problem at hand, a module typically focuses on one relatively small portion of the problem. If you’re working on a single module, you’ll have a smaller problem domain to wrap your head around. This makes development easier and less error-prone.

  • Maintainability: Modules are typically designed so that they enforce logical boundaries between different problem domains. If modules are written in a way that minimizes interdependency, there is decreased likelihood that modifications to a single module will have an impact on other parts of the program. (You may even be able to make changes to a module without having any knowledge of the application outside that module.) This makes it more viable for a team of many programmers to work collaboratively on a large application.

  • Reusability: Functionality defined in a single module can be easily reused (through an appropriately defined interface) by other parts of the application. This eliminates the need to duplicate code.

  • Scoping: Modules typically define a separatenamespace, which helps avoid collisions between identifiers in different areas of a program. (One of the tenets in theZen of Python isNamespaces are one honking great idea—let’s do more of those!)

Functions,modules andpackages are all constructs in Python that promote code modularization.

Take the Quiz: Test your knowledge with our interactive “Python Modules and Packages” quiz. You’ll receive a score upon completion to help you track your learning progress:


Python Modules & Packages

Interactive Quiz

Python Modules and Packages

In this quiz, you'll test your understanding of Python modules and packages, which are mechanisms that facilitate modular programming. Modular programming involves breaking a large programming task into smaller, more manageable subtasks or modules. This approach has several advantages, including simplicity, maintainability, and reusability.

Free PDF Download:Python 3 Cheat Sheet

Python Modules: Overview

There are actually three different ways to define amodule in Python:

  1. A module can be written in Python itself.
  2. A module can be written inC and loaded dynamically at run-time, like there (regular expression) module.
  3. Abuilt-in module is intrinsically contained in the interpreter, like theitertools module.

A module’s contents are accessed the same way in all three cases: with theimport statement.

Here, the focus will mostly be on modules that are written in Python. The cool thing about modules written in Python is that they are exceedingly straightforward to build. All you need to do is create a file that contains legitimate Python code and then give the file a name with a.py extension. That’s it! No special syntax is necessary.

For example, suppose you have created a file calledmod.py containing the following:

mod.py

Python
s="If Comrade Napoleon says it, it must be right."a=[100,200,300]deffoo(arg):print(f'arg ={arg}')classFoo:pass

Several objects are defined inmod.py:

  • s (a string)
  • a (a list)
  • foo() (a function)
  • Foo (a class)

Assumingmod.py is in an appropriate location, which you will learn more about shortly, these objects can be accessed byimporting the module as follows:

Python
>>>importmod>>>print(mod.s)If Comrade Napoleon says it, it must be right.>>>mod.a[100, 200, 300]>>>mod.foo(['quux','corge','grault'])arg = ['quux', 'corge', 'grault']>>>x=mod.Foo()>>>x<mod.Foo object at 0x03C181F0>

The Module Search Path

Continuing with the above example, let’s take a look at what happens when Python executes the statement:

Python
importmod

When the interpreter executes the aboveimport statement, it searches formod.py in alist of directories assembled from the following sources:

  • The directory from which the input script was run or thecurrent directory if the interpreter is being run interactively
  • The list of directories contained in thePYTHONPATH environment variable, if it is set. (The format forPYTHONPATH is OS-dependent but should mimic thePATH environment variable.)
  • An installation-dependent list of directories configured at the time Python is installed

The resulting search path is accessible in the Python variablesys.path, which is obtained from a module namedsys:

Python
>>>importsys>>>sys.path['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib','C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib','C:\\Python36', 'C:\\Python36\\lib\\site-packages']

Note: The exact contents ofsys.path are installation-dependent. The above will almost certainly look slightly different on your computer.

Thus, to ensure your module is found, you need to do one of the following:

  • Putmod.py in the directory where the input script is located or thecurrent directory, if interactive
  • Modify thePYTHONPATH environment variable to contain the directory wheremod.py is located before starting the interpreter
    • Or: Putmod.py in one of the directories already contained in thePYTHONPATH variable
  • Putmod.py in one of the installation-dependent directories, which you may or may not have write-access to, depending on the OS

There is actually one additional option: you can put the module file in any directory of your choice and then modifysys.path at run-time so that it contains that directory. For example, in this case, you could putmod.py in directoryC:\Users\john and then issue the following statements:

Python
>>>sys.path.append(r'C:\Users\john')>>>sys.path['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib','C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib','C:\\Python36', 'C:\\Python36\\lib\\site-packages', 'C:\\Users\\john']>>>importmod

Once a module has been imported, you can determine the location where it was found with the module’s__file__ attribute:

Python
>>>importmod>>>mod.__file__'C:\\Users\\john\\mod.py'>>>importre>>>re.__file__'C:\\Python36\\lib\\re.py'

The directory portion of__file__ should be one of the directories insys.path.

Theimport Statement

Module contents are made available to the caller with theimport statement. Theimport statement takes many different forms, shown below.

import <module_name>

The simplest form is the one already shown above:

Python
import<module_name>

Note that thisdoes not make the module contentsdirectly accessible to the caller. Each module has its ownprivate symbol table, which serves as the global symbol table for all objects definedin the module. Thus, a module creates a separatenamespace, as already noted.

The statementimport <module_name> only places<module_name> in the caller’s symbol table. Theobjects that are defined in the moduleremain in the module’s private symbol table.

From the caller, objects in the module are only accessible when prefixed with<module_name> viadot notation, as illustrated below.

After the followingimport statement,mod is placed into the local symbol table. Thus,mod has meaning in the caller’s local context:

Python
>>>importmod>>>mod<module 'mod' from 'C:\\Users\\john\\Documents\\Python\\doc\\mod.py'>

Buts andfoo remain in the module’s private symbol table and are not meaningful in the local context:

Python
>>>sNameError: name 's' is not defined>>>foo('quux')NameError: name 'foo' is not defined

To be accessed in the local context, names of objects defined in the module must be prefixed bymod:

Python
>>>mod.s'If Comrade Napoleon says it, it must be right.'>>>mod.foo('quux')arg = quux

Several comma-separated modules may be specified in a singleimport statement:

Python
import<module_name>[,<module_name>...]

from <module_name> import <name(s)>

An alternate form of theimport statement allows individual objects from the module to be importeddirectly into the caller’s symbol table:

Python
from<module_name>import<name(s)>

Following execution of the above statement,<name(s)> can be referenced in the caller’s environment without the<module_name> prefix:

Python
>>>frommodimports,foo>>>s'If Comrade Napoleon says it, it must be right.'>>>foo('quux')arg = quux>>>frommodimportFoo>>>x=Foo()>>>x<mod.Foo object at 0x02E3AD50>

Because this form ofimport places the object names directly into the caller’s symbol table, any objects that already exist with the same name will beoverwritten:

Python
>>>a=['foo','bar','baz']>>>a['foo', 'bar', 'baz']>>>frommodimporta>>>a[100, 200, 300]

It is even possible to indiscriminatelyimport everything from a module at one fell swoop:

Python
from<module_name>import*

This will place the names ofall objects from<module_name> into the local symbol table, with the exception of any that begin with theunderscore (_) character.

For example:

Python
>>>frommodimport*>>>s'If Comrade Napoleon says it, it must be right.'>>>a[100, 200, 300]>>>foo<function foo at 0x03B449C0>>>>Foo<class 'mod.Foo'>

This isn’t necessarily recommended in large-scale production code. It’s a bit dangerous because you are entering names into the local symbol tableen masse. Unless you know them all well and can be confident there won’t be a conflict, you have a decent chance of overwriting an existing name inadvertently. However, this syntax is quite handy when you are just mucking around with the interactive interpreter, for testing or discovery purposes, because it quickly gives you access to everything a module has to offer without a lot of typing.

from <module_name> import <name> as <alt_name>

It is also possible toimport individual objects but enter them into the local symbol table with alternate names:

Python
from<module_name>import<name>as<alt_name>[,<name>as<alt_name>]

This makes it possible to place names directly into the local symbol table but avoid conflicts with previously existing names:

Python
>>>s='foo'>>>a=['foo','bar','baz']>>>frommodimportsasstring,aasalist>>>s'foo'>>>string'If Comrade Napoleon says it, it must be right.'>>>a['foo', 'bar', 'baz']>>>alist[100, 200, 300]

import <module_name> as <alt_name>

You can also import an entire module under an alternate name:

Python
import<module_name>as<alt_name>
Python
>>>importmodasmy_module>>>my_module.a[100, 200, 300]>>>my_module.foo('qux')arg = qux

Module contents can be imported from within afunction definition. In that case, theimport does not occur until the function iscalled:

Python
>>>defbar():...frommodimportfoo...foo('corge')...>>>bar()arg = corge

However,Python 3 does not allow the indiscriminateimport * syntax from within a function:

Python
>>>defbar():...frommodimport*...SyntaxError: import * only allowed at module level

Lastly, atry statement with anexcept ImportError clause can be used to guard against unsuccessfulimport attempts:

Python
>>>try:...# Non-existent module...importbaz...exceptImportError:...print('Module not found')...Module not found
Python
>>>try:...# Existing module, but non-existent object...frommodimportbaz...exceptImportError:...print('Object not found in module')...Object not found in module

Thedir() Function

The built-in functiondir() returns a list of defined names in a namespace. Without arguments, it produces an alphabetically sorted list of names in the currentlocal symbol table:

Python
>>>dir()['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__']>>>qux=[1,2,3,4,5]>>>dir()['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__', 'qux']>>>classBar():...pass...>>>x=Bar()>>>dir()['Bar', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__', 'qux', 'x']

Note how the first call todir() above lists several names that are automatically defined and already in the namespace when the interpreter starts. As new names are defined (qux,Bar,x), they appear on subsequent invocations ofdir().

This can be useful for identifying what exactly has been added to the namespace by an import statement:

Python
>>>dir()['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__']>>>importmod>>>dir()['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__', 'mod']>>>mod.s'If Comrade Napoleon says it, it must be right.'>>>mod.foo([1,2,3])arg = [1, 2, 3]>>>frommodimporta,Foo>>>dir()['Foo', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__', 'a', 'mod']>>>a[100, 200, 300]>>>x=Foo()>>>x<mod.Foo object at 0x002EAD50>>>>frommodimportsasstring>>>dir()['Foo', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__', 'a', 'mod', 'string', 'x']>>>string'If Comrade Napoleon says it, it must be right.'

When given an argument that is the name of a module,dir() lists the names defined in the module:

Python
>>>importmod>>>dir(mod)['Foo', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__','__name__', '__package__', '__spec__', 'a', 'foo', 's']
Python
>>>dir()['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__']>>>frommodimport*>>>dir()['Foo', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__', 'a', 'foo', 's']

Executing a Module as a Script

Any.py file that contains amodule is essentially also a Pythonscript, and there isn’t any reason it can’t be executed like one.

Here again ismod.py as it was defined above:

mod.py

Python
s="If Comrade Napoleon says it, it must be right."a=[100,200,300]deffoo(arg):print(f'arg ={arg}')classFoo:pass

This can be run as a script:

Windows Command Prompt
C:\Users\john\Documents>python mod.pyC:\Users\john\Documents>

There are no errors, so it apparently worked. Granted, it’s not very interesting. As it is written, it onlydefines objects. It doesn’tdo anything with them, and it doesn’t generate any output.

Let’s modify the above Python module so it does generate some output when run as a script:

mod.py

Python
s="If Comrade Napoleon says it, it must be right."a=[100,200,300]deffoo(arg):print(f'arg ={arg}')classFoo:passprint(s)print(a)foo('quux')x=Foo()print(x)

Now it should be a little more interesting:

Windows Command Prompt
C:\Users\john\Documents>python mod.pyIf Comrade Napoleon says it, it must be right.[100, 200, 300]arg = quux<__main__.Foo object at 0x02F101D0>

Unfortunately, now it also generates output when imported as a module:

Python
>>>importmodIf Comrade Napoleon says it, it must be right.[100, 200, 300]arg = quux<mod.Foo object at 0x0169AD50>

This is probably not what you want. It isn’t usual for a module to generate output when it is imported.

Wouldn’t it be nice if you could distinguish between when the file is loaded as a module and when it is run as a standalone script?

Ask and ye shall receive.

When a.py file is imported as a module, Python sets the specialdunder variable__name__ to the name of the module. However, if a file is run as a standalone script,__name__ is (creatively) set to the string'__main__'. Using this fact, you can discern which is the case at run-time and alter behavior accordingly:

mod.py

Python
s="If Comrade Napoleon says it, it must be right."a=[100,200,300]deffoo(arg):print(f'arg ={arg}')classFoo:passif(__name__=='__main__'):print('Executing as standalone script')print(s)print(a)foo('quux')x=Foo()print(x)

Now, if you run as a script, you get output:

Windows Command Prompt
C:\Users\john\Documents>python mod.pyExecuting as standalone scriptIf Comrade Napoleon says it, it must be right.[100, 200, 300]arg = quux<__main__.Foo object at 0x03450690>

But if you import as a module, you don’t:

Python
>>>importmod>>>mod.foo('grault')arg = grault

Modules are often designed with the capability to run as a standalone script for purposes of testing the functionality that is contained within the module. This is referred to asunit testing. For example, suppose you have created a modulefact.py containing afactorial function, as follows:

fact.py

Python
deffact(n):return1ifn==1elsen*fact(n-1)if(__name__=='__main__'):importsysiflen(sys.argv)>1:print(fact(int(sys.argv[1])))

The file can be treated as a module, and thefact() function imported:

Python
>>>fromfactimportfact>>>fact(6)720

But it can also be run as a standalone by passing an integer argument on the command-line for testing:

Windows Command Prompt
C:\Users\john\Documents>python fact.py 6720

Reloading a Module

For reasons of efficiency, a module is only loaded once per interpreter session. That is fine for function and class definitions, which typically make up the bulk of a module’s contents. But a module can contain executable statements as well, usually for initialization. Be aware that these statements will only be executed thefirst time a module is imported.

Consider the following filemod.py:

mod.py

Python
a=[100,200,300]print('a =',a)
Python
>>>importmoda = [100, 200, 300]>>>importmod>>>importmod>>>mod.a[100, 200, 300]

Theprint() statement is not executed on subsequent imports. (For that matter, neither is the assignment statement, but as the final display of the value ofmod.a shows, that doesn’t matter. Once the assignment is made, it sticks.)

If you make a change to a module and need to reload it, you need to either restart the interpreter or use a function calledreload() from moduleimportlib:

Python
>>>importmoda = [100, 200, 300]>>>importmod>>>importimportlib>>>importlib.reload(mod)a = [100, 200, 300]<module 'mod' from 'C:\\Users\\john\\Documents\\Python\\doc\\mod.py'>

Python Packages

Suppose you have developed a very large application that includes many modules. As the number of modules grows, it becomes difficult to keep track of them all if they are dumped into one location. This is particularly so if they have similar names or functionality. You might wish for a means of grouping and organizing them.

Packages allow for a hierarchical structuring of the module namespace usingdot notation. In the same way thatmodules help avoid collisions between global variable names,packages help avoid collisions between module names.

Creating apackage is quite straightforward, since it makes use of the operating system’s inherent hierarchical file structure. Consider the following arrangement:

Image of a Python package

Here, there is a directory namedpkg that contains two modules,mod1.py andmod2.py. The contents of the modules are:

mod1.py

Python
deffoo():print('[mod1] foo()')classFoo:pass

mod2.py

Python
defbar():print('[mod2] bar()')classBar:pass

Given this structure, if thepkg directory resides in a location where it can be found (in one of the directories contained insys.path), you can refer to the twomodules withdot notation (pkg.mod1,pkg.mod2) and import them with the syntax you are already familiar with:

Python
import<module_name>[,<module_name>...]
Python
>>>importpkg.mod1,pkg.mod2>>>pkg.mod1.foo()[mod1] foo()>>>x=pkg.mod2.Bar()>>>x<pkg.mod2.Bar object at 0x033F7290>
Python
from<module_name>import<name(s)>
Python
>>>frompkg.mod1importfoo>>>foo()[mod1] foo()
Python
from<module_name>import<name>as<alt_name>
Python
>>>frompkg.mod2importBarasQux>>>x=Qux()>>>x<pkg.mod2.Bar object at 0x036DFFD0>

You can import modules with these statements as well:

Python
from<package_name>import<modules_name>[,<module_name>...]from<package_name>import<module_name>as<alt_name>
Python
>>>frompkgimportmod1>>>mod1.foo()[mod1] foo()>>>frompkgimportmod2asquux>>>quux.bar()[mod2] bar()

You can technically import the package as well:

Python
>>>importpkg>>>pkg<module 'pkg' (namespace)>

But this is of little avail. Though this is, strictly speaking, a syntactically correct Python statement, it doesn’t do much of anything useful. In particular, itdoes not place any of the modules inpkg into the local namespace:

Python
>>>pkg.mod1Traceback (most recent call last):  File"<pyshell#34>", line1, in<module>pkg.mod1AttributeError:module 'pkg' has no attribute 'mod1'>>>pkg.mod1.foo()Traceback (most recent call last):  File"<pyshell#35>", line1, in<module>pkg.mod1.foo()AttributeError:module 'pkg' has no attribute 'mod1'>>>pkg.mod2.Bar()Traceback (most recent call last):  File"<pyshell#36>", line1, in<module>pkg.mod2.Bar()AttributeError:module 'pkg' has no attribute 'mod2'

To actually import the modules or their contents, you need to use one of the forms shown above.

Package Initialization

If a file named__init__.py is present in a package directory, it is invoked when the package or a module in the package is imported. This can be used for execution of package initialization code, such as initialization of package-level data.

For example, consider the following__init__.py file:

__init__.py

Python
print(f'Invoking __init__.py for{__name__}')A=['quux','corge','grault']

Let’s add this file to thepkg directory from the above example:

Illustration of hierarchical file structure of Python packages

Now when the package is imported, the global listA is initialized:

Python
>>>importpkgInvoking __init__.py for pkg>>>pkg.A['quux', 'corge', 'grault']

Amodule in the package can access the global variable by importing it in turn:

mod1.py

Python
deffoo():frompkgimportAprint('[mod1] foo() / A = ',A)classFoo:pass
Python
>>>frompkgimportmod1Invoking __init__.py for pkg>>>mod1.foo()[mod1] foo() / A =  ['quux', 'corge', 'grault']

__init__.py can also be used to effect automatic importing of modules from a package. For example, earlier you saw that the statementimport pkg only places the namepkg in the caller’s local symbol table and doesn’t import any modules. But if__init__.py in thepkg directory contains the following:

__init__.py

Python
print(f'Invoking __init__.py for{__name__}')importpkg.mod1,pkg.mod2

then when you executeimport pkg, modulesmod1 andmod2 are imported automatically:

Python
>>>importpkgInvoking __init__.py for pkg>>>pkg.mod1.foo()[mod1] foo()>>>pkg.mod2.bar()[mod2] bar()

Note: Much of the Python documentation states that an__init__.py filemust be present in the package directory when creating a package. This was once true. It used to be that the very presence of__init__.py signified to Python that a package was being defined. The file could contain initialization code or even be empty, but ithad to be present.

Starting withPython 3.3,Implicit Namespace Packages were introduced. These allow for the creation of a package without any__init__.py file. Of course, itcan still be present if package initialization is needed. But it is no longer required. Check outWhat’s a Python Namespace Package, and What’s It For? to learn more.

Importing* From a Package

For the purposes of the following discussion, the previously defined package is expanded to contain some additional modules:

Illustration of hierarchical file structure of Python packages

There are now four modules defined in thepkg directory. Their contents are as shown below:

mod1.py

Python
deffoo():print('[mod1] foo()')classFoo:pass

mod2.py

Python
defbar():print('[mod2] bar()')classBar:pass

mod3.py

Python
defbaz():print('[mod3] baz()')classBaz:pass

mod4.py

Python
defqux():print('[mod4] qux()')classQux:pass

(Imaginative, aren’t they?)

You have already seen that whenimport * is used for amodule,all objects from the module are imported into the local symbol table, except those whose names begin with an underscore, as always:

Python
>>>dir()['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__']>>>frompkg.mod3import*>>>dir()['Baz', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__', 'baz']>>>baz()[mod3] baz()>>>Baz<class 'pkg.mod3.Baz'>

The analogous statement for apackage is this:

Python
from<package_name>import*

What does that do?

Python
>>>dir()['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__']>>>frompkgimport*>>>dir()['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__']

Hmph. Not much. You might have expected (assuming you had any expectations at all) that Python would dive down into the package directory, find all the modules it could, and import them all. But as you can see, by default that is not what happens.

Instead, Python follows this convention: if the__init__.py file in thepackage directory contains alist named__all__, it is taken to be a list of modules that should be imported when the statementfrom <package_name> import * is encountered.

For the present example, suppose you create an__init__.py in thepkg directory like this:

pkg/__init__.py

Python
__all__=['mod1','mod2','mod3','mod4']

Nowfrom pkg import * imports all four modules:

Python
>>>dir()['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__']>>>frompkgimport*>>>dir()['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__', 'mod1', 'mod2', 'mod3', 'mod4']>>>mod2.bar()[mod2] bar()>>>mod4.Qux<class 'pkg.mod4.Qux'>

Usingimport * still isn’t considered terrific form, any more forpackages than formodules. But this facility at least gives the creator of the package some control over what happens whenimport * is specified. (In fact, it provides the capability to disallow it entirely, simply by declining to define__all__ at all. As you have seen, the default behavior for packages is to import nothing.)

By the way,__all__ can be defined in amodule as well and serves the same purpose: to control what is imported withimport *. For example, modifymod1.py as follows:

pkg/mod1.py

Python
__all__=['foo']deffoo():print('[mod1] foo()')classFoo:pass

Now animport * statement frompkg.mod1 will only import what is contained in__all__:

Python
>>>dir()['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__']>>>frompkg.mod1import*>>>dir()['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__','__package__', '__spec__', 'foo']>>>foo()[mod1] foo()>>>FooTraceback (most recent call last):  File"<pyshell#37>", line1, in<module>FooNameError:name 'Foo' is not defined

foo() (the function) is now defined in the local namespace, butFoo (the class) is not, because the latter is not in__all__.

In summary,__all__ is used by bothpackages andmodules to control what is imported whenimport * is specified. Butthe default behavior differs:

  • For a package, when__all__ is not defined,import * does not import anything.
  • For a module, when__all__ is not defined,import * imports everything (except—you guessed it—names starting with an underscore).

Subpackages

Packages can contain nestedsubpackages to arbitrary depth. For example, let’s make one more modification to the examplepackage directory as follows:

Illustration of hierarchical file structure of Python packages

The four modules (mod1.py,mod2.py,mod3.py andmod4.py) are defined as previously. But now, instead of being lumped together into thepkg directory, they are split out into twosubpackage directories,sub_pkg1 andsub_pkg2.

Importing still works the same as shown previously. Syntax is similar, but additionaldot notation is used to separatepackage name fromsubpackage name:

Python
>>>importpkg.sub_pkg1.mod1>>>pkg.sub_pkg1.mod1.foo()[mod1] foo()>>>frompkg.sub_pkg1importmod2>>>mod2.bar()[mod2] bar()>>>frompkg.sub_pkg2.mod3importbaz>>>baz()[mod3] baz()>>>frompkg.sub_pkg2.mod4importquxasgrault>>>grault()[mod4] qux()

In addition, a module in onesubpackage can reference objects in asibling subpackage (in the event that the sibling contains some functionality that you need). For example, suppose you want to import and execute functionfoo() (defined in modulemod1) from within modulemod3. You can either use anabsolute import:

pkg/sub__pkg2/mod3.py

Python
defbaz():print('[mod3] baz()')classBaz:passfrompkg.sub_pkg1.mod1importfoofoo()
Python
>>>frompkg.sub_pkg2importmod3[mod1] foo()>>>mod3.foo()[mod1] foo()

Or you can use arelative import, where.. refers to the package one level up. From withinmod3.py, which is in subpackagesub_pkg2,

  • .. evaluates to the parent package (pkg), and
  • ..sub_pkg1 evaluates to subpackagesub_pkg1 of the parent package.

pkg/sub__pkg2/mod3.py

Python
defbaz():print('[mod3] baz()')classBaz:passfrom..importsub_pkg1print(sub_pkg1)from..sub_pkg1.mod1importfoofoo()
Python
>>>frompkg.sub_pkg2importmod3<module 'pkg.sub_pkg1' (namespace)>[mod1] foo()

Conclusion

In this tutorial, you covered the following topics:

  • How to create a Pythonmodule
  • Locations where the Python interpreter searches for a module
  • How to obtain access to the objects defined in a module with theimport statement
  • How to create a module that is executable as a standalone script
  • How to organize modules intopackages andsubpackages
  • How to control package initialization

Free PDF Download:Python 3 Cheat Sheet

This will hopefully allow you to better understand how to gain access to the functionality available in the many third-party and built-in modules available in Python.

Additionally, if you are developing your own application, creating your ownmodules andpackages will help you organize and modularize your code, which makes coding, maintenance, and debugging easier.

If you want to learn more, check out the following documentation atPython.org:

Happy Pythoning!

Take the Quiz: Test your knowledge with our interactive “Python Modules and Packages” quiz. You’ll receive a score upon completion to help you track your learning progress:


Python Modules & Packages

Interactive Quiz

Python Modules and Packages

In this quiz, you'll test your understanding of Python modules and packages, which are mechanisms that facilitate modular programming. Modular programming involves breaking a large programming task into smaller, more manageable subtasks or modules. This approach has several advantages, including simplicity, maintainability, and reusability.

Recommended Course

Python Modules and Packages: An Introduction(1h 34m)

🐍 Python Tricks 💌

Get a short & sweetPython Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

Python Tricks Dictionary Merge

AboutJohn Sturtz

John is an avid Pythonista and a member of the Real Python tutorial team.

» More about John

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

MasterReal-World Python Skills With Unlimited Access to Real Python

Locked learning resources

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

MasterReal-World Python Skills
With Unlimited Access to Real Python

Locked learning resources

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

What Do You Think?

Rate this article:

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students.Get tips for asking good questions andget answers to common questions in our support portal.


Looking for a real-time conversation? Visit theReal Python Community Chat or join the next“Office Hours” Live Q&A Session. Happy Pythoning!

Keep Learning

Related Topics:basicspython

Related Learning Paths:

Related Courses:

Related Tutorials:

Keep reading Real Python by creating a free account or signing in:

Already have an account?Sign-In

Almost there! Complete this form and click the button below to gain instant access:

Python Logo

Python 3 Cheat Sheet (PDF)

🔒 No spam. We take your privacy seriously.


[8]ページ先頭

©2009-2026 Movatter.jp