How to introduce third-party modules in Python

How to use third-party modules in Python?

In Python, you can import modules in the code, and then you can use third-party modules.

import statement

To use a Python source file, just execute the import statement in another source file, the syntax is as follows:

import module1[, module2[,... moduleN]

When the interpreter encounters an import statement, it will be imported if the module is in the current search path.

The search path is a list of all directories that the interpreter will search first. If you want to import the module hello.py, you need to put the command at the top of the script:

#! /usr/bin/python
# - *- coding: UTF-8-*-
# Import module
import support
# Now you can call the functions contained in the module
support.print_func("Zara")

The output of the above example:

Hello : Zara

A module will only be imported once, no matter how many imports you perform. This prevents imported modules from being executed over and over again.

From...import statement

Python's from statement allows you to import a specified part from the module into the current namespace. The syntax is as follows:

from modname import name1[, name2[,... nameN]]

For example, to import the fibonacci function of the module fib, use the following statement:

from fib import fibonacci

This declaration will not import the entire fib module into the current namespace, it will only introduce the fibonacci in the fib into the global symbol table of the module executing this declaration.

From...import* statement

It is also possible to import all the contents of a module into the current namespace, just use the following declaration:

from modname import*

This provides an easy way to import all the items in a module. However, this statement should not be used too much.

Knowledge point expansion:

In Python, the installation of third-party modules is done through the tool setuptools. Python has two package management tools that encapsulate setuptools: easy_install and pip. Currently, pip is officially recommended.

If you are using Mac or Linux, the step of installing pip itself can be skipped.

If you are using Windows, please refer to the install Python section, make sure pip and Add python.exe to Path are checked during installation.

Try to run pip in the command prompt window. If Windows prompts that the command is not found, you can re-run the installer to add pip.

The above is the detailed content of how to introduce third-party modules in Python. For more information about the methods of introducing third-party modules in Python, please pay attention to other related articles on ZaLou.Cn!

Recommended Posts

How to introduce third-party modules in Python
How to install third-party modules in Python
How to add custom modules in Python
How to view installed modules in python
How to wrap in python code
How to omit parentheses in Python
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to view errors in python
How to write return in python
How to understand variables in Python
How to clear variables in python
How to use SQLite in Python
How to use and and or in Python
How to delete cache files in python
How to represent null values in python
How to save text files in python
How to write win programs in python
How to run id function in python
How to custom catch errors in python
How to write try statement in python
How to define private attributes in Python
How to understand global variables in Python
How to open python in different systems
How to sort a dictionary in python
How to add background music in python
How to represent relative path in python
How to use the round function in python
How to use the zip function in Python
How to program based on interfaces in Python
How to install python in ubuntu server environment
How to simulate gravity in a Python game
How to use the format function in python
How to use code running assistant in python
How to set code auto prompt in python
Teach you how to write games in python
How to delete files and directories in python
How to install the downloaded module in python
How to write a confession program in python
How to perform continuous multiplication calculation in python
How to comment python code
How to learn python quickly
How to uninstall python plugin
Introduction to Python related modules
How to understand the introduction of packages in Python
How to understand a list of numbers in python
Example of how to automatically download pictures in python
How to save IE as an attachment in python
How to understand python objects
How to use python tuples
How to create a Python virtual environment in Ubuntu 14.04
How to find the area of a circle in python
Do you still know how to draw cakes in python? ? ?
How to install Helm in Ubuntu
python how to view webpage code
How to use hanlp in ubuntu
How to use python thread pool
How to write python configuration file
How to install PHP7.4 in CentOS
How to save the python program