Python Modules

Table of Contents

Importing modules

  1. Python comes with the "batteries included" in the form of a large Standard Library of code.
  2. Let's pick one and investigate it. Let's find out about accessing files and directories on our laptop so we'll look into the os module. os stands for "Operating System".
  3. We can make use of the os module by importing it

    import os
    

Directories

  1. Like many things in Python, we can ask os about itself.

    >>> help(os)
    Help on module os:
    
    NAME
        os - OS routines for NT or Posix depending on what system we're on.
    
    FILE
        /usr/lib/python3.5/os.py
    
    MODULE DOCS
        /usr/share/doc/python3/html/os.html
    
    DESCRIPTION
        This exports:
          - all functions from posix, nt, os2, or ce, e.g. unlink, stat, etc.
          - os.path is one of the modules posixpath, or ntpath
          - os.name is 'posix', 'nt', 'os2', 'ce' or 'riscos'
          - os.curdir is a string representing the current directory ('.' or ':')
          - os.pardir is a string representing the parent directory ('..' or '::')
          - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')
          - os.extsep is the extension separator ('.' or '/')
          - os.altsep is the alternate pathname separator (None or '/')
          - os.pathsep is the component separator used in $PATH etc
          - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
          - os.defpath is the default search path for executables
          - os.devnull is the file path of the null device ('/dev/null', etc.)
    
        Programs that import and use 'os' stand a better chance of being
        portable between different platforms.  Of course, they must then
        only use functions that are defined by all platforms (e.g., unlink
        and opendir), and leave all pathname manipulation to os.path
        (e.g., split and join).
    
    ...
    

    It's a big module with lots of documentation!

  2. Let's try to make it more manageable by asking what functions there are in the module.

    >>> dir(os)
    ['EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST',
     'EX_NOINPUT', 'EX_NOPERM', 'EX_NOUSER', 'EX_OK', 'EX_OSERR', 'EX_OSFILE',
     'EX_PROTOCOL', 'EX_SOFTWARE', 'EX_TEMPFAIL', 'EX_UNAVAILABLE', 'EX_USAGE',
     'F_OK', 'NGROUPS_MAX', 'O_APPEND', 'O_ASYNC', 'O_CREAT', 'O_DIRECT',
     'O_DIRECTORY', 'O_DSYNC', 'O_EXCL', 'O_LARGEFILE', 'O_NDELAY', 'O_NOATIME',
     'O_NOCTTY', 'O_NOFOLLOW', 'O_NONBLOCK', 'O_RDONLY', 'O_RDWR', 'O_RSYNC',
     'O_SYNC', 'O_TRUNC', 'O_WRONLY', 'P_NOWAIT', 'P_NOWAITO', 'P_WAIT', 'R_OK',
     'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'UserDict', 'WCONTINUED',
     'WCOREDUMP', 'WEXITSTATUS', 'WIFCONTINUED', 'WIFEXITED', 'WIFSIGNALED',
     'WIFSTOPPED', 'WNOHANG', 'WSTOPSIG', 'WTERMSIG', 'WUNTRACED', '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', '_spawnvef', 'abort',
     'access', 'altsep', 'chdir', 'chmod', 'chown', 'chroot', 'close', 'closerange',
     'confstr', 'confstr_names', 'ctermid', 'curdir', 'defpath', 'devnull', 'dup',
     'dup2', 'environ', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe',
     'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fchdir', 'fchmod', 'fchown',
     'fdatasync', 'fdopen', 'fork', 'forkpty', 'fpathconf', 'fstat', 'fstatvfs',
     'fsync', 'ftruncate', 'getcwd', 'getcwdu', 'getegid', 'getenv', 'geteuid',
     'getgid', 'getgroups', 'getloadavg', 'getlogin', 'getpgid', 'getpgrp',
     'getpid', 'getppid', 'getresgid', 'getresuid', 'getsid', 'getuid',
     'initgroups', 'isatty', 'kill', 'killpg', 'lchown', 'linesep', 'link',
     'listdir', 'lseek', 'lstat', 'major', 'makedev', 'makedirs', 'minor', 'mkdir',
     'mkfifo', 'mknod', 'name', 'nice', 'open', 'openpty', 'pardir', 'path',
     'pathconf', 'pathconf_names', 'pathsep', 'pipe', 'popen', 'popen2', 'popen3',
     'popen4', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename',
     'renames', 'rmdir', 'sep', 'setegid', 'seteuid', 'setgid', 'setgroups',
     'setpgid', 'setpgrp', 'setregid', 'setresgid', 'setresuid', 'setreuid',
     'setsid', 'setuid', 'spawnl', 'spawnle', 'spawnlp', 'spawnlpe', 'spawnv',
     'spawnve', 'spawnvp', 'spawnvpe', 'stat', 'stat_float_times', 'stat_result',
     'statvfs', 'statvfs_result', 'strerror', 'symlink', 'sys', 'sysconf',
     'sysconf_names', 'system', 'tcgetpgrp', 'tcsetpgrp', 'tempnam', 'times',
     'tmpfile', 'tmpnam', 'ttyname', 'umask', 'uname', 'unlink', 'unsetenv',
     'urandom', 'utime', 'wait', 'wait3', 'wait4', 'waitpid', 'walk', 'write']
    
  3. Let's ask about getcwd. Not how we refer to it as os.getcwd because it is in the os module.

    >>> help(os.getcwd)
    Help on built-in function getcwd in module posix:
    
    getcwd(...)
        getcwd() -> path
    
        Return a string representing the current working directory.
    
  4. That looks useful. Let's try it out:

    >>> os.getcwd()
    '/tmp'
    

    This is a string representation of the directory (or folder) in which my Python is running.

  5. If we want to change the working directory, we use chdir:

    >>> os.chdir('/home/bon/projects/auc/courses/pyw')
    >>> os.getcwd()
    '/home/bon/projects/auc/courses/pyw'
    
  6. Try it for yourself.
  7. If you're on Windows, note that Microsoft writes the slashes the other way, e.g.

    'C:\user\docs\homework'
    

    This is a bit of a nuisance. Since Python uses the backslash as an escape character in strings, we have to write double backslashes:

    'C:\\user\\docs\\homework'
    

    Another solution is to use Python raw strings by preceding the opening quote or doublequote by an 'r':

    r'C:\user\docs\homework'
    

    This tells Python to treat the backslash literally (i.e. not as an escape character).

Back to importing

  1. We can limit what we import from a module by specifying

    from os import getcwd, chdir
    

    This is usually good practice unless we are using a particularly large number of functions from the module.

  2. Now we can call these functions without the os. prefix

    >>> from os import getcwd, chdir
    >>> getcwd()
    '/home/bon/projects/auc/courses/pyw'
    >>> chdir('/tmp')
    >>> getcwd()
    '/tmp'
    >>>
    
  3. Or we can just say to import everything from a module for use without the prefix.

    from os import *
    

    This isn't usually a good idea though since it can lead to confusion about which module each name comes from or if names clash. We say it pollutes the name space.

  4. You can even rename the functions as you import them

    >>> from math import floor
    >>> floor(4.3)
    4.0
    >>> from math import floor as vloer
    >>> vloer(4.3)
    4.0
    

    That was a silly example but renaming might be useful for example if we need to import functions with the same name from two different modules

    from foo import bar as foobar
    from goo import bar as goobar
    
  5. Python comes with many modules included. If you scan the Standard Library, you will see many interesting modules which you can familiarise yourself with. You will also see many modules which do things that you don't (yet) understand.

Author: Breanndán Ó Nualláin <o@uva.nl>

Date: 2025-09-04 Thu 08:55