MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaEmacsUnicode ♥
Web Hosting by 1&1

Python & Perl: Using Modules/Packages/Library

Xah Lee, ,

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

A library in Python is called a module.

Finding Modules and Docs

To list all modules, type pydoc modules in shell or in Python help('modules'):

# -*- coding: utf-8 -*-
# python

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

To read the doc of a module, in terminal, do pydoc ‹module name›, or in Python program call help(‹module name›).

# -*- coding: utf-8 -*-
# python

import os

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

Call dir(‹module name›) to list all names exported by the module.

# -*- coding: utf-8 -*-
# python

import os

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

# sample output:
# ['F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'UserDict', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_copy_reg', '_execvpe', '_exists', '_exit', '_get_exports_list', '_make_stat_result', '_make_statvfs_result', '_pickle_stat_result', '_pickle_statvfs_result', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'curdir', 'defpath', 'devnull', 'dup', 'dup2', 'environ', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fstat', 'fsync', 'getcwd', 'getcwdu', 'getenv', 'getpid', 'isatty', 'kill', 'linesep', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'popen2', 'popen3', 'popen4', 'putenv', 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'sep', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs_result', 'strerror', 'sys', 'system', 'tempnam', 'times', 'tmpfile', 'tmpnam', 'umask', 'unlink', 'unsetenv', 'urandom', 'utime', 'waitpid', 'walk', 'write']

Using Modules

Use import ‹name› to load a module. Once loaded, use ‹module name›.‹function name› to call a function.

# python
# import the standard module named os
import os

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

Module's function name can be imported directly by the syntax from ‹module name› import ‹function name 1›, ‹function name 2›, … or from ‹module name› import *.

# -*- coding: utf-8 -*-
# python

from os import getcwd
print getcwd()

Module Search Path

Python module search path is stored in the variable sys.path.

# -*- coding: utf-8 -*-
# python

import sys
print sys.path

# sample output
# ['/home/xah', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']

Perl

In Perl, a library is called a module. The standard filename suffix is “.pm”.

Listing Modules

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

To load a package, call use ‹package name›;. It will import all functions in that package. Example:

# -*- coding: utf-8 -*-
# perl

# 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.

Module Load Path

Here is a example showing module paths and loaded modules:

# -*- coding: utf-8 -*-
# perl

use Data::Dumper;

print Dumper \@INC; # prints all module searching paths

print Dumper \%INC; # prints all loaded modules

__END__

sample output:

$VAR1 = [
          '/etc/perl',
          '/usr/local/lib/perl/5.12.4',
          '/usr/local/share/perl/5.12.4',
          '/usr/lib/perl5',
          '/usr/share/perl5',
          '/usr/lib/perl/5.12',
          '/usr/share/perl/5.12',
          '/usr/local/lib/site_perl',
          '.'
        ];
$VAR1 = {
          'warnings/register.pm' => '/usr/share/perl/5.12/warnings/register.pm',
          'bytes.pm' => '/usr/share/perl/5.12/bytes.pm',
          'XSLoader.pm' => '/usr/share/perl/5.12/XSLoader.pm',
          'Carp.pm' => '/usr/share/perl/5.12/Carp.pm',
          'Exporter.pm' => '/usr/share/perl/5.12/Exporter.pm',
          'strict.pm' => '/usr/share/perl/5.12/strict.pm',
          'warnings.pm' => '/usr/share/perl/5.12/warnings.pm',
          'overload.pm' => '/usr/share/perl/5.12/overload.pm',
          'Data/Dumper.pm' => '/usr/lib/perl/5.12/Data/Dumper.pm'
        };

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

blog comments powered by Disqus