Python

How to Import Modules and Libraries in Python

Learn how Python modules, standard-library imports, third-party packages, aliases, and package installation work in notebooks and terminals.

Python modules and libraries being connected through import statements

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 packages such as NumPy and BeautifulSoup. They must be installed in the active Python environment before they can be imported.

                                
                                    %pip install numpy
                                
                            

%pip install numpy is notebook syntax for Jupyter and IPython. In a terminal, use python -m pip install numpy so the package is installed for the Python interpreter you intend to run.

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.
  • Use %pip install inside a notebook and python -m pip install in a terminal.
  • 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!

Sources and further reading

Continue the conversation

Have a correction, collaboration idea, or technical topic to discuss?

Contact Lance