Python for Beginners - Part 5 - Function, Module and Virtual Environment

Oct 15, 2019

python_titulo

Fifth part of articles about the Microsoft’s Python for Beginners series. You can read the part 4 here. All the examples from the playlist are available in this GitHub repository: 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

Creating functions.

code19

Executing the command below:

python .\examples\function.py

The result is:

code20

module.py

Importing the “math_module.py”.

code21

Using its functions.

code22

Executing the command below:

python .\examples\module.py

The result is:

code23

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’:

python -m venv venv

Press the ‘Yes’ button inside the popup that shows up inside the VSCode.

code24

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

code25

Run the command below to use it:

.\venv\Scripts\Activate.ps1

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

code26

And at the bottom of the VSCode, as well.

code27

Create a file named ‘requirements.txt’ at the project root folder and type colorama inside of it.

code28

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

pip install -r requirements.txt

The result will be:

code29

Import colorama inside the _mathmodule.py to write colored messages.

code30

Finally, run the command below:

python .\examples\module.py

The result will be:

code31

Next

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

Video References


Categories:
Tags: