Modules

Intermediate Python for Developers

George Boorman

Curriculum Manager, DataCamp

What are modules?

  • Modules are Python scripts

    • Files ending with .py
    • Contain functions and attributes
    • Can contain other modules
  • Python comes with several modules

  • Help us avoid writing code that already exists!

Man looking at reflection, seeing further reflections

Intermediate Python for Developers

Python modules

  • There are around 200 built-in modules

 

  • Popular modules include:
    • os - for interpreting and interacting with your operating system
    • collections - advanced data structure types and functions
    • string - performing string operations
    • logging - to log information when testing or running software
    • subprocess - to run terminal commands within a Python file

 

 

# List all files in a directory
ls
Intermediate Python for Developers

Importing a module

# General syntax
import <module_name>
# Import the os module
import os
# Check the type
type(os)
<class 'module'>
Intermediate Python for Developers

Finding a module's functions

  • Look at the documentation
# Call help()
# Warning - will return a very large output!
help(os)
Help on module os:

NAME
    os - OS routines for NT or Posix depending on what system we're on.

MODULE REFERENCE
    https://docs.python.org/3.10/library/os.html
1 https://docs.python.org/3/library/os.html#module-os
Intermediate Python for Developers

Getting the current working directory

# Using an os function
os.getcwd()
'/home/georgeboorman/intermediate_python_for_developers'
  • Useful if we need to refer to the directory repeatedly
# Assign to a variable
work_dir = os.getcwd()
Intermediate Python for Developers

Changing directory

# Changing directory
os.chdir("/home/georgeboorman")
# Check the current directory
os.getcwd()
'/home/georgeboorman'
# Confirm work_dir has not changed
work_dir
'/home/georgeboorman/intermediate_python_for_developers'
Intermediate Python for Developers

Module attributes

  • Attributes have values
  • Functions perform tasks
  • Don't use parentheses with attributes
# Get the local environment
os.environ
environ{'PATH': '/usr/local/bin',
        'TERM': 'xterm',
        'HOSTNAME': '097a0fe4-d6ce-4325-a6e2-1d0ce2800c2b',
        'TZ': 'Europe/Brussels',
        'PYTHONWARNINGS': 'ignore',
        'LANG': 'en_US.UTF-8'
        ...}
Intermediate Python for Developers

Importing a single function from a module

  • Importing a whole module can require a lot of memory

  • Can import a specific function from a module

# Import a function from a module
from os import chdir
Intermediate Python for Developers

Importing multiple functions from a module

# Import multiple functions from a module
from os import chdir, getcwd

# No need to include os. getcwd()
'/home/georgeboorman'
  • Haven't imported os module so Python won't understand
Intermediate Python for Developers

Let's practice!

Intermediate Python for Developers

Preparing Video For Download...