Category: Python Modules

  • Exploring the Python os Module: A gateway to the Operating System

    Python is widely celebrated for its simplicity and power, and one of the key reasons behind its versatility is the vast collection of built-in modules. Among these, the os module stands out as a foundational tool for interacting with the operating system. Whether you’re managing files, navigating directories, or accessing system variables, the os module makes it easy to perform OS-level tasks directly from Python.

    What is the os module?

    The os module provides a way of using operating system-dependent functionality like reading or writing to the file system, modifying environment variables, or working with processes. It is a part of Python’s standard library, meaning you don’t need to install anything extra—just import it and you’re ready to go:

    Python
    import os

    Common use cases

    • Navigating the file system
    • Listing files and directories
    • Creating and removing directories
    • File manipulation
    • Working with paths
    • Environment variables

    Why use the os module?

    The os module abstracts away platform-specific details, allowing your scripts to run across Linux, Windows, and macOS without change. It helps automate workflows, manage resources, and build more powerful, adaptable applications.

    Files and Directory Operations

    Python
    os.getcwd()                  # Get current working directory
    os.chdir(path)               # Change current working directory
    os.listdir(path='.')         # List files and dirs
    os.mkdir(path, mode=0o777)   # Create directory
    os.makedirs(path, exist_ok=False)  # Create nested directories
    os.rmdir(path)               # Remove empty directory
    os.removedirs(path)          # Remove nested directories
    os.remove(path)              # Delete a file
    os.rename(src, dst)          # Rename/move file or dir
    os.replace(src, dst)         # Rename/move, overwrite if needed
    os.stat(path)                # Get file metadata
    os.scandir(path='.')         # Iterator of DirEntry objects
    os.walk(top)                 # Recursively list files/dirs

    Path Manipulation (os.path)

    Python
    os.path.join(a, b, ...)      # Join paths safely
    os.path.abspath(path)        # Absolute path
    os.path.basename(path)       # File name from path
    os.path.dirname(path)        # Directory from path
    os.path.exists(path)         # Path exists?
    os.path.isfile(path)         # Is file?
    os.path.isdir(path)          # Is directory?
    os.path.getsize(path)        # File size in bytes
    os.path.split(path)          # (dir, file)
    os.path.splitext(path)       # (filename, extension)

    Environment Variables

    Python
    os.environ                   # Dict-like env vars
    os.environ.get(key, default) # Get env var
    os.environ['VAR'] = value    # Set env var
    os.putenv(key, value)        # Set env var (less common)
    os.unsetenv(key)             # Remove env var

    Process Management

    Python
    os.system(command)           # Run shell command
    os.startfile(path)           # Open file/app (Windows)
    os.execv(path, args)         # Replace current process
    os.fork()                    # Create process (Unix)
    os.spawn*()                  # Start process (multiple variants)
    os.kill(pid, sig)            # Send signal to process
    os.getpid()                  # Current process ID
    os.getppid()                 # Parent process ID

    File Descriptor Ops (low-level)

    Python
    os.open(path, flags, mode=0o777) # Open file descriptor
    os.read(fd, n)                   # Read n bytes
    os.write(fd, b'some bytes')      # Write bytes
    os.close(fd)                     # Close file descriptor

    OS Info & Constants

    Python
    os.name               # 'posix', 'nt', 'java'
    os.sep                # Path separator ('/' or '\\')
    os.altsep             # Alt path sep (Windows)
    os.extsep             # Extension separator '.'
    os.pathsep            # PATH separator
    os.linesep            # Newline character(s)
    os.devnull            # Null device path
    os.cpu_count()        # Number of CPUs

    Miscellaneous

    Python
    os.urandom(n)         # n random bytes
    os.getlogin()         # Logged-in username
    os.getuid(), os.getgid()   # User/group ID (Unix)
    os.chmod(path, mode)  # Change permissions
    os.access(path, mode) # Check permissions
  • How to set up a Python Virtual Environment on Mac, Windows or Linux

    Setting up a Python virtual environment is one of the most important skills you can learn as a Python developer. It helps you manage dependencies, avoid version conflicts, and keep your global environment clean — especially when working on multiple projects.

    In this guide, I’ll walk you through setting up a virtual environment on macOS, Windows, and Linux with clear, step-by-step instructions.

    So what is a Virtual Environment?

    A virtual environment is a self-contained directory where Python packages can be installed independently of your system’s global Python setup. It’s perfect for:

    • Keeping dependencies isolated per project
    • Preventing version conflicts
    • Making your projects easier to share and deploy

    Prerequisites

      Before you begin, make sure you have:

      • Python 3.3 or later installed
      • pip (Python’s package manager)

      To check the system version of Python you have installed on a Mac open a terminal prompt and run:

      (base) peter@MacBookPro Python % python -V
      Python 3.9.12

      Similarly, to check your pip installation:

      (base) peter@MacBookPro Python % pip -V
      pip 24.0 from /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pip (python 3.9)
      (base) peter@MacBookPro Python % 

      Create a Project Directory

      Create a Directory where you are going to be developing your project and the navigate to it in a terminal or bash window:

      (base) peter@MacBookPro Python % mkdir newdirectory
      (base) peter@MacBookPro Python % cd newdirectory

      Create the Virtual Environment

      Enter the following command to create the virtual environment;

      Bash
      python3 -m venv virtualenvname

      This will create.a subdirectory containing your virtual environment. I tend to call my virtual environments (or ‘virtual envs’) .venv but the choice is yours,

      Activating the Virtual Environment

      Mac OS / Linux

      Before activation

      Bash
      (base) peter@MacBookPro sbs % 

      Activated

      Bash
      (base) peter@MacBookPro sbs % source virtenv/bin/activate<br>(virtenv) (base) peter@MacBookPro sbs % 

      Windows

      Command Prompt

      myenv\Scripts\activate.bat

      Powershell

      PowerShell
      myenv\Scripts\Activate.ps1
      

      Installing Packages

      Now that your environment is active, you can install packages without affecting your global Python installation.

      An example of how to install a package. With the virtual environment (virtenv) activated:

      (virtenv) (base) peter@MacBookPro sbs % pip install requests

      The response I get:

      (virtenv) (base) peter@MacBookPro sbs % pip install requests
      Collecting requests
        Using cached requests-2.32.4-py3-none-any.whl.metadata (4.9 kB)
      Requirement already satisfied: charset_normalizer<4,>=2 in ./virtenv/lib/python3.9/site-packages (from requests) (3.4.2)
      Requirement already satisfied: idna<4,>=2.5 in ./virtenv/lib/python3.9/site-packages (from requests) (3.10)
      Requirement already satisfied: urllib3<3,>=1.21.1 in ./virtenv/lib/python3.9/site-packages (from requests) (2.4.0)
      Requirement already satisfied: certifi>=2017.4.17 in ./virtenv/lib/python3.9/site-packages (from requests) (2025.4.26)
      Using cached requests-2.32.4-py3-none-any.whl (64 kB)
      Installing collected packages: requests
      Successfully installed requests-2.32.4
      
      [notice] A new release of pip is available: 25.1.1 -> 25.2
      [notice] To update, run: pip install --upgrade pip
      (virtenv) (base) peter@MacBookPro sbs % 

      The pip process will install any dependencies which may or may not already be present on your local system so the response you get may vary from mine.

      The key point is you should receive a success message similar to line 10 in the example above.

      Note: whenever you use pip for the first time in a virtual environment, you will receive a notice that your pip version is out of date (lines 12 & 13 in the example). Running an out of date version doesn’t appear to make any difference but you may want to consider upgrading (line 13) if only to remove this annoying message.