Python - How "Import" Directive Works
Tags: python, debugging, tutorial
Published: 2024-04-09 10:52:57

---
title: Python - How "Import" Directive Works
published: true
description: Learn how to use the "import" directive works in Python, a leading language for programmers
tags: python, debugging, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/anf35augxlo3uobd4xd8.jpg
canonical_url: https://blog.appseed.us/python-how-import-directive-works/
---

Hello Coders! This article explains the **["import" directive in Python](https://blog.appseed.us/python-how-import-directive-works/)**. How it works and how can be used in different contexts. The import directive in Python is used to bring modules or packages into your current namespace, allowing you to use the functionality defined within them. Thanks for reading!

--- 

> Let's break down how it works in detail:

---

## ✅ Basic Importing

You can import a module or package in Python using the import keyword followed by the name of the module or package:

```python
import module_name
```

OR

```python
import package_name
```

---

## ✅ Using Imported Items

Once imported, you can use functions, classes, or variables defined in the module or package by prefixing them with the module or package name:

```python
module_name.function_name()
```

OR

```python
package_name.module_name.function_name()
```

---

## ✅ Importing Specific Items

You can import specific items from a module instead of the entire module using the `from` keyword:

```python
from module_name import function_name
```

You can also import multiple items from a module using comma separation:

```python
from module_name import function_name1, function_name2
```

---

## ✅ Aliasing

You can provide an alias to imported modules or items using the `as` keyword:

```python
import module_name as alias_name
```

OR 

```python
from module_name import function_name as alias_name
```

---

## ✅ Importing All Items

You can import all items from a module using the `*` wildcard:

```python
from module_name import *
```

However, it's generally not recommended because it can lead to namespace pollution and makes it unclear where each item is coming from, and also imports items unused in the current context.

---

## ✅ Importing Packages

If you want to import a module from a package, you use dot notation:

```python
import package_name.module_name
```

OR 

```python
from package_name import module_name
```

---

## ✅ Execution of Import Statements

Import statements are executed just like any other statements, and they are typically executed only once per interpreter session. After a module is imported, it's cached in `sys.modules`, so subsequent imports of the same module do not cause it to be re-executed.

---

## ✅ Conclusion

In summary, the import directive in Python is a powerful tool for bringing in functionality from modules and packages. It allows you to organize your code into reusable and modular components, making it easier to manage and maintain large codebases. Understanding how to use import effectively is essential for writing clean and maintainable Python code.

---

> **Thanks for reading!** For more resources, feel free to access:

- 👉 [AppSeed](https://appseed.us/), a platform already used by 8k registered developers
- 👉 Build apps with [Django App Generator](https://app-generator.dev/django/) (free service)