dynamically changing sys.path

42 views
Skip to first unread message

Juan Cristóbal Quesada

unread,
Jul 26, 2018, 9:52:30 AM7/26/18
to python_in...@googlegroups.com
Hi,
this is more of a pure python question i want to throw here.
Im developing a PySide Application that is used as the context/task/app/openfile launcher by the artists.

The artist makes some choices that drive him towards different dialogs and widgets. The thing is, due to a request, im facing the need to dynamically change the sys path and reload all the modules from a different location inside the same main QApplication.

The reason for this is that, once an artist makes a specific choice, all the following widgets... and code that is executed should be loaded from another location different than the current app. But, should look as if it were still part of the same app, the widgets need to appear embedded.

Ive managed to change the sys.path of the original folder to the folder i want and successfully loaded all the modules from the new path, but then im starting to get some strange behavior:

QtCore.QObject.__init__(self)
AttributeError: 'NoneType' object has no attribute 'QObject'

(Even if performed the from PySide2 import QtCore)

I understand what im trying to achieve is not very orthodox, i was wondering if its even possible!

I would like to avoid obvious ways of separating it in two QApplications and closing one when the other is opened for example.

Has any of you ever tried to do something like this? 
What concerns should i be aware of when doing it?

Justin Israel

unread,
Jul 26, 2018, 7:00:27 PM7/26/18
to python_in...@googlegroups.com
Would you be able to explain a bit more about the goal you are trying to solve, with dynamically loading a second widget into your main application? Does this second widget have drastically different dependencies? I'm interested specifically in the part where you need to reload modules. This is where things are going to go wrong. It is one thing to modify the sys.path (which you should still try to avoid at runtime), but reloading is usually something only reserved for debug situations. It definitely does not work correct with compiled extensions like the PySide modules.



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CANOg8wWN-Gwzz-drk79z3C_CMxvLqC_iyCRzRbcH%2BZoAsXMJRw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Alok Gandhi

unread,
Jul 26, 2018, 9:35:22 PM7/26/18
to python_in...@googlegroups.com
Try using importlib or imp

Although, as Justin has already pointed out, it is not recommended to use reload in production code or to dynamically append/insert paths in sys.path.

Give more thought to the design and architecture of your app. A good design should handle dependencies in a more elegant and efficient manner. A possible design idea would be this - If you are already aware of all the path that the app might need during runtime, load them up at boot time and use them later. Do note the fact that python modules are first class object i.e they can be used as a variable, can be dynamically created, passed and returned from a function. Use this for your design.

- Alok


For more options, visit https://groups.google.com/d/optout.


--

Juan Cristóbal Quesada

unread,
Jul 27, 2018, 9:46:48 AM7/27/18
to python_in...@googlegroups.com
basically,
the same application when a selection button is clicked should perform again all the imports from a specific repository. This specific repository is not known beforehand, it is only known at the specific moment when the artist chooses and hits that button.
Some of the dependencies are duplicated in both folders, so what im doing is deleting the modules loaded in the "pre-button hit", modifiying the sys path and then importing them manually again to the sys.modules dictionary
For example, we are using the Qt.py module. This module is duplicated and appears in the "static" repo and also in every other "dynamic" repo, although the PySide modules are the same.
uhmm, maybe  i could treat PySide modules as a special case and choose not to reimport them...... will give it a try.

Apparently im not having any other problems with importing the 'dynamic' repo modules but when it comes to PySide it does.... is this a general behaviour of compiled libraries?


2018-07-27 3:35 GMT+02:00 Alok Gandhi <alok.ga...@gmail.com>:
Try using importlib or imp

Although, as Justin has already pointed out, it is not recommended to use reload in production code or to dynamically append/insert paths in sys.path.

Give more thought to the design and architecture of your app. A good design should handle dependencies in a more elegant and efficient manner. A possible design idea would be this - If you are already aware of all the path that the app might need during runtime, load them up at boot time and use them later. Do note the fact that python modules are first class object i.e they can be used as a variable, can be dynamically created, passed and returned from a function. Use this for your design.

