> For the complete documentation index, see [llms.txt](https://prof-rossetti.gitbook.io/intro-to-python/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://prof-rossetti.gitbook.io/intro-to-python/notes/python/modules.md).

# Python Modules

You can extend the capabilities of your Python program by leveraging, or "importing" other files of code called "modules".

Some selected Python modules of interest include:

* [The `csv` Module](/intro-to-python/notes/python/modules/csv.md)
* [The `datetime` Module](/intro-to-python/notes/python/modules/datetime.md)
* [The `itertools` Module](/intro-to-python/notes/python/modules/itertools.md)
* [The `json` Module](/intro-to-python/notes/python/modules/json.md)
* [The `math` Module](/intro-to-python/notes/python/modules/math.md)
* [The `os` Module](/intro-to-python/notes/python/modules/os.md)
* [The `random` Module](/intro-to-python/notes/python/modules/random.md)
* [The `statistics` Module](/intro-to-python/notes/python/modules/statistics.md)
* [The `time` Module](/intro-to-python/notes/python/modules/time.md)
* [The `webbrowser` Module](/intro-to-python/notes/python/modules/webbrowser.md)

You can also create and import your own modules to help you organize your code into separate logical files.

For more details, follow along with this official tutorial on modules:

* <https://docs.python.org/3/tutorial/modules.html#modules>
* <https://docs.python.org/3/tutorial/modules.html#more-on-modules>
* <https://docs.python.org/3/tutorial/modules.html#executing-modules-as-scripts>

## Usage

To load any module, whether a built-in module or a custom module you create, use the `import` statement. Then after importing the module, you can reference code contained within.

To see this concept in action, create a new directory on your computer called "modules-overview" and place inside the following two files...

Script:

```python
# modules-overview/my_script.py

import my_module

print("IMPORTING MY MODULE ...")
my_module.my_message()
```

Module:

```python
# modules-overview/my_module.py

# anything in the global scope of this file will be executed immediately when the module is imported.
# ... so we generally wrap all the code inside separate functions, which can later be invoked as desired.

def my_message():
    print("HELLO FROM A MODULE")

def other_message():
    print("GREETINGS EARTHLING")

# but if we want something to happen when the module is invoked directly from the command line (as a script)
# ... we can use this special conditional to detect that use case and perform instructions as desired.
if __name__ == "__main__":
    print("INVOKING MY MODULE AS A SCRIPT...")
    my_message()
```

Then execute the script to prove it has access to code in the module:

```bash
python my_script.py
#> IMPORTING MY MODULE ...
#> HELLO FROM A MODULE
```

It is also possible to execute the module directly:

```bash
python my_module.py
#> INVOKING MY MODULE AS A SCRIPT...
#> HELLO FROM A MODULE
```

### Modules in Subdirectories

If your python file is located in a subdirectory, you can reference it using the `[directory name].[file name]`. Like this:

```python
# modules-overview/things/robot.py

def robot_message():
    print("HELLO I'M A ROBOT")
```

```python
# modules-overview/robot_script.py

import things.robot as bot

bot.robot_message()
```

```bash
python robot_script.py
#> HELLO I'M A ROBOT
```
