In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to import Python into the py file written by you. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
one。 The effect you want to achieve
The newly created folder will be centrally managed by the self-designed module.
Self-programming modules can be import in other py files, just like import third-party libraries
two。 The method of realization
Step 1: put the numbered module * .py into the folder, which on my computer is: F:\ OneDrive\ PythonPrj\ _ myFunction
Step 2: create a new user / system environment variable.
Variable name: PYTHONPATH
Variable value: folder path
Then you can achieve the desired effect, all of the following three import ways.
The rationale for this is that the Python interpreter looks for the import module in the following three locations:
The directory where the current file is located
The directory specified by the environment variable PYTHONPATH
Default installation directory for Python
The sys.path variable in Python records the location of the interpreter Import module, as shown in the following figure:
So, you can tell the interpreter where to find the module by manipulating the sys.path variable, as follows:
Sys.path is a list. Here, a path is added to the list with append, and then the module a under this path can be successfully imported. This method writes two more lines of code, which is inconvenient, so it is only suitable for temporary use.
three。 Advanced usage
If you have more modules, or if you want to add documentation to the module, you still want to store each module in a separate folder to facilitate management and maintenance. Like this:
Then store the self-made module in a folder (such as myPackage1).
At this point, if you want to import the module, you should write it as follows:
This is the writing of one-level subfolders, and more multi-level folders can always click (.) down. Of course, the first-level subfolders are generally used, and the second-level subfolders are hardly used.
four。 Use _ init_.py to control what to do when you import a folder
The above import method can only import one module in a folder at a time. If you want to control all the modules in the import folder at a time, instead of clicking with periods one by one, we need to use the _ init_.py file.
This file needs to be in the same directory as the module, as shown in the following figure, and the folder in Python that owns the _ init_.py file is called the package.
Each time you import myPackage1, _ init.py executes automatically, so it is also called a package initialization file. With this feature, we can write some code in this file to control the behavior when importing the folder myPackage1. Write the initialization file like this:
Import sys# adds the directory where the _ _ init__.py file is located to the Python search directory, otherwise it will prompt that myModule1sys.path.append (_ _ file__ [:-12]) import myModule1 cannot be found.
Then, import myPackage1 in other py files, and because the _ init_.py file executes automatically, the myModule1 file is imported automatically. Verify:
Of course, even if an import module is written in the package initialization file, it can still be imported in such a way as import myPackage1.myModule. The package initialization file does not have any effect on the original import method.
In another way, you can import the functions and properties defined in a module in the package at once, and write the _ init_.py file like this:
Import sys# adds the directory where the _ _ init__.py file is located to the Python search directory, otherwise it will prompt that myModule1sys.path.append (_ _ file__ [:-12]) import myModule1 cannot be found.
Then, when other py files are imported, this is written:
As you can see, you can use the functions defined in myModule1 directly.
To sum up, the import module needs to be written in the same way as _ init_.py, that is, either the import package name or the from package name import * can achieve the desired effect.
I don't have a deep understanding of Python. I just share some of the rules I've found out.
On "Python how to import their own py files" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.
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.