Skip to main content

How to use Python modules

Python Code

Once you know how to add and use a Python module, you will greatly extend the capabilities of the language.

A Python module is an external class or set of functions that exist outside the main file of your program. This can be something that you built yourself, or it can be provided by the community. Either way, the modular nature of Python means these elements can be used to seamlessly extend the capabilities of an application, or to share utilities you’ve built across multiple projects.

In this post, we’ll explore how to add and use a Python module.

How to use built-in modules

Your basic Python installation comes with a host of Python modules that are ready to use. These provide basic functionality that a large proportion of programmers will rely on.

Also read: How to install Python and start coding on Windows, Mac, or Linux

For example, if you want to generate a “pseudo random number” (“pseudo” because there is no such thing as a random number in programming), then you will need to lean on a module called “random.” This is ready and available, so all you need to do is to add the following line to your code:

import random

From here, you will be able to access functions that belong to that module. For example:

import random

n = random.randint(1, 5)
return n

This gives us a number between one and five!

How to get new modules

One of the keys to effective programming is learning not to “reinvent the wheel.” That is to say, that if you need to perform a particular job in your code, there’s a good chance that someone has done precisely that before. You could waste time figuring out how to do it yourself, or you could just use the code that someone else provided for free! Guess which is a smarter use of your time?

The great news is that you don’t need to copy and paste code you found on a forum into your project! Instead, you can just find the Python module that does what you need it to, then grab that to use in your code.

First: search for the thing you want to do. For example, if we want to open Word documents in Python, we could just Google:

“How to open Word documents.”

What we’ll quickly find, is that there is a module available for that, called docx.

To get that Python module, we will use a tool called “pip.” We covered how to use this in our comprehensive beginners’ guide to Python. To access pip open up the terminal or load a command prompt in the directory where Python is installed (unless you added Python to path, in which case that latter point doesn’t matter!).

Now type:

“python –m pip install docx”

Now add this to your code:

import docx

You’ll now be able to access all the functions from this module: such as opening and writing doc files.

Alternatively, if you are using PyCharm, you can simply type that line, and the click on the underlined text and select “install package.”

What’s the difference between a Python module, class, or package?

You might be wondering what a Python “package” is, and how it relates to the Python module. Essentially, a package is a group of modules with an __init__.py fie that ties them all together.

A class is a piece of code that describes a “data object.” An object could be a bad guy in a computer game, or it could be an entry in a contact management database. A single class can be used to create endless “instances” of the object.

Also read: How to use classes in Python

Many classes are kept in separate files, meaning they work just like a Python module. However, not all classes are modules, and likewise, classes can be included in-line in the main flow of your code.

How to make your own Python module

Want to know how to make and use a Python module? It’s extremely simple!

Simply create a new .py file in the same folder as your main Python code. Then “import” that file as you now know how to do, then access the functions from within that module.

So, if we make a new .py file called “useful tools,” this might look like so:

def say_hi():
    print("Hello World!")

From any other Python file, we can then use:

import useful_tools

useful_tools.say_hi()

And that is how you create and use a Python module!

With this useful skill under your belt, you might now be ready to take on more Python challenges. We’ve compiled a list of the best online Python courses to take you to that next level. Check that out here.



source https://www.androidauthority.com/python-module-1149431/

Comments

Popular posts from this blog

4 Surprising Ways Artificial Intelligence will Empower Consumers

Ever wonder what makes Siri search the items inside the iPhone and on the web by recognizing your voice? How does Google listening work? Artificial Intelligence and data science have already infused in this consumer-oriented generation, but we have not realized it yet. It has been quite a while that natural language processing, speech recognition, and gesture recognition system took over the market with their all new features, but what are the new things in AI that are yet to come? Few amazing transformations are: Retailing sector to become AI-ready Voice Recognition will change the game Search engines to become smarter Machine learning to bring revolutionary changes Recently we got to learn about a verbal spat between Elon Musk and Mark Zuckerberg over the probable risk and opportunities brought by AI . Believe it or not, but we already accustomed to Artificial Intelligence and probably waiting to witness jaw-dropping inventions. Some of AI inventions we have known till yet are: Mach

How to unhide or show folders in mx player list

In this blog post, I tell you about how to Show or Hide folders from MX player list. There are two methods to Show folder from MX Player list. Method 1: Unhide / show folders If you want to temporarily Show / Unhide hidden folder from MX Player list, then go to Settings and untick Recognize .nomedia ".  Method 2: Permanently unhide / show folder: Open memory by any file explorer and I recommend X-Plore, and open the folder that is Hidden and find the file " .nomedia ". If you didn't find it, you should first enable "Show files hidden files that starts with .(dot)". Delete the file and you just need to refresh MX Player list to take changes. Note: MX Player always hide those folders which file " .nomedia " exists.

How to use arrays in Python

Arrays in Python give you a huge amount of flexibility for storing, organizing, and accessing data. This is crucial, not least because of Python’s popularity for use in data science. But what precisely is an array? And how do you use arrays in Python? Also read: How to use dictionaries in Python Read on, and we’ll shed some light on the matter. What is an array? An array is a way to store multiple values in a single variable. That means that you can use a single “reference” in order to access your data. A list is also an example of a variable that stores multiple values, but has some slight differences. When using lists in Python, you store a series of values each with a numbered index. For example, this is how you would create a list of fruits in Python: fruits = [“apple”, “orange”, “pear”, “nectarine”] If we then say: print(fruits[3]) We will see “nectarine” appear on the screen (the first entry is stored as “0”). Also read: How to use lists in Python This is not an