Running external module and accessing the created objects

Kene Meniru kemeniru at gmail.com
Tue Mar 12 09:28:45 EDT 2013


Dave Angel <davea <at> davea.name> writes:

> 
> The __import__() function is defined
>     http://docs.python.org/2/library/functions.html#__import__
> 

Thanks. The name of the imported file will change with each user and for
each project so according to the this reference using this in my situation makes
sense.

> appname = "myapp"
> usermodule = __import__(appname, globals(), locals(), [], -1)
> 
> And now you can use usermodule as though you had imported it in the 
> usual way.
> 

Thanks. This worked! I was using __import__ without the other arguments
before. I guess did not think it will work :-)

> As for my other caveat, I've said it before in this thread.  Make sure 
> you don't ever load a module by more than one name, or you'll end up 
> with a mess.  And that includes the original script, which is loaded by 
> the name '__main__'
> 
> You also should avoid any circular import, as it can be very tricky to 
> deal with them.
> 

The two programs are separate, there is no fear of a circular import. Also,
I need only a function to get access to the objects in the other module so
the import is inside the function... no fear of ending up in a mess.

Thanks. I guess this makes more sense than execfile and it works.




More information about the Python-list mailing list