- Alok
On Fri, Jul 27, 2018 at 7:00 AM Justin Israel <justin...@gmail.com> wrote:
Would you be able to explain a bit more about the goal you are trying to solve, with dynamically loading a second widget into your main application? Does this second widget have drastically different dependencies? I'm interested specifically in the part where you need to reload modules. This is where things are going to go wrong. It is one thing to modify the sys.path (which you should still try to avoid at runtime), but reloading is usually something only reserved for debug situations. It definitely does not work correct with compiled extensions like the PySide modules.



On Fri, Jul 27, 2018 at 1:52 AM Juan Cristóbal Quesada <juan.cri...@gmail.com> wrote:
Hi,
this is more of a pure python question i want to throw here.
Im developing a PySide Application that is used as the context/task/app/openfile launcher by the artists.

The artist makes some choices that drive him towards different dialogs and widgets. The thing is, due to a request, im facing the need to dynamically change the sys path and reload all the modules from a different location inside the same main QApplication.

The reason for this is that, once an artist makes a specific choice, all the following widgets... and code that is executed should be loaded from another location different than the current app. But, should look as if it were still part of the same app, the widgets need to appear embedded.

Ive managed to change the sys.path of the original folder to the folder i want and successfully loaded all the modules from the new path, but then im starting to get some strange behavior:

QtCore.QObject.__init__(self)
AttributeError: 'NoneType' object has no attribute 'QObject'

(Even if performed the from PySide2 import QtCore)

I understand what im trying to achieve is not very orthodox, i was wondering if its even possible!

I would like to avoid obvious ways of separating it in two QApplications and closing one when the other is opened for example.

Has any of you ever tried to do something like this? 
What concerns should i be aware of when doing it?

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.


--

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPaTLMRzu_d9%2BtP-b1fJawQ8DGYVHDmcW3FeXmb4%3DEEMU72D5g%40mail.gmail.com.

Alok Gandhi

unread,
Jul 27, 2018, 10:36:33 AM7/27/18
to python_in...@googlegroups.com
To quote from the docs:
Python modules’ code is recompiled and the module-level code reexecuted, defining a new set of objects which are bound to names in the module’s dictionary. The init function of extension modules is not called a second time. As with all other objects in Python the old objects are only reclaimed after their reference counts drop to zero. The names in the module namespace are updated to point to any new or changed objects. Other references to the old objects (such as names external to the module) are not rebound to refer to the new objects and must be updated in each namespace where they occur if that is desired.

Basically, you have to reconstruct all objects for class that is reimported in the module. If you have no need to rimport Pyside modules then do avoid them and see if it works. If you have to anyway then here is an example reference:

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.


--

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CANOg8wUTtSzaZMSTzUG9QcBCa5wGu6x%3DQPHzqAwzo%2Bm5%3DRe7%3DA%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.


--

Robert White

unread,
Jul 27, 2018, 4:12:04 PM7/27/18
to Python Programming for Autodesk Maya
I think you'd be better served with a small launcher app. That allows for the artist to make their selection.
Then that opens the proper app from the proper location.
Otherwise you're going to be fighting a lot of low level language machinery to get what you want.

Justin Israel

unread,
Jul 27, 2018, 5:56:57 PM7/27/18
to python_in...@googlegroups.com


On Sat, Jul 28, 2018, 8:12 AM Robert White <robert....@gmail.com> wrote:
I think you'd be better served with a small launcher app. That allows for the artist to make their selection.
Then that opens the proper app from the proper location.

This is what I did at my previous studio. I had an "app store" that tracked all our applications for updates and allowed users to update and launch as new apps. So much easier than trying to wedge them into a single process. It's also more flexible because apps can be anything and not specifically importable Py Qt code. 

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
--

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/a62f74ac-9d78-469b-81c5-2a780577df87%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages