I have four projects. Three of them (A, B, C) have custom Python module (which has same name but there are differences in functionality) that fourth project (D) is using.
In project D I need to import that module, but which (from project A, B or C) will be imported depends on provided path. How can I achieve this? I have this (pseudo)code:
import mymodule
# code
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(--path)
path = args[0].path
# now I need to use this variable 'path' to insert it to sys.path so that `import mymodule` will work
sys.path.insert(0, path)
This, of course, won't work because Python interprets code line by line from the beginning of file. So it will throw exception ModuleNotFoundError
.
Is there some way to achieve this except adding this part of code with argparser
at the top of file before line `import mymodule'?