h0945466 发表于 2017-5-7 12:11:21

python import非当前目录下的文件

  将被import的模块的路径,添加到sys.path中即可
  如下:
if not "/home/wu/script/tuner/mobile_request/etc" in sys.path:
sys.path.append("/home/wu/script/tuner/mobile_request/etc")
if not 'etc' in sys.modules:
etc = __import__('etc')
  1.如果该路径不在path中,则添加
2.如果模块不在标准模块中,则添加
3.最后执行导入即可
  缺点:这是临时添加的,当前脚本运行结束就消失,每个脚本开头都要写
优点:比较方便,随意导入,也正是因为缺点的存在,才不用担心把path添乱了,多个脚本相互不影响
页: [1]
查看完整版本: python import非当前目录下的文件