Stupid quick question about importing module from inside directory.

75 views
Skip to first unread message

I73

unread,
Nov 29, 2016, 8:17:22 PM11/29/16
to Python Programming for Autodesk Maya
I am trying to import a module with the __import__ because I want to dynamically call a method from multiple modules this is the original import code:

from Art.Scripts import MyScript as myScript
myScrip
t.Run()

What I would like to do is something like this: 

module = __import__ ('Art/Scripts/%s' % Module)
getattr
(module, 'Run')

How can I achieve this, when it's saying Import by filename is not supported. # 

Thanks guys!

Justin Israel

unread,
Nov 29, 2016, 9:18:03 PM11/29/16
to python_in...@googlegroups.com
The __import__() function expects you to use the same semantics as the normal import keyword, where you are specifying a dot-style module path, i.e.  foo.bar.biz.baz. But it lets you do it like a function call with strings. So you would be expected, in this case, to use a format like "Art.Scripts.MyModule" and the "Art" location would need to be on the PYTHONPATH

If what you actually want is to directly work with known absolute paths to files, then you should be looking at the "imp" module, and specifically at "imp.load_source":

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/e8be73c8-5cb9-48ce-bb6e-d83e2e92fc44%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alok Gandhi

unread,
Nov 29, 2016, 9:18:35 PM11/29/16
to python_in...@googlegroups.com
How can I achieve this, when it's saying Import by filename is not supported. #
Just a heads up, the `imp` module is deprecated in python 3.4 in favor of `importlib`

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



--

Wesley Keeling

unread,
Dec 1, 2016, 1:57:47 PM12/1/16
to python_in...@googlegroups.com
Hey Justin, I have appended the module path to the system python path under the ART directory

sys.path.append( '%s' % os.environ['MAYA_TOOLS_REPO']) #tools repo is Art Directory 'C:/some_Directory''

I am still getting that error if I use windows path string or dot path. I tried using imp to load the modules before I started this question, but it's still giving me errors: No module named Art.Scripts.myModule #

fp, pathname, description = imp.find_module('Art.Scripts.%s' % Module)
module = imp.load_module(name, fp, pathname, description)

Trying to use the system path to load the modules seem a little confusing, since I don't think I can do that directly, everything I read online is either by direct path eg "C:/Some_dir/foo.py" and it also works if I use the entire path (Not the system path). 

Am I able to do it with imp? Because It's returning me an IOError 'Using dot path and windows slash pathing':

module = imp.load_source('myF**kingAnoyingModule', 'Art/Scripts/%s.py' % moduleName)

Also Thanks Alok Gandhi for helping! 



On Tue, Nov 29, 2016 at 6:17 PM, Justin Israel <justin...@gmail.com> wrote:
On Wed, Nov 30, 2016 at 2:17 PM I73 <wesley....@iugome.com> wrote:
I am trying to import a module with the __import__ because I want to dynamically call a method from multiple modules this is the original import code:

from Art.Scripts import MyScript as myScript
myScrip
t.Run()

What I would like to do is something like this: 

module = __import__ ('Art/Scripts/%s' % Module)
getattr
(module, 'Run')

How can I achieve this, when it's saying Import by filename is not supported. # 

Thanks guys!

The __import__() function expects you to use the same semantics as the normal import keyword, where you are specifying a dot-style module path, i.e.  foo.bar.biz.baz. But it lets you do it like a function call with strings. So you would be expected, in this case, to use a format like "Art.Scripts.MyModule" and the "Art" location would need to be on the PYTHONPATH

If what you actually want is to directly work with known absolute paths to files, then you should be looking at the "imp" module, and specifically at "imp.load_source":

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_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/GDBq_xWNE-s/unsubscribe.
To unsubscribe from this group and all its topics, 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/CAPGFgA24QLw4X3rCBmfiv-HoaUwkzVH7GNwDMJMNqOgddFPhVQ%40mail.gmail.com.

Justin Israel

unread,
Dec 1, 2016, 3:42:34 PM12/1/16
to python_in...@googlegroups.com
On Fri, Dec 2, 2016 at 7:57 AM Wesley Keeling <wesley....@iugome.com> wrote:
Hey Justin, I have appended the module path to the system python path under the ART directory

sys.path.append( '%s' % os.environ['MAYA_TOOLS_REPO']) #tools repo is Art Directory 'C:/some_Directory''

