Re: Python Importing Dynamically

  • From: Storm Dragon <stormdragon2976@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Sun, 28 Mar 2010 17:22:02 -0400

Hi,
That did the trick. Thanks for the help. Just in case anyone needs this
in the future, I had to filter out .pyc files too. So, here's the
finished project:
#Dynamic Import section
customScripts = os.listdir("customizations")
if '__init__.py' in customScripts:
    customScripts.remove('__init__.py')
for i in customScripts:
    if i[-3:] == ".py":
        exec("from customizations.%s import *" % i[:-3])
#end dynamic import section
Thanks
Storm


-- 
Follow me on Twitter:
http://www.twitter.com/stormdragon2976
My blog, Thoughts of a Dragon:
http://www.stormdragon.us/
What color dragon are you?
http://quizfarm.com/quizzes/new/alustriel07/what-color-dragon-would-you-be/
Install Windows Vista in under 2 minutes:
http://is.gd/am6TD



On Sun, 2010-03-28 at 15:55 -0400, Haden Pike wrote:

> Is the customizations folder recognized as a package.  I mean, does it
> have an __init__.py file.  If not, create one with nothing in it, then
> remove it from the customizations list like so.
> 
> customizations = os.listdir("customizations")
> if '__init__.py' in customizations:
>  customizations.remove('__init__.py')
> for i in customizations:
>  exec("from customizations.%s import *" % i[:-3)
> Haden
> 
> On 3/28/2010 3:34 PM, Storm Dragon wrote: 
> 
> > Hi,
> > The reason I changed the . to a / is because it altered the actual
> > file name that it should be importing. Here is the error with the .
> > in place:
> > Traceback (most recent call last):
> >   File "orca-customizations.py", line 16, in <module>
> >     exec("from customizations.%s import *" % i[:-3])
> >   File "<string>", line 1, in <module>
> > ImportError: No module named customizations.weather
> > Thanks
> > Storm
> > 
> > 
> > -- 
> > Follow me on Twitter:
> > http://www.twitter.com/stormdragon2976
> > My blog, Thoughts of a Dragon:
> > http://www.stormdragon.us/
> > What color dragon are you?
> > http://quizfarm.com/quizzes/new/alustriel07/what-color-dragon-would-you-be/
> > Install Windows Vista in under 2 minutes:
> > http://is.gd/am6TD
> >         
> > 
> > 
> > 
> > 
> > On Sun, 2010-03-28 at 15:25 -0400, Haden Pike wrote:
> > 
> > > This is because you have to use a . (period) instead of a / in the
> > > line reading exec("from customizations/%s import *" % i[:-3]).  It
> > > needs to be exec("from customizations.%s import *" % i[:-3]).
> > > Haden
> > > 
> > > On 3/28/2010 3:07 PM, Storm Dragon wrote: 
> > > 
> > > > Hi,
> > > > The new code is:
> > > > #Dynamic Import section
> > > > customScripts = os.listdir("customizations")
> > > > for i in customScripts:
> > > >     exec("from customizations/%s import *" % i[:-3])
> > > > #end dynamic import section
> > > > I still get the syntax error:
> > > > Traceback (most recent call last):
> > > >   File "orca-customizations.py", line 16, in <module>
> > > >     exec("from customizations/%s import *" % i[:-3])
> > > >   File "<string>", line 1
> > > >     from customizations/weather import *
> > > >                        ^
> > > > SyntaxError: invalid syntax
> > > > Thanks
> > > > Storm
> > > > 
> > > > 
> > > > -- 
> > > > Follow me on Twitter:
> > > > http://www.twitter.com/stormdragon2976
> > > > My blog, Thoughts of a Dragon:
> > > > http://www.stormdragon.us/
> > > > What color dragon are you?
> > > > http://quizfarm.com/quizzes/new/alustriel07/what-color-dragon-would-you-be/
> > > > Install Windows Vista in under 2 minutes:
> > > > http://is.gd/am6TD
> > > >         
> > > >             
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > On Sun, 2010-03-28 at 14:38 -0400, Haden Pike wrote:
> > > > 
> > > > > xec("from customizations.%s import *" % i[:-3) 
> > > 
> > > 
> 
> 

Other related posts: