IntroductionThe Python programming language comes with a variety of built-in functions. Among these are several common functions, including: Show
These built-in functions, however, are limited, and we can make use of modules to make more sophisticated programs. Modules are Python Modules can define functions, classes, and
variables that you can reference in other Python In Python, modules are accessed by using the When Python imports a module called This tutorial will walk you through checking for and installing modules, importing modules, and aliasing modules. PrerequisitesYou should have Python 3 installed and a programming environment set up on your computer or server. If you don’t have a programming environment set up, you can refer to the installation and setup guides for a local programming environment or for a programming environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.) Checking For and Installing ModulesThere are a number of modules that are built into the Python Standard Library, which contains many modules that provide access to system functionality or provide standardized solutions. The Python Standard Library is part of every Python installation. Info: To follow along with the example code in this tutorial, open a Python interactive shell on your local system by running the To check that these Python modules are ready to go, enter into your local Python 3 programming environment or server-based programming environment and start the Python interpreter in your command line like so:
From within the interpreter you can run the
Since Let’s run the
If
You can deactivate the Python interpreter with Next, we can use
Once it is installed, you can import Importing ModulesTo make use of the functions in a module, you’ll need to import the module with an
An In a Python file, this will be declared at the top of the code, under any shebang lines or general comments. So, in the Python program file my_rand_int.py
When we import a module, we are making it available to us in our current program as a separate
namespace. This means that we will have to refer to the function in dot notation, as in In practice, with the example of the
Let’s create a
my_rand_int.py
This small program first imports the When we run the program with
The integers should never go below 1 or above 25. If you would like to use functions from more than one module, you can do so by adding multiple my_rand_int.py
You may see programs that import multiple modules with commas separating them — as in To make use of our additional module, we can add the constant my_rand_int.py
Now, when we run our program, we’ll receive output that looks like this, with an approximation of pi as our last line of output:
The Using from … importTo refer to items from a module within your program’s namespace, you can use the In this construction, you can specify which definitions to reference directly. In other programs, you may see the Let’s first review at importing one specific function, my_rand_int.py
Here, we first call the Now, when we implement this function within our program, we will no longer write the function in dot notation as my_rand_int.py
When you run the program, you’ll receive output similar to what we received earlier. Using the Aliasing ModulesIt is possible to modify the names of modules and their functions within Python by using the You may want to change a name because you have already used the same name for something else in your program, another module you have imported also uses that name, or you may want to abbreviate a longer name that you are using a lot. The construction of this statement looks like the following:
Let’s modify the name of the my_math.py
Within the program, we now refer to the For some modules, it is commonplace to use aliases. The
This allows programmers to append the shorter word ConclusionWhen we import modules we’re able to call functions that are not built into Python. Some modules are installed as part of Python, and some we will install through Making use of modules allows us to make our programs more robust and powerful as we’re leveraging existing code. We can also create our own modules for ourselves and for other programmers to use in future programs. How do u import library in Python?Import in python is similar to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way.
How do I install and import libraries in Python?Procedure. Install launcher for all users.. Add Python to the PATH.. Install pip (which allows Python to install other packages). Install tk/tcl and IDLE.. Install the Python test suite.. Install py launcher for all users.. Associate files with Python.. Create shortcuts for installed applications.. Why do we import libraries in Python?In Python, you use the import keyword to make code in one module available in another. Imports in Python are important for structuring your code effectively. Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable.
|