add parent dir to module search path only when importing fails

This commit is contained in:
ander 2020-10-16 13:42:16 +03:00
parent 86057cef19
commit fd04c03613

View file

@ -25,13 +25,19 @@ import logging
import os import os
import sys import sys
cdist_bin = os.path.abspath(__file__) # try to import cdist and if that fails, then add this file's parent dir to
if os.path.islink(cdist_bin): # module search path and try again. additionally check if this file is
# symlinked, so user can symlink this file to, for example, ~/.local/bin.
try:
import cdist
except ModuleNotFoundError:
cdist_bin = os.path.abspath(__file__)
if os.path.islink(cdist_bin):
cdist_bin = os.readlink(cdist_bin) cdist_bin = os.readlink(cdist_bin)
cdist_dir = os.path.abspath(os.path.join(os.path.dirname(cdist_bin), os.pardir)) cdist_dir = os.path.abspath(os.path.join(os.path.dirname(cdist_bin), os.pardir))
sys.path.insert(0, cdist_dir) sys.path.insert(0, cdist_dir)
import cdist
import cdist
import cdist.argparse import cdist.argparse
import cdist.banner import cdist.banner
import cdist.config import cdist.config