In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to import python modules and packages related knowledge, content detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone reading this python module and package how to import articles will have some gains, let's take a look at it.
Python Module 1. import module
In Python, a py file is a module, and to create a module is actually to create a python file.
Note: Module names require specification of symbolic identifiers
Introduction of external modules in one module
① import module name (module name, is the name of python file, be careful not to py)
② import module name as module alias
You can introduce the same module multiple times, but an instance of the module creates only one.
import can be called anywhere in the program, but generally import statements are written at the beginning of the program.
Inside each module there is a__ name __attribute, through which you can get the module name
__ name __attribute value of__ main __module is the main module, a program will have only one main module, the main module is the module we directly execute through python, which file is the main module.
import test_module as test print(test.__ name__)print(__name__)2. import the contents of a module # import m#access variables in a module: module name. variable name #can also be used to import only part of the contents of the module, so that you can use the variable name directly instead of using the module name to access the variable #syntax from module name import variable, variable...# from m import * #All contents introduced into a module are usually not #. Aliases can also be used for introduced variables to avoid conflicts caused by multiple modules having the same variable name.# Syntax: from module name import variable as alias 3. module private variable
With underscore_attribute names, underlined variables are not introduced when all variables are introduced via from m import *.
Note that when importing modules via import m, you can still import them via m._ Property name to access
4. Import modules Skip executable code
When other modules are introduced, there may be executable code in other modules, so that the code in these modules will be executed during import. We want this code to be executed only when the module is the main module.
#Add a statement in front of the executable code of the module #Write test code, this part of the code needs to be executed only when the current file is the main module #When the module is introduced by other modules, it does not need to be executed. In this case, we must check whether the current module is the main module if __name__= '__main_': pass5. package
Package is also a module, when we have too much code in the module, or a module needs to be decomposed into multiple modules, then we need to use the package.
An ordinary module is a py file, and a package is a folder. A package must have a__ init __.py file, which can contain the main contents of the package.
#We can define variables or functions in the__init__.py file inside the package, and then import the package as a module, so that we can access these variables import package_name#Package can contain other modules, when using variables inside the module, you must use the full pathname: package_name.module_name. attribute name import package_name.module_name #When importing in this way, you only need to write the module name, you don't need the package name from package_name import module_name
__ pycache __is the cache file of the module, after compilation there will be a cache file under the package
py code needs to be converted into machine code by the parser before execution, so when we use the module (package), we also need to convert the module code into machine code before being executed by the computer. In order to improve the performance of the program, python will save the code to a cache file after compiling once. In this way, when loading the module (package) next time, we can not recompile but directly load the compiled code in the cache.
sys standard library import sysimport pprint#Gets the parameters included in the command line when executing code, this property is a list that holds all the parameters of the current command sys.argvprint (sys.argv)#Get all modules introduced in the current program,modules is a dictionary, the dictionary key is the module name, the dictionary value is the module object sys.modules#pprint module It provides us with a method pprint() which can be used to simply format printed data pprint.pprint (sys.modules)#This is a list of module search paths sys.path#function used to exit the program sys.exit()sys.exit ('program exception, end! ') import modules under specified directory from sys import path#import path variable under sys module, this is a list, add module search path.append(r"E:\\") import variables in module from test0 import k print(k) About "Python modules and packages how to import" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of "how to import python modules and packages" knowledge. If you still want to learn more knowledge, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.