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 reference a package in a Python module

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to quote the package in the Python module". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to quote the package in the Python module.

Definition of package

The python package is a special folder for storing multiple interrelated module files and resource files, and there must be a _ _ init__.py file under this folder. The _ _ init__.py file runs automatically when Python imports the package for the first time. Without this file, it will be regarded as a folder, and only if there is this file will it be regarded as a package.

Example of a package

There are 3 files under the folder jihe, which are as follows:

_ _ init__.py file:

From jihe import mianji # Import mianji.py module from jihe import tiji # Import tiji.py module _ _ all__ = ['mianji',' tiji'] # define the module name that can be used by from jihe import *

Mianji.py file:

'' calculated area''def changfx (a, b): return a*bdef yuanx (r): return 3.14159*r*rdef sanjx (a, b): return a*b/2

Tiji.py

'' calculate Volume''def changft (a, b, c): return a*b*cdef zhengft (a): return aquisition 3 reference package in different ways

Test1.py file (using from jihe import * method, the functions that can be used are limited by _ _ all__):

From jihe import * print (mianji.changfx (3)) print (mianji.sanjx (2)) print (mianji.yuanx (5)) print (tiji.changft (2)) print (tiji.zhengft (3)) out:154.078.539752427

Test2.py file (using import jihe method, the functions you can use are not restricted by _ _ all__):

Import jiheprint (jihe.mianji.changfx (3)) print (jihe.mianji.sanjx (2)) print (jihe.mianji.yuanx (5)) print (jihe.tiji.changft (2) 3)) print (jihe.tiji.zhengft (3)) out:154.078.539752427

From XX import * when using resources in the package, omit the package name and write "module name. member name", but there is a risk of naming conflicts.

When using in-package resources in the import XX way, you should write "package name. module name. member name", but there is no risk of naming conflicts.

Alternative methods for defining packages

The following is to write an alternative way to define the package, expand the way of thinking, for your reference.

The previous case is still used, but the _ _ init__.py file is redefined as follows:

From .mianji import * from .tiji import * _ _ all__ = ['changfx',' yuanx','sanjx','changft','zhengft']

Test3.py file (using from jihe import * method, the functions that can be used are limited by _ _ all__):

From jihe import * print (changfx (3)) print (sanjx (2)) print (yuanx (5)) print (changft (2)) print (zhengft (3)) out:154.078.539752427

The _ _ init__.py file uses from XX import *, and test3 also uses from XX import *. All members in the jihe package can be used as local members. The advantage is that you only need to write the member name when using the resources in the package, and the disadvantage is that the probability of naming conflict is greatly increased.

Thank you for reading, the above is the content of "how to reference the package in the Python module". After the study of this article, I believe you have a deeper understanding of how to reference the package in the Python module, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report