1. Add
to the program using sys.path, for example:
import sys sys.path.append('/Users/username/Documents/') sys.path
and you can use sys.path to output what is currently included directory.
2. Use the .pth file to add
Add a .pth file to the lib/python/site-packages/ directory in the directory where lib/python is installed.
What you need to pay attention to here is where the directory where python is installed is.
For example, if you use the my_project environment of the virtual environment in conda, it should be /Users/username/opt/anaconda3/envs/fealpy Add a mypath.pth file to the directory /lib/python3.8/site-packages. The content in
mypath.pth is the directory to be added:
/Users/username/Documents/ /Users/username/Downloads/
After adding, the same You can call sys.path in python to view:
import sys sys.path
Put the address of your project in the sys.path list to facilitate the quick import of modules.
import os, sys
# 获取当前用户的home目录 module = os.path.expanduser('~') # 打印当前用户的home目录 print(module) # 定义home目录的搜索优先级,0为最高优先级 # sys.path.insert()加入的也是临时搜索路径,程序退出后失效。 sys.path.insert(0, module) # 打印搜索路径 print(sys.path)
The above is the detailed content of How to add search path in Python. For more information, please follow other related articles on the PHP Chinese website!