Loading A Library in Python

2005-01-25

A library in python is called a module. Here's the basics of loading in a module, finding out what function a module contains, locating the module's documentation, and showing a list of available modules.

# Python

# import the standard module named os
import os

# print all names exported by the module
print dir(os)

# print the module's online manual
print help(os)

# example of using a function
print 'current dir is:', os.getcwd()

# this prints a list of existing modules
print help('modules')

Reference: Python Doc↗.

Perl

In Perl, a library is called a package. To get a list of standard modules that are bundled with perl (but not necessarily installed), see: “perldoc perlmodlib”.

Reference: perldoc perlmodlib↗.

To load a package, do “use packageName”. It will import all functions in that package. For example:

use strict;
use Data::Dumper;
use File::Find;

To find out what functions are available in a module, read its documentation, for example “perldoc Data::Dumper”.

Here is a example showing module paths and loaded modules:

use Data::Dumper; print Dumper \@INC; # prints all module searching paths
use Data::Dumper; print Dumper \%INC; # prints all loaded modules

For more info about the predefined variables @INC and %INC, see: “perldoc perlvar↗


Page created: 2005-01.
© 2005 by Xah Lee.
Xah Signet