Using Modules & Pip In Python | Python Tutorials For Absolute Beginners

Introduction

Python is a strong and flexible language is used to develop various types of programs with the least complex code. Python programming language has a rich set of tools and resources, known as modules and packages, and this is one of the critical aspects that make it important and impressive. These help you to add further capabilities to your programs, although you don’t write all the code starting from the scratch.

In this tutorial, we will discuss about steps in using modules incorporate into Python and how can uses the package of pip in Python.

Understanding Modules

What is a Module?

Having learned about Python modules it is important to pointed out that a module is simply a file containing Python definitions and statements. This is an agreed understanding of the way the file name is as the module name with the extension of . py added. Modules are used to create function definitions, class definition, as well as variable that one would wish to reuse in their coding process.

Importing Modules

So let us call a module in your Python program is to use the keyword ‘import’. This feature has been provided by the Python language using the built-in function known as import.

Example

Now let’s create a simple module with the name - mymodule. py:


You can now use this module in another Python script:


When you run ' main.py ' you should see:


Importing Specific Functions or Variables

You can also import specific functions or variables from a module:

Using Built-in Modules

Python comes with a large standard library of modules. For example, the math module provides mathematical functions:

Managing Packages with pip

What is pip?

pip is the package installer for Python. You can use it to install and manage additional libraries and dependencies that are not included in the standard library.

Installing Packages

To install a package using pip, you use the install command followed by the package name:

For example, to install the popular requests library, you would run:

Using Installed Packages

Once a package is installed, you can import and use it in your Python scripts:

Listing Installed Packages

To see a list of all installed packages, use:

Uninstalling Packages

To uninstall a package, use the uninstall command followed by the package name:

Post a Comment

Previous Post Next Post