pyblish / pyblish-qml

Pyblish QML frontend for Maya 2013+, Houdini 11+, Nuke 8+ and more
GNU Lesser General Public License v3.0
115 stars 44 forks source link

add sys.path to python path #373

Closed hannesdelbeke closed 2 years ago

hannesdelbeke commented 2 years ago

this PR adds support to the QML server working in certain isolated environments without having to do any additional custom env management

follow up from the gitter discussion:

Hannes @hannesdelbeke

in qml, (server.py) we pass the absolute minimum of environment variables to the server, to avoid issues on invalid types

we do pass PYTHONPATH.,

# Append PyQt5 to existing PYTHONPATH, if available
        environ["PYTHONPATH"] = os.pathsep.join(
            path for path in [os.getenv("PYTHONPATH"), pyqt5]
            if path is not None
        )

but we don't pass sys.path.

so python modules accessible to our software (ex. blender), but which are not in the PYTHONPATH, are thus not accessible to our server.

which means QML cant run if it's installed in your user script path.

passing sys.path fixes the issue. is this too specific to create a PR for?

# Append sys.path to PYTHONPATH
        if environ["PYTHONPATH"] is not None:
            environ["PYTHONPATH"] += os.pathsep + os.pathsep.join(sys.path)
        else:
            environ["PYTHONPATH"] = os.pathsep.join(sys.path)

Marcus Ottosson @mottosso May 20 06:44

Heya, before you make a PR, just to clarify a common misconception about QML; the PYTHONPATH used by QML are unrelated to your collector/validator plug-ins. They only relate to finding the PyQt5/PySide2 to draw the UI. Plug-ins are still run in your local environment, with your local sys.path

Hannes @hannesdelbeke May 20 10:24

yes the issue is not with the plugins. those all work fine.

the server needs to import the QML module. if pyblish_qml is not in PYTHONPATH, but only in your sys.path, it will fail to launch qml.

mottosso commented 2 years ago

Thanks for this.

What makes me hesitant, is that we can't be sure what else is on sys.path that may interfere with QML. It'd run within its own Python interpreter, possibly (most likely) of a different version (possibly of differing 32 or 64-bit flavour) that may or may not jive well with everything that's on there. It would be a problem very hard to debug, and on-top of that it would fall outside of what a user could do to solve it. They couldn't manually remove things from their own sys.path.

So preferably, we would find PySide2 or PyQt5 within sys.path and add only that.. But that still means it'll pick up Maya's own internal PySide2 version, which again may not jive with whichever Python is currently running QML.

So, I'm unsure. What are your thoughts? 🤔

hannesdelbeke commented 2 years ago

True, if we pass sys path it'll crash when there are incompatible modules passed cross python version since we run 2 different python versions

However, if we dont pass sys path, the QML module cant be imported if it's in the sys.path and we run only 1 version of python.

since it sounds like this PR introduces the above issue it'll need rethinking.


the issue here is that QML does some env management for you and it mostly works but not always we now try to make it work for more cases, but might then introduce new issues.

The situation i tried to solve is slightly different, more a case of single env. if don;t have python on your pc & only use whatever build in version. you'll never be able to launch QML unless you add it to your env path. since qml only knows about modules in your env path, the qml module won't be detected because it lives in the sys path. which doesn't get passed.

mottosso commented 2 years ago

The design intent is for QML to have its own dedicated Python, where QML is already installed into its own site-packages. A separate, completely independent Python typically located on the network which is only ever capable and meant to be run with Pyblish QML. In that case, you don't need any variables, just set the path to that executable and it'll know where to find QML.

Anything else is an optimisation for 50 mb of disk space and increases the complexity of your setup.

hannesdelbeke commented 2 years ago

yes maybe QML is just not the right solution then for what i'm doing. I treat it more as a fancy UI but use it the same way as pyblish lite. if we want to use it we cant ever assume the user has python installed so need to limit to maya's or blender's python