Popular Posts

Thursday, September 10, 2020

Python Beginner: Modules & Pip

 Hello, developer!


Good day! Hope you're fine today! 😀

Today, we will learn "Modules" & "Pip". A module is an external Python file that has functions or variables. You can load external Python module file in the Python by "import" command.


Module loading command

import Python_module_exercise # Loading the Python_module_exercise file in the same directory.


You can also refer to the "Python Module Index" in the Python official documents site link(https://docs.python.org) for searching out suitable modules on your program. 


Python Module Index


There are two types of modules.

Built-in modules are automatically loaded so you can use these modules.

External modules are needed to import in order to use these modules. In the Python official module list - "Python Module Index" - "External module" shows "Source code" where it was located in the Python library folder like "Lib/base64.py" but "Built-in" modules don't show "Source code" because it was automatically loaded in the Python program. 


External Module example(random in the Python Module Index"


Built-in Module example(random in the Python Module Index"

Also, you can check external Python modules at the following installed Python folders in the PyCharm.

 


For 3rd-party modules, we can use the "PIP" program for the installation of the 3rd-party module package. "PIP" is pre-installed if you installed the latest Python 3 versions but if you are using an older version of Python, you need to install it manually. You can check the "PIP" installation through the below steps in Windows OS.

1. Press the "Windows" key + "R" key

2. Input "cmd" in the input field and press the "Enter" key

3. Opened "Command Prompt Window", input "pip --version" in the prompt line and press "Enter" key.

 4. If you see the version information of pip, "PIP" was installed on your PC.

"PIP" enables you to manage 3rd-party packages with the following features.

 - install: install package(i.e. pip install sample_package)

 - uninstall: uninstall package(i.e. pip uninstall sample_package) 

 - list: list installed packages(i.e. pip list)

 

You can refer more information of "PIP" from the "pip" site(https://pypi.org/project/pip/)

I showed full module exercise codes and execution result as following.

Useful_tools.py

import random
feet_in_mile = 5280
meters_in_kilometer = 1000
beatles = ["John Lenonn", "Paul McCartney", "George Harrison", "Ringo Star"]

def get_file_ext(filename):
return filename[filename.index(".") + 1:]

def roll_dice(num):
return random.randint(1, num)

Module & Pip.py

import Useful_tools

print(Useful_tools.roll_dice(10))


Module imported Python program execution result

3rd-party module package installation using "PIP" execution result.

Can you see the same result on your PC? Congratulations! Great job! Now you can make modules and import these modules for your Python programming.

See you at the next post, Python developer! 😊


No comments:

Post a Comment

Python - Web crawling exercises

Hello Python developer! How are you? Hope you've been good and had wonderful time with Python. There are 10 Python requests module exerc...