Python for Beginners - Part 5 - Function, Module and Virtual Environment
Tags: python
Published: 2019-10-15 23:46:21

![python_titulo](https://thepracticaldev.s3.amazonaws.com/i/7zqzzun9sjvr6jl69ul1.jpg)
Fifth part of articles about the Microsoft's [Python for Beginners](https://www.youtube.com/watch?v=jFCNu1-Xdsw&list=PLlrxD0HtieHhS8VzuMCfQD4uJ9yne1mE6) series.
All the examples from the playlist are available in this GitHub repository: [Python Examples](https://github.com/lucianopereira86/Python-Examples).

### Objective
You will learn how to create functions, importing modules and creating virtual environments.
A Python file was created to demonstrate each example.

## Topics
* [function.py](#functionpy)
* [module.py](#modulepy)
* [venv.py](#venvpy)

### function.py

Creating functions.

![code19](https://thepracticaldev.s3.amazonaws.com/i/rim5hdr6r3obndou91jg.JPG)

Executing the command below:

```batch
python .\examples\function.py
```

The result is:

![code20](https://thepracticaldev.s3.amazonaws.com/i/v3m75cvnchuqbwvy48y3.JPG)

### module.py

Importing the "math_module.py".

![code21](https://thepracticaldev.s3.amazonaws.com/i/5flzkjqno4a8iyndr00g.JPG)

Using its functions.

![code22](https://thepracticaldev.s3.amazonaws.com/i/b51xdvktz77hs3icw2hq.JPG)

Executing the command below:

```batch
python .\examples\module.py
```

The result is:

![code23](https://thepracticaldev.s3.amazonaws.com/i/2dj0hbuqwmqn83vsvwmt.JPG)

### venv.py

Create a virtual environment, install a package and use it in a module.

Execute the command below to create a folder called 'venv':

```bash
python -m venv venv
```

Press the 'Yes' button inside the popup that shows up inside the VSCode.

![code24](https://thepracticaldev.s3.amazonaws.com/i/1n5vt2pw1em53dey5h81.JPG)

The virtual environment will be available in the project root folder.

![code25](https://thepracticaldev.s3.amazonaws.com/i/uub3mro5idg49iusfjmd.JPG)

Run the command below to use it:

```bash
.\venv\Scripts\Activate.ps1
```

The word '(venv)' will be visible in the left side of the current path.

![code26](https://thepracticaldev.s3.amazonaws.com/i/px6y022xbozfwj860rw5.JPG)

And at the bottom of the VSCode, as well.

![code27](https://thepracticaldev.s3.amazonaws.com/i/hhf10hct67pb636zvvst.JPG)

Create a file named 'requirements.txt' at the project root folder and type _colorama_ inside of it.

![code28](https://thepracticaldev.s3.amazonaws.com/i/8c8jttwxqq2lr0lunrjg.JPG)

Install the _colorama_ package inside the venv with the following command:

```bash
pip install -r requirements.txt
```

The result will be:

![code29](https://thepracticaldev.s3.amazonaws.com/i/1ipow07nhd3suwciflym.JPG)

Import _colorama_ inside the _math_module.py_ to write colored messages.

![code30](https://thepracticaldev.s3.amazonaws.com/i/sydlwhvj8felmgmldv9f.JPG)

Finally, run the command below:

```bash
python .\examples\module.py
```

The result will be:

![code31](https://thepracticaldev.s3.amazonaws.com/i/q3dtcnzlvg4xfznye1ve.JPG)

## Next
You will learn how to make connect with web APIs, manipulate dictionaries, read environment variables and use decorators.

### Video References
* [Programming with Python | Python for Beginners [29 of 44]](https://www.youtube.com/watch?v=nrCAxXfRU28&list=PLlrxD0HtieHhS8VzuMCfQD4uJ9yne1mE6&index=29)
* [Programming with Python | Python for Beginners [30 of 44]](https://www.youtube.com/watch?v=C9ZEGqGHXms&list=PLlrxD0HtieHhS8VzuMCfQD4uJ9yne1mE6&index=30)
* [Programming with Python | Python for Beginners [31 of 44]](https://www.youtube.com/watch?v=sKW-zdYZNX4&list=PLlrxD0HtieHhS8VzuMCfQD4uJ9yne1mE6&index=31)
* [Programming with Python | Python for Beginners [32 of 44]](https://www.youtube.com/watch?v=LtKAXFRtxhQ&list=PLlrxD0HtieHhS8VzuMCfQD4uJ9yne1mE6&index=32)
* [Programming with Python | Python for Beginners [33 of 44]](https://www.youtube.com/watch?v=Uei2ILcxuPs&list=PLlrxD0HtieHhS8VzuMCfQD4uJ9yne1mE6&index=33)
* [Programming with Python | Python for Beginners [34 of 44]](https://www.youtube.com/watch?v=_eczHOiFMZA&list=PLlrxD0HtieHhS8VzuMCfQD4uJ9yne1mE6&index=34)
* [Programming with Python | Python for Beginners [35 of 44]](https://www.youtube.com/watch?v=mzpQgMNdR0g&list=PLlrxD0HtieHhS8VzuMCfQD4uJ9yne1mE6&index=35)