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

What are the knowledge points of Python packages and modules

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "what are the knowledge points of Python packages and modules". In daily operation, I believe many people have doubts about what are the knowledge points of Python packages and modules. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "what are the knowledge points of Python packages and modules"! Next, please follow the small series to learn together!

0. A module is a py file, and the name of the module is the name of the file (without suffixes).

A package is a folder (Python2 specifies that the folder must contain an__init__.py, Python 3 does not require it), and the package name is the folder name.

2. There are four scenarios according to the different objects imported:

1. import #Import a package

2. import #Import a module

3. from import #Import modules/subpackages/objects from a package

4. from import #Import objects from modules

The interpreter looks up the package or module names that are introduced in the order of the sys.path list.

>>> import sys

>>> import pprint

>>> pprint.pprint(sys.path)

['',

'C:\\Python\\Python35-32\\python35.zip',

'C:\\Python\\Python35-32\\DLLs',

'C:\\Python\\Python35-32\\lib',

'C:\\Python\\Python35-32',

'C:\\Python\\Python35-32\\lib\\site-packages']

Load modules in the current working directory first. If you use a package or module name with the same name as the built-in module in your project, you will encounter an error prompt such as no xx attribute. Novices like to do this.

You can manipulate sys.path so that files from other paths are added to Path so that they can be discovered by the interpreter.

# test.py

import sys, os

#There is no hi module in the current directory, and an error is reported that the module cannot be found

import hi

Traceback (most recent call last):

ImportError: No module named hi

# hi module location: /data/hi.py

#Add hi module to sys.path

sys.path.append("/data")

It works normally

import hi

5. Another way to load modules: If your module is not under sys.path, in addition to recommendation 4, you can also use the imp module method imp.load_source

import imp

imp.load_source("hi", "C://data/hi.py")

import hi

#You can specify the module name yourself, which is equivalent to import hi as h3

imp.load_source("h3", "C://data/hi.py")

import h3

When importing module, all code in the module will be executed (class object, function object will be created, not called), when importing package, the code in__init__.py file will also be executed.

7.__file__attribute of module

When importing a module, you can view the path location of the disk where the module resides through the module's__file__attribute

>>> import requests

>>> requests.__ file__

'D:\\Programs\\Anaconda3\\envs\\py_test\\lib\\site-packages\\requests\\__init__.py'

Never use from import *, there are unpredictable risks

At this point, the study of "What are the knowledge points of Python packages and modules" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

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