Modules are a way of packaging and segmenting code to help with organization. Python comes with a huge selection of modules already built-in called the python standard library. This includes everything from ways to access the computers operating system (the os module) to random number and choice generators (the random module), to even an etch-a-sketch style drawing module called turtle.

Module imports

In python you can import modules in a few different ways. For example, in python there is a math module for doing math operations:

import math # Brings the whole math module in

print(math.sqrt(4)) # Prints the square root of 4, which is 2

One thing you will notice with this is that I had to type math.sqrt() to call the function. This is because I imported the whole module, which means I need to tell python what to run, and from which module. A better way of doing this example would be to use the from module_name import function.

Using the previous math example from before:

from math import sqrt # brings in JUST the sqrt() function from the math module

print(sqrt(4)) # Prints the square root of 4, which is 2

Another neat trick is to import a function with an alias (another name). Using the previous example we could also write:

# brings in JUST the sqrt() function from the math module and aliases it to square_root()
from math import sqrt as square_root

print(square_root(4)) # Prints the square root of 4, which is 2

Modules are essential to building larger applications, but now that you know how to use them let's look at what they actually are. Modules are just plain old python files, buried deep inside the guts of your python installation there is a file called math.py with a function called sqrt().

Writing your own modules

There are actually many ways to write your own modules, but the simplest is just to link separate python files together inside the same folder.

Let's assume you have a project that is structured like this:

├── /project
|   ├── my_program.py
|   └── my_module.py

Now lets assume your files look like this:

my_module.py

def my_module_function():
    print("Hello from my_module.py")

my_program.py

from my_module import my_module_function # import my_module_function() from the my_module.py file

if __name__ == "__main__":
    my_module_function() # Run my_module_function() from my_module.py

As you can see you can import 'modules' by using the filename without the .py.

NOTE: this explanation of modules is incredibly simplified, if you are looking at writing your own module to put online please read a more in-depth guide about packaging: https://packaging.python.org/tutorials/packaging-projects/ (I will also have a section dedicated to writing 'community' modules in PYTH-202).

pip; The python package manager

In python the most popular way of managing modules (sometimes called packages) is with a utility called pip. If you have followed my instructions for installing python you should already have pip installed. You can find out if it's installed by typing pip(windows) or pip3 (linux/mac) into your command line. If you do not have pip installed, please go back and look at the installation instructions in module 0.

pip allows you to download and run modules that other people have created on pypi (The python package index; I explain more about this later), or install modules that you have created specifically for pip. Using pip you can get access to thousands of modules that do everything from machine learning, to web development, to interfacing with other services.

The simplest way to use pip is to just type pip install <project name> on windows or pip3 install <project name> on mac/linux.

requirements.txt

It is common in python projects to store a list of dependencies necessary to run a project in a file called requirements.txt. For example lets say you have a project that requires you to install requests, tqdm and Flask you can write a requirements.txt file that would look like this:

requests
tqdm
Flask

Once you have the file you can get pip to install everything listed by using the -r (read from file) flag. i.e. pip install -r requirements.txt.

PyPI. (The python package index)

PyPI. is a website that hosts python modules (sometimes called packages). In every previous example of using a module that is not part of the Python Standard Library, we have been installing packages from PyPI. For example Flask is hosted by PyPI. which is why when we type pip install flask, what actually happens is that pip looks up flask on PyPI. and installs it from there.

Typically most packages can be installed through pip using PyPI.

Exercises

""" =========== Exercise 1 ============= import the math module and use math.sqrt() on a user inputted variable. NOTE: you have the input converted to an int automatically using int(input()). """ """ =========== Exercise 2 ============= import the sleep function from the time module as pause and run pause(5) """ """ =========== Exercise 3 (question) ============= import the sleep function from the time module as pause. Write a function called pause() that takes an argument and prints it. Which pause() will be called, and why? """

Challenges

def challenge_one(): """ =========== Challenge 1 ============= TQDM is a third-party module that allows you to create progress bars for loops. Figure out how to install the python module TQDM with pip and look at the documentation to help figure out how to create a for loop with a progress bar that goes through 10 iterations at 1 iteration a second. I have setup a skeleton version of the code below that will get you 90% of the way there, all you need to do is figure out the TQDM part. Hint: You only need to make an adjustment to line 30 """ import time for number in range(10): time.sleep(1) #Waits one second before continuing. def challenge_two(): """ =========== Challenge 2 ============= Create a python module (another file in the same folder) called my_module.py Inside of my_module.py define a function called print_backwards(). This function should take a string and return the string in reverse. For example: print_backwards('hello') # returns 'olleh' You will know that you did it right if when you run this challenge_two() function it prints 'olleh'. Hint: You can reverse a string using reversed() """ # from my_module import print_backwards # Uncommnet when you have made the file # print(print_backwards("hello")) # Uncommnet when you have made the file """ =========== Challenge 3 ============= Go to https://github.com/Descent098/Resume-Site, download the repository and run the application. """

Solutions




""" =========== Exercise 1 ============= import the math module and use math.sqrt() on a user inputted variable. NOTE: you have the input converted to an int automatically using int(input()). """ import math value = int(input("Enter a number to square: ")) print(math.sqrt(value)) """ =========== Exercise 2 ============= import the sleep function from the time module as pause and run pause(5) """ from time import sleep as pause pause(5) """ =========== Exercise 3 (question) ============= import the sleep function from the time module as pause. Write a function called pause() that takes an argument and prints it. Which pause() will be called, and why? """ """If you write a function called pause() it will run instead of the imported pause function because python runs files top down meaning that it will overwrite the imported function when defined. """ def challenge_one(): """ =========== Challenge 1 ============= TQDM is a third-party module that allows you to create progress bars for loops. Figure out how to install the python module TQDM with pip and look at the documentation to help figure out how to create a for loop with a progress bar that goes through 10 iterations at 1 iteration a second. I have setup a skeleton version of the code below that will get you 90% of the way there, all you need to do is figure out the TQDM part. """ import time from tqdm import tqdm for number in tqdm(range(10)): time.sleep(1) #Waits one second before continuing. def challenge_two(): """ =========== Challenge 2 ============= Create a python module (another file in this folder) called my_module.py Inside of my_module.py define a function called print_backwards(). This function should take a string and return the string in reverse. For example: print_backwards('hello') # returns 'olleh' You will know that you did it right if when you run this challenge_two() function it prints 'olleh'. Hint: You can reverse a string using reversed() """ # from my_module import print_backwards # Uncommnet when you have made the file # print(print_backwards("hello")) # Uncommnet when you have made the file """Since I can't add the extra file here this is what it would contain: my_module.py ------------ def print_backwards(value): print(reversed(value)) """ """ =========== Challenge 3 ============= Go to https://github.com/Descent098/Resume-Site, download the repository and run the application. """ """There are 3 steps to this 1. Go to the link and download the zip file of the project 2. run pip install -r requirements.txt inside the newly extracted project 3. read the documentation to find out you need to run python routes.py to start the project """