HomeMathComputingArtsWordsLiteratureMusictwitter facebook webfeed

Modules & Packages in Python & Perl

Advertise Here For Profit

Xah Lee, 2005-01-25

Python

A library in python is called a module. This page explains the basics of loading 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

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

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

# print the module's online manual
print help(os)
# Python
# prints a list of existing modules
print help('modules')

Perl

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

To load a package, do use packageName. It will import all functions in that package. Example:

# loading some commonly used packages
use Data::Dumper; # for printing list and hash
use File::Find; # for traversing directories

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

blog comments powered by Disqus