Skip to content

Instantly share code, notes, and snippets.

@ayuLiao
Last active November 22, 2018 06:02
Show Gist options
  • Select an option

  • Save ayuLiao/5b1c880f634882188eb9db4aae29e887 to your computer and use it in GitHub Desktop.

Select an option

Save ayuLiao/5b1c880f634882188eb9db4aae29e887 to your computer and use it in GitHub Desktop.
python程序中使用 import XXX 时,python解析器会在当前目录、已安装和第三方模块中搜索 xxx,如果都搜索不到就会报错。
'''
加入上层目录和绝对路径
'''
import sys
sys.path.append('..') #表示导入当前文件的上层目录到搜索路径中
sys.path.append('/home/model') # 绝对路径
from folderA.folderB.fileA import functionA
'''
加入当前目录
'''
import sys
sys.path.append(os.getcwd())
'''
定义搜索优先顺序
'''
import sys
sys.path.insert(1, "./model")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment