1

I have two versions of the same package with the following structure:

pkgs
├── v1
│   ├── pkg
│   │   ├── main.py
│   │   └── utils.py
│   └── setup.py
└── v2
    ├── pkg
    │   ├── main.py
    │   └── utils.py
    └── setup.py

I want to first use function from v1 main.py and then use the same function but from v2 main.py. I can't import pkg by path since it imports another function from utils.py using import pkg.utils, so pkg have to be installed.

I tried reinstalling the package using pip and reloading:

import pkg
subprocess.check_call([sys.executable, "-m", "pip", "install", "--quiet", "pkgs/v2"])
pkg = reload(pkg)

that doesn't work, the python reloads v1 version.

Is there a way to make this work?

EDIT: Code example:

    subprocess.check_call([sys.executable, "-m", "pip", "uninstall", "--quiet", "-y", module_name])
    subprocess.check_call([sys.executable, "-m", "pip", "install", "--quiet", "-e", pkg_dir])

    importlib.invalidate_caches()
    module = importlib.import_module(module_name)
    pkgfunc = getattr(module, "func")
Rizhiy
  • 775
  • 1
  • 9
  • 31
  • This usage of the modules system is a bit abusive :). Did you try changing `sys.path` before calling `reload`? Can you overcome this using `import pkg as pkgv1` change `sys.path` and then `import pkg as pkgv2`? – Amitai Irron May 15 '20 at 10:53
  • I'm using `importlib.import_module()`. so not sure if `import ... as` is applicable. – Rizhiy May 15 '20 at 11:14
  • It seems that you are correct and `sys.path` is not being updated, will try to check that. – Rizhiy May 15 '20 at 11:18
  • 1
    Updating `sys.path` didn't help – Rizhiy May 15 '20 at 11:29
  • The mechanism through which you access module content is not specified in your question, and may have significance here. There are methods for importing modules directly from fully specified file names, and you can even alias modules (e.g., `import re, os; re = os.path` - not that you want to do that). I'm knd of stabbing in the dark here, because your end goal is not clear, but you can seriously mess around with Python's module import system (I have even made import work over HTTP - don't ask). – Amitai Irron May 15 '20 at 11:50
  • @AmitaiIrron Added code example – Rizhiy May 15 '20 at 21:11
  • Would this be helpful - https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path - ? – Amitai Irron May 15 '20 at 21:21
  • Does it work if the file imports from other files in the same folder? – Rizhiy May 17 '20 at 02:02
  • I am not deep into this topic but I regularly see questions like this come up and I have never seen a full comprehensive answer. As far as I can tell there are indeed things that one can do (with _importlib_ obviously), but they all have their own limitations, they might be good enough for some use cases but on the long run they don't actually work. The consensus seems to be that it's easier to reload the whole process. Maybe more background to read in this (recent) [discussion](https://news.ycombinator.com/item?id=23498506). – sinoroc Jun 14 '20 at 12:42

0 Answers0