Hello all; I have been beating my head against this one for a
while...there seems to be strange import behavior when the application
is in its 'frozen' state.

It's a long story, but I'm trying to copy a module to a cache
directory, and then import it with __import__.  It works great when
just running my app as a script, but when running as an executable,
only the first module being imported in this way works, and the
following ones fail.

I've distilled the problem down to the script attached at the bottom
of this message.  It does nothing more than copy .py files from a
"plugins" directory to a cache directory, and then immediately try to
import them.  Again, the first one works, but the rest do not.

Along with the script, I also have the full example downloadable here:
http://www.curveexpert.net/downloads/example.zip .  There, I have the
full setup for this little test problem; all you have to do is unzip
this on your c:\ drive specifically (because I'm using absolute paths
in the script below to illustrate the problem), forming a directory
called c:\example.  Then, just run go.bat in that directory to build
the frozen executable, and run it...you'll see the behavior that I'm
talking about.

I'm still working on trying to sort out where the problem is coming
from (the error message I'm getting comes from within iu.py in the
pyinstaller distro), but any advice or insight on where to look would
be greatly, greatly appreciated!

Pyinstaller is really a godsend....

Thanks,

dhyams



#!/usr/bin/env python

# This code reproduces the pyinstaller __import__ issue.

import os,sys
import shutil

CACHEDIR =  "c:\\example\\cache"
PLUGINDIR = "c:\\example\\plugins"



def cachedimport(abspath,relpath):

    # here is where the cache directory path gets added to sys.path
    if CACHEDIR not in sys.path:  sys.path.append(CACHEDIR)

    # generate the module name, and cache the module in the CACHEDIR.
    modulename = cachemodule(abspath,relpath)

    # and import it
    return __import__(modulename)



def cachemodule(abspath,relpath):
    # copies our .py file to cache directory, and returns the name of
the module

    pyfile = os.path.split(relpath)[1]

    ffrom = catpath(abspath,relpath)
    fto   = catpath(CACHEDIR,pyfile)

    if not os.path.exists(fto):
       shutil.copyfile(ffrom,fto)
    else:
       print("File already exists in cache.")

    return os.path.splitext(pyfile)[0]



def catpath(*args_):
   # this is just a utility function that is a fancier version of
os.path.join.
   # assumes there  are at least two arguments.
   args = list(args_)
   for i in xrange(1,len(args)): args[i] = args[i].lstrip(os.sep)
   s = os.path.join(args[0],args[1])
   args = args[2:]
   for a in args:
      s = os.path.join(s,a)
   return s





if __name__ == "__main__":

    # it seems to be the *appearance* of new files in the cache
directory
    # that causes the problem.  If the files that I want to import are
already in
    # the cache directory from the start, there's no problem.  But if
they appear
    # just before they are to be imported, only the first one gets
imported, and the
    # rest don't.

    # Also, when I just run this script, there are .pyc files created
for the
    # stuff in the cache dir, which is what I want.  No .pyc files are
created from
    # the frozen executable, though.

    abspath = PLUGINDIR
    relpaths = [
                "Bessel_j0.py",
                "Bessel_j1.py",
                "Bessel_y0.py",
                "Bessel_y1.py"
                ]

    for relpath in relpaths:
        mdl = cachedimport(abspath,relpath)
        print "SUCCEEDED: "+ mdl.name

    raw_input("hit return to end")








-- 
You received this message because you are subscribed to the Google Groups 
"PyInstaller" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pyinstaller?hl=en.

Reply via email to