Convert Path String To Module Name

31 views
Skip to first unread message

Anas Elkinawy

unread,
Jun 8, 2022, 12:28:39 PM6/8/22
to Python Programming for Autodesk Maya
Is there any way to convert string to module name in order to import it 

ex:

characterName = mc.textField(character_name, q=1, text=True)
char_project_folder = mc.textField(project_folder, q=1, text=True)
rig_python_path = r"{0}\{1}\scripts\python\{2}Rig".format(char_project_folder, characterName, characterName)

rig_mod = importlib.import_module("{0}Rig".format(characterName))

from rig_mod import rig

Error: ImportError: file No module named rig_mod #

as you can see it doesn't recognize the module as it deals with it as a string 

Marcus Ottosson

unread,
Jun 8, 2022, 12:37:53 PM6/8/22
to python_in...@googlegroups.com

Almost got it!

rig = rig_mod.rig

That’s because the module was already imported in the line above, but cannot use the from ... syntax since that would try and import it again. Your rig_mod is a perfectly fine imported module.


--
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/5fbb91c7-1c54-458b-8115-f932a6397404n%40googlegroups.com.

Anas Elkinawy

unread,
Jun 8, 2022, 6:17:12 PM6/8/22
to Python Programming for Autodesk Maya
Thanks alot Marcus 
Reply all
Reply to author
Forward
0 new messages