1

Let's assume I import a custom python module as follows: from folder1.folder2 import module

I use this import statement in multiple files. Can I add this file path in the environment variables so that I can use it in a dynamic way as follows or in a similar fashion:

(ENVIRONMENT VARIABLE)
EXPORT env_var_path=folder1.folder2

(PYTHON CODE)
from os.getenv("env_var_path") import module

The reason why I'm adding it in the env variables is that my folder names will change and keeping it in the env variables makes it a single source of change whenever I have to.

Thanks!

Pallav Doshi
  • 209
  • 2
  • 9
  • 3
    Does this answer your question? [Import arbitrary python source file. (Python 3.3+)](https://stackoverflow.com/questions/19009932/import-arbitrary-python-source-file-python-3-3) - another possible dupe using [importlib.import_module](https://stackoverflow.com/questions/10675054/how-to-import-a-module-in-python-with-importlib-import-module) – Mike Scotty Sep 27 '21 at 14:55
  • You can just use the `PYTHONPATH` env var: directories first on it will be searched first by Python import mechanism. – jsbueno Sep 27 '21 at 14:59
  • @MikeScotty I can try that, but that doesn't look that elegant to me, is it? – Pallav Doshi Sep 27 '21 at 15:02
  • @jsbueno The reason why I'm adding it in the env variables is that my folder names will change and keeping it in the env vars makes it a single source of change whenever I have to. – Pallav Doshi Sep 27 '21 at 15:03
  • @PallavDoshi not sure what you mean by "doesn't look that elegant", it's literally the library to import modules in python, it's simply what must be used in this situation. – Shinra tensei Sep 27 '21 at 15:07

0 Answers0