How to Import Modules and Libraries in Python

Published by Lance Adhikari on July 14, 2025 · 9 min read

Importing Python Libraries

Python allows you to extend your coding power by importing reusable code from modules and libraries. Whether you're analyzing security logs or working with data, learning how to properly import and use modules is an essential step. This article walks you through how to import both built-in and external Python modules, with practical examples focused on real-world tasks.

What Are Modules and Libraries?

A module is simply a Python file containing useful code: variables, functions, and classes. A library is a collection of related modules. Python comes with a built-in set of these called the Standard Library, and you can also download external libraries for more advanced tasks.

Common Standard Library Modules

  • re: pattern matching in logs
  • csv: reading and writing CSV files
  • glob and os: file system and shell interaction
  • time and datetime: managing timestamps
  • statistics: analyzing numeric data

Importing a Full Module

To import everything from a module, use the import keyword. For example:

                                
                                    import statistics
                                    monthly_failed_attempts = [20, 17, 178, 33, 15, 21, 19, 29, 32, 15, 25, 19]
                                    mean_failed_attempts = statistics.mean(monthly_failed_attempts)
                                    print("mean:", mean_failed_attempts)
                                
                            

Output:

                                
                                    mean: 35.25
                                
                            

You can also calculate the median using another function from the same module:

                                
                                    median_failed_attempts = statistics.median(monthly_failed_attempts)
                                    print("median:", median_failed_attempts)
                                
                            

Output:

                                
                                    median: 20.5
                                
                            

Importing Specific Functions

If you don’t need the whole module, use from ... import ... to import only what you need. This avoids typing the module name before every function call:

                                
                                    from statistics import mean, median

                                    monthly_failed_attempts = [20, 17, 178, 33, 15, 21, 19, 29, 32, 15, 25, 19]
                                    print("mean:", mean(monthly_failed_attempts))
                                    print("median:", median(monthly_failed_attempts))
                                
                            

This is cleaner and improves readability for longer scripts.

Installing and Importing External Libraries

Python also supports third-party libraries like NumPy and BeautifulSoup for advanced tasks. These must be installed before importing:

                                
                                    %pip install numpy
                                
                            

Then, import as you would a built-in module:

                                
                                    import numpy
                                 
                            

External libraries open up capabilities for web scraping, data analysis, machine learning, and much more.

Key Takeaways

  • Modules contain reusable code; libraries are collections of modules.
  • Use import module_name to import everything from a module.
  • Use from module_name import function_name to only bring in what you need.
  • Install external libraries like NumPy or BeautifulSoup using %pip install.
  • Use built-in modules like statistics for calculations directly in Python.

Mastering imports makes your code more efficient and gives you access to the full power of Python’s ecosystem. Whether you’re building a data pipeline or analyzing logs, importing modules is an essential step.

Have questions or suggestions for other modules to explore? Feel free to reach out!