In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge about "how to import package in python". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Package in Python, is a valid organization code, module can be a file, you can import a module single file, and package is imported as a directory. We'll also see how multiple nesting is imported later.
>>> import collections,socket>>> print(collections.__ path__)['/anaconda3/envs/py38/lib/python3.8/collections']>>> print(socket.__ path__)Traceback (most recent call last): File "", line 1, in AttributeError: module 'socket' has no attribute '__path__'
Let's take a look at Python's annotation libraries, such as collections and sockets, where collections is a package, meaning it's a directory, in Python files, and socket is a module, for socket and we introduced the import module before. Package differs from module in that it has__path__attribute, we can access the path to store python files through__path__. This property does not exist for modules.
In Python, there are two packages: regular package and namespace package.
regular package:
Let's first look at a regular package. Below is a regular package structure.
main.pypkg1---__init__.py
Under the project, we create a folder kpg1 with a file__init__.py, so that package is regular package, so that the name of this folder is a package name, we import package can directly import this package name. Add the following statement to the__init__.py file.
print("importing pk1")def hi_say(): print("pkg1 say hi")
At main.py we import the package i.e. import pkg1, python locates the package by finder, pathFinder locates the package by searching path in sys. path. Do you remember? sys.path The first path is our current directory, which is why python can locate pkg1, which is based on, when we import package python will automatically execute the__init__.py file under package. When you import a package, the module compiles the code object, which we can do with pkg1.hi_say().
print(pkg1.__ path__)
The absolute path to the directory of pkg1 is determined by__path__of pkg1.
print(pkg1.__ file__)
__file__corresponds to the absolute path to the__init__.py file.
print(pkg1.__ package__)
You can also get the name of a package by its__package__attribute, and you can also get the name of a module by__name__.
Next, we further increase the difficulty, that is, create a new mod1.py file under the pkg1 folder.
main.py pkg1 ---__init__.py ---mod1.py
__init__.py file
print("importing pk1")
In the mod1.py file, we print out a message "import mod1" and define the say_hi function. What we want to do next is import this module and execute the say_hi method.
print("importing mod1")def say_hi(): print("pkg1 say hi")import sysimport pkg1print('pkg1' in globals())#Trueprint('pkg1' in sys.modules)#True
We import pkg1 to create a reference to the module object like this and add it to the global variable, which is also added to the sys.modules cache as module.
Then when we directly access mod1 module via pkg1.mod1, we throw the following error, telling the user that this attribute does not exist
AttributeError: module 'pkg1' has no attribute 'mod1'
That is, simply importing a package does not import the modules below it, so the above error occurs. If we want to import mod1 under pkg1 we need to import mod1 correctly using import pkg1.mod1
import sysimport pkg1.mod1
Notice that when we import mod1 the correct way is to import pkg1.mod1, from the output below, python first executes__init__.py, which means python will import package pkg1 first and then import module1 in this order.
importing pk1importing mod1
The say_hi() method in module mod1 can be accessed if:
pkg1.mod1.say_hi()print('pkg1' in sys.modules)#Trueprint('pkg1.mod1' in sys.modules)#Trueprint('pkg1' in globals())#Trueprint('pkg1.mod1' in globals())#False
From the above output we find that both pkg1 and pkg1.modules1 exist in the sys.modules cache, and it is not difficult to see that only pkg1 is located in the global variable, we can only mod1 by pkg1 properties.
"How to import package in python" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality 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.
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.