Interview question 2:
Part ONE:
- Sets the way python modules search for paths
- Set the search path in PyCharm
- Set the search path in the console
Part 2 — Interview Questions:
1. Interview question 1: There are several ways to set the search path for python modules.
2. Interview question 2: There are several ways to permanently set python module search paths, and how to use them.
3. Interview question 3: How to temporarily set the search path for Python modules.
Part III — Analysis:
Interview question 1: Several ways to set the search path for Python modules:
- Set the PYTHONPATH environment variable: Click on the button to the left of the PyCharm run button and select Edit Configurations… “, and then click the three points on the right of Environment variables to open and add. Write PYTHONPATH in Name and the address of the module to be imported in Value.
- Add a.pth file: Open python’s root directory in the console, such as Linux, and create a.pth file in the python package directory, where vi writes the address of the module to be imported. How it works: Python scans files with a. PTH suffix (if any) at runtime and automatically adds the path to sys.path.
- Path: import sys sys.path.append(‘ path ‘)
- If pyCharm is used, you can set the search path directly: right-click the module you want to import, click the Mark Derectory as option, click the first one, and make it the Python source root.
Several ways to permanently set the python module search path and use it:
(1,2,4 of these three methods!)
- Set the PYTHONPATH environment variable
- Add the.pth file
- If pyCharm is used, the search path can be set directly
How to temporarily set the search path for Python modules:
(Number 3 of interview question 1!)
- Run the sys.path command to set the path