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

5 tips to Voice Speech Recognition in Android Marshmallow

Android Marshmallow landed on our Android devices. The opportunity for us to offer a small compilation of tricks to try immediately. The latest version of the Google OS starts (very gently, unhurriedly) to be offered on other devices as Nexus. You will find on Android TM, in the coming weeks, a compilation of the best tips for Android 6.0 Marshmallow. It starts slowly with a video featuring the 10 essential tips & tricks of this new version of the little green robot OS. To continue, we offer a selection of five "tricks" on the management of the battery on Android 6.0 Marshmallow. To enjoy longer your newly updated mobile. Follow the guide. then continue with 5 tips to tame the super-assistant Google Now on Tap. Here you will find 5 "tips" to manage in the best way your applications. We then discuss the quick tips to navigate more easily on this version of the Google OS. We enchanters with features focused on safety and the protection of personal data. We co...

Energy Android TV Play turns your TV into a Smart TV

ENERGY SISTEM Android Energy TV Play, you have a smart TV with Android operating system allows you to convert any traditional TV has announced the launch of a new product. Energy Android TV Play can be connected to the TV to enjoy f the size of a flash drive, a smart phone, a tablet and a computer unconsolidated is a lightweight device. System 1.6 GHz, DDR3 1GB of RAM and a dual-core processor can be expanded using external USB devices, which is the internal memory of 8 GB. It also integrates WiFi and a USB port for connecting external devices. One of its outstanding features, it is easily connected to the TV screen by screen cast application to display the contents of any terminal, making any phone or tablet is synchronized with iOS or Android. All ENERGY SISTEM products one click In addition, through streaming media service applications, images, video or other multimedia content, and game play is the ability to share. With integrated WiFi, the device you want from t...

How to run Python apps on any platform

Credit: Adam Sinicki / Android Authority Want to know how to run Python? It sounds simple, but it can actually be tricky to figure this out. In this post, we’ll discuss how to test your Python code, as well as how to run Python in other contexts: online for example, or as a packaged app. Sometimes, the thing holding you back from learning to code can be extremely simple. I remember wanting to learn to program when I was younger – or learning to take what I’d learned from BASIC on the ZX Spectrum and apply that to a modern environment. My problem? I didn’t know “where” to program. Once I understood C# or Java, where would I enter the code and how would I run it? And whenever I asked someone, they would look at me blankly. What kind of a question is that? Thing is, I had never needed an IDE or an interpreter before. Machines like the ZX Spectrum and Tatung Einstein (any other Einstein users out there?) simply booted up with a prompt to code into! Many people have a similar iss...