a neater way of importin multiple modules

48 views
Skip to first unread message

s...@weacceptyou.com

unread,
Aug 1, 2016, 5:32:11 PM8/1/16
to Python Programming for Autodesk Maya
Hi there,

i have a 'run' script which is importing all of the relevant modules to make my program run. The script is quite long with roughly 12 modules that are imported and run one after the other. Along with each import statement there is also the 'reload()' thing to make it reload the code. eg:

----------------------------------------------------------

import samDev.rigging.TOPOCOAT.config as config
reload(config)
import samDev.rigging.TOPOCOAT.utility_functions.subdivide_curves as subdivide_curves
reload(subdivide_curves)
import samDev.rigging.TOPOCOAT.utility_functions.general as general
reload(general)
import samDev.rigging.TOPOCOAT.utility_functions.circle_config as circle_config
reload(circle_config)
import samDev.rigging.TOPOCOAT.build_body_parts as body
reload(body)

etc (up to 12 modules or so)
------------------------------------------------------------

But i want to keep my 'run' script looking clean, is there a neater/tighter way to write all this?

thanks alot,
Sam

Justin Israel

unread,
Aug 1, 2016, 5:54:28 PM8/1/16
to Python Programming for Autodesk Maya
If you think it would be cleaner and more readable you could look at using a list of strings, and the __import__ function

Justin


--
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/cb698bec-8ba7-4e66-9005-5123569ca9a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

s...@weacceptyou.com

unread,
Aug 1, 2016, 6:03:17 PM8/1/16
to Python Programming for Autodesk Maya
k thanks Justin.

Also say theres a bunch of different modules in one folder. Do i need to import each module individually.

Or is there a way to just import the folder and access the modules individually in the script when theyre needed?

thanks,
Sam

Justin Israel

unread,
Aug 1, 2016, 6:05:27 PM8/1/16
to Python Programming for Autodesk Maya
You should look into expressing that via your packages __init__.py

The package can import all the modules it wants. Then your higher level package can do a "from subpackage import *"

Justin


--
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.

33th...@gmail.com

unread,
Aug 2, 2016, 9:34:59 PM8/2/16
to Python Programming for Autodesk Maya, s...@weacceptyou.com
there's probably a variety of ways to do it.

import os

def moduleList(folder=None):

modules = []

folder = os.path.dirname(__file__) if folder is None else folder
for (dirpath, dirnames, filenames) in os.walk(folder):

modules += [f[:-3] for f in filenames if f.endswith(".py") and f != "__init__.py"]
modules += [d for d in dirnames if os.path.exists(os.path.join(folder, d, "__init__.py"))]
break

return sorted(modules)


for moduleName in moduleList():
__import__(moduleName, globals(), locals(), ["*"], -1)

Marcus Ottosson

unread,
Aug 3, 2016, 4:00:12 AM8/3/16
to python_in...@googlegroups.com, s...@weacceptyou.com

I might have done it this way.

from samDev.rigging.TOPOCOLAT import (
    config,
    subdivide_curves,
    general,
    circle_config,
    build_body_parts as body
)

And reloaded maybe this way.

for module in (config,
               subdivide_curves,
               general,
               circle_config,
               body):
    reload(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.

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



--
Marcus Ottosson
konstr...@gmail.com

s...@weacceptyou.com

unread,
Aug 3, 2016, 4:28:59 AM8/3/16
to Python Programming for Autodesk Maya, s...@weacceptyou.com
thanks alot guys,

very helpful,
Sam
Reply all
Reply to author
Forward
0 new messages