I am still getting that error if I use windows path string or dot path. I tried using imp to load the modules before I started this question, but it's still giving me errors: No module named Art.Scripts.myModule #

1) Does the 'Art' directory under C:/some_Directory contain a "__init__.py" file?
2) Does the 'Scripts' directory under 'Art' contain a "__init__.py" file?
3) Is a file called 'myModule.py' located within the 'Scripts' directory?

If all of that is true, then you should be able to use the 'Art.Scripts.myModule' import path via the __import__() function
 

fp, pathname, description = imp.find_module('Art.Scripts.%s' % Module)
module = imp.load_module(name, fp, pathname, description)

Trying to use the system path to load the modules seem a little confusing, since I don't think I can do that directly, everything I read online is either by direct path eg "C:/Some_dir/foo.py" and it also works if I use the entire path (Not the system path). 

This particular approach is just a lower level way of using the __import__() function. It also requires the use of your PYTHONPATH to find the module, or a path you provide. If you look at the docs for that function it says you cannot pass a dot-named module. It needs to be the actual module you want, and optionally a path to search for it. I don't think this is what you want, since the suggestion was to either use the __import__ approach if you want to use your PYTHONPATH and a standard import path ("a.b.c"), or to use imp to load an absolute file path.
 

Am I able to do it with imp? Because It's returning me an IOError 'Using dot path and windows slash pathing':

module = imp.load_source('myF**kingAnoyingModule', 'Art/Scripts/%s.py' % moduleName)

This should be using the absolute path. Not a relative path.

fp = os.path.join(os.environ['MAYA_TOOLS_REPO'], 'Art/Scripts/%s.py' % moduleName)
module = imp.load_source("myModuleName", fp)
 


Also Thanks Alok Gandhi for helping! 



On Tue, Nov 29, 2016 at 6:17 PM, Justin Israel <justin...@gmail.com> wrote:
On Wed, Nov 30, 2016 at 2:17 PM I73 <wesley....@iugome.com> wrote:
I am trying to import a module with the __import__ because I want to dynamically call a method from multiple modules this is the original import code:

from Art.Scripts import MyScript as myScript
myScrip
t.Run()

What I would like to do is something like this: 

module = __import__ ('Art/Scripts/%s' % Module)
getattr
(module, 'Run')

How can I achieve this, when it's saying Import by filename is not supported. # 

Thanks guys!

The __import__() function expects you to use the same semantics as the normal import keyword, where you are specifying a dot-style module path, i.e.  foo.bar.biz.baz. But it lets you do it like a function call with strings. So you would be expected, in this case, to use a format like "Art.Scripts.MyModule" and the "Art" location would need to be on the PYTHONPATH

If what you actually want is to directly work with known absolute paths to files, then you should be looking at the "imp" module, and specifically at "imp.load_source":

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.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/GDBq_xWNE-s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.

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

--
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/CAAD4CW469S0OiGdXKAwygg1nHLdi5tHDrHPryq5Yc4TiV1_dXw%40mail.gmail.com.

Wesley Keeling

unread,
Dec 1, 2016, 3:56:44 PM12/1/16
to python_in...@googlegroups.com
Yes, I am using __init__.py :) 

I did get it to work with using the direct path, Thank you! 
I was just really curious if I could have used the relative system path. Thanks for clearing this up! 

Thanks again guys!

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 a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/GDBq_xWNE-s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_maya+unsub...@googlegroups.com.

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

--
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 a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/GDBq_xWNE-s/unsubscribe.

Justin Israel

unread,
Dec 1, 2016, 4:01:26 PM12/1/16
to python_in...@googlegroups.com
On Fri, Dec 2, 2016 at 9:56 AM Wesley Keeling <wesley....@iugome.com> wrote:
Yes, I am using __init__.py :) 

I did get it to work with using the direct path, Thank you! 
I was just really curious if I could have used the relative system path. Thanks for clearing this up! 

The path would be relative to your current working directory (os.curdir), which means you would have to change it via os.chdir()
If you owned the process it would be fine, but maybe its not a good idea to change the current directory from within a shared python environment like maya, just for the purpose of importing a script.
 
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 a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/GDBq_xWNE-s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.

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

--
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 a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/GDBq_xWNE-s/unsubscribe.
To unsubscribe from this group and all its topics, 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/CAAD4CW4KrPzgTCnSVoktRLh-Vo7ej0VXkMA31uV86yh44xw7Kg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages