In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how python modifies the search path when package import". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to modify the search path when python package import" can help you solve the problem.
Path search mechanism when module import 1.1 package import search mechanism
The essence of python's import operation is similar to our daily search of files with specified file names on computers, except that python generally does not search on the local disk as we do, but has its own control mechanism, which can control the scope and order of the search, which can ensure efficiency and achieve a certain degree of isolation. For example, python creates a virtual operating environment based on this principle. Different virtual operating environments, python installation path, import search path are isolated, and the core of the commonly used virtual environment maintenance and creation tools is also achieved by modifying the default installation path, version, package search path and other information of python.
In general, when import, the order and scope of package search are as follows:
First search the current folder of the running command, or the current working folder of the system (cwd can be modified by the command)
Then search the file path in the environment variable PYTHONPATH
Finally, search the sitepackages folder in the python installation path (generally, packages installed through pip will be placed under this folder by default, of course, you can also simply put your own packages under this folder, so there is no need to change the search path)
1.2 modify the search path
The core idea is to modify the values of the three search nodes mentioned in 1.1 to change the search scope when the package is imported
1.2.1 by modifying the sys.path variable
Sys.path variable, which manages all the folder paths within the Python package search path (that is, the current folder, the path within the PYTHONPATH environment variable, and the default installation path of python). This variable is a list, and you can add the path to be searched when you want to import it to the list variable through methods such as sys.path.append or sys.path.insert.
However, this method will only take effect at run time, and will become invalid once you exit.
1.2.2 by modifying the PYTHONPATH environment variable
The PYTHONPATH environment variable is a variable that manages the search path when the python package is imported, so the search path can be modified by adding a path to the environment variable, and this method is permanent because it modifies the environment variable, and the impact is global.
For Mac computers, the method to modify this environment variable is as follows:
Open the ~ / .bashrc file, and if not, create it with the touch command
Write export PYTHONPATH=your own dir: $PYTHONPATH in the file. For example, if you want to add the ~ / test path to the environment variable, it is export PYTHONPATH=~/test: $PYTHONPATH
Execute source ~ / .bashrc on the command line (mainly to have the system reload and run the file to make this change take effect)
1.2.3 by adding a * * .pth file
The above two methods are either temporary modification or the impact of global modification is too large, and the third method can be said to have the characteristics of both the first two methods.
You can add a * * .pth file in the site-packages directory folder under the specified Python installation directory, and add the path you want to be searched line by line in the file.
Attachment: the method of temporarily adding python search package path
Method 1: (enter python first)
Import syssys.path.append ('path')
(this method is only valid for the current python)
Method 2:
Export PYTHONPATH= path
(this method works for all python)
This is the end of the content about "how python modifies the search path when the package is imported". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.