1

I have two Python environments installed. EnvA which is my default up to date environment and EnvB which has older versions of some packages used in EnvA. Is it possible to launch some code based on EnvB from a program running with the interpreter of EnvA?

As a practical example: I have a pyqt5 GUI which I use to convert my *.ui files, it basically calls the pyuic5 shell command. I would like to add the option of converting .ui to pyqt4 code, which is not possible as is, since the PyQt4 library was replaced by PyQt5 in this environment. Is it possible for my main program to execute a script in a subprocess/interpreter/.. with a different python environment, and return once the script has done its job?

MisterTea
  • 21
  • 1
  • 9
  • Why not just create a third environment that is configured the way you want it to be? Also, are you using Anaconda, virtualenv, or something else? – Mad Physicist May 17 '17 at 14:38
  • the specific package I'm trying to use is a deprecated version of a package I'm already using in my program, so I don't think it's possible. I've added details on what I'm trying to do in the question. Also I'm using Anaconda. – MisterTea May 17 '17 at 17:47
  • Where did you hear that PyQt4 was deprecated? – Mad Physicist May 17 '17 at 17:52
  • "old" might be the correct term? In any case I can't use Pyqt4 in the same environment as Pyqt5 – MisterTea May 17 '17 at 17:56
  • You know the path to `pyuic4`, why not just run it with the full path? – Mad Physicist May 17 '17 at 18:17
  • I did not know that was possible, if so it saves me a lot of trouble! I'll try it out, thanks! – MisterTea May 17 '17 at 18:31

1 Answers1

2

It's not really recommended, but you could probably do this by modifying sys.path. Just append a path to a package from EnvB earlier in sys.path and Python will get pick it up before getting to the EnvA version.

You could also use importlib to specifically import from a fully qualified path. This other answer has some good examples, but basically it looks something like this:

spec = importlib.util.spec_from_file_location("module", "path/to/file.py")
module = importlib.util.module_from_spec(spec)
Community
  • 1
  • 1
Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88