Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Pyhton module and package

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to use Pyhton modules and packages". The editor shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to use Pyhton modules and packages" can help you solve the problem.

1. Module

Each Python source code file ending with the extension py is a module name and also an identifier. Global variables, functions, and classes defined in the module in accordance with the naming rules for identifiers are provided to external tool modules for direct use.

1) import Import

Import module name 1, module name 2

Or

Import module name 1import module name 2

After import

Through the module name. Use the tools provided by the module-global variables, functions, classes

Use as to specify the alias of the module

Import module name 1 as module alias

Note: module aliases should conform to the big hump nomenclature.

2) from... Import Import

If you want to import some tools from a module, you can use from. The way import

The import module name imports all the tools in the module at once and is accessed through the module name / alias

# Import a tool from a module from module 1 import tool name

After import

There is no need to pass through the module name.

You can directly use the tools provided by the module-global variables, functions, classes

Note:

If there is a function with the same name in two modules, then the function that imports the module will overwrite the function imported first.

If the code conflicts, you can use the as key to give one of the modules an alias

From... Import * (not recommended)

# Import all tools from module from module name 1 import * 2, search order of the module

When the Python interpreter imports the module, it will:

1. Search for the file with the specified module name in the current directory, and import it directly if any

two。 If not, search the system directory again

Note: when naming a file, do not repeat it with the module file of the system

Each module in Python has a built-in attribute _ _ file__ to view the full path of the module

Third, make sure that the test code under the module will not be executed when it is imported

When you import a file, all code in the file that does not have any indentation will be executed again

_ _ name__ attribute

The _ _ name__ attribute does so that the code of the test module is run only in the case of the test, but not executed when it is imported

_ _ name__ is a built-in property of Python that records a string

If imported by another file, _ _ name__ is the module name

If the currently executed program _ _ name__ is _ _ main__

Example:

# Import module # define global variables # define classes # define functions # at the bottom of the code def main (): #... According to _ _ name__, pass# judges whether to execute the following code if _ _ name__ = ='_ main__': main () 4. Package

A package is a special directory containing multiple modules

There is a special file _ _ init__.py under

Use the import package name to import all modules in the package at once

_ _ init__.py

To use the modules in the package outside, you need to specify a list of modules provided to the outside world in _ _ init__.py

# Import module list from from current directory. Import module name from. Import module name 5. Release module

Steps to make and release a package

1) create a setup.py

Setup.py 's file.

From distutils.core import setupsetup (name= "vvcat_package", # package name version= "1.0", # version description=" used for testing ", # description information long_description=" used to test how to release a compressed package ", # full description information author=" vvcat " # author author_email= "206647497@qq.com", # author email url= "https://blog.csdn.net/qq_44989881?t=1", # homepage py_modules= [" vvcat_package.test1 "," vvcat_package.test2 "]) # modules in the package

First cd to the directory where setup.py is located, and you can see the following files through the ls-l command

2) build the module

Python3 setup.py build

You can see the current directory structure through the tree command

If you do not have the tree command, you can install the

Sudo apt-get install tree

After using the tree command, you will see

The contents of the build folder generated using the python3 setup.py build command

3) generate release package

Python3 setup.py SDU VI. Installation module tar-zxvf package name-1.0.tar.gzsudo python3 setup.py install

After using the tar-zxvf vvcat_package-1.0.tar.gz command

The vvcat_package-1.0 folder appears

Contents of the vvcat_package-1.0 folder

Use the cat PKG-INFO command to see the contents of the PKG-INFO file

Use the sudo python3 setup.py install command to install the vvcat_package module

Looking at the directory where the vvcat_package module is installed, you will see the following files:

Vvcat_package vvcat_package-1.0.egg-info

Test the installed module through ipython3

7. Uninstall the module

Directly from the installation directory, delete the installation module directory.

Cd / usr/local/lib/python3.6/dist-package/sudo rm-r vvcat_package*

Do the following according to the above two commands:

First check the directory where the module is installed

# _ _ file__ is used to view the directory vvcat_package.__file__ where the module is located.

Go to the dist-packages directory through the cd command

Finally, you can uninstall the installed vvcat_package module using the sudo rm-r vvcat_package* command

The following is the only vvcat_package-1.0.egg-info file left after deletion

When the module is called again using ipython3, it is shown that there is no module.

VIII. Pip installs third-party modules

Pip is a modern, general-purpose Python package management tool that provides the functions of finding, downloading, installing and uninstalling Python packages.

The installation and uninstall commands are as follows:

# install the module into the Python 3.x environment sudo pip3 install pygamesudo pip3 uninstall pygame

Install iPython under Mac

Sudo pip install ipython

Install iPython under Linux

This is the end of sudo apt install ipython3's introduction to "how to use Pyhton modules and packages". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report