[Pyinstaller] How to use find_module() in my app using --onefile option with Pyinstaller

15 views
Skip to first unread message

David Lai

unread,
Mar 23, 2012, 11:30:31 PM3/23/12
to PyInstaller
Hi,
i pack my app into onefile name application. and i print some
thing like a

print find_module('web.db')

in my code. but i got some error
"ImportError: No module named web.db"

but i check the executable, the module is just in there, see the
following:
......
'web.browser': (False, 1033924, 4065),
'web.db': (False, 803411, 14724),
'web.debugerror': (False, 524320, 5553),
'web.form': (False, 787326, 5123),
......

and also, i print sys.modules in my code, its output shows web.db has
been imported:

'web.time': None, 'codecs': <module 'codecs' from '/opt/engr/
application?39138/codecs.pyc'>, 'logging.sys': None, 'web.itertools':
None, 'logging.weakref': None, '_functools': <module '_functools' from
'/tmp/_MEIYFZIdY/_functools.so'>, 'web.db': <module 'web.db' from '/
opt/engr/application?39138/web/db.pyc'>,

what's wrong with the find_module()?

thanks in advance and bye.

Hartmut Goebel

unread,
Mar 24, 2012, 6:02:30 AM3/24/12
to pyins...@googlegroups.com
Am 24.03.2012 04:30, schrieb David Lai:
> i pack my app into onefile name application. and i print some

First rule: " If --onfile failes, try --onedir first." :-)

We can not remite debug your system. So please follow the instructions
outlined at >http://www.pyinstaller.org/wiki/FAQ#IfThingsGoWrong>.

--
Schönen Gruß - Regards
Hartmut Goebel
Dipl.-Informatiker (univ.), CISSP, CSSLP

Goebel Consult
Spezialist für IT-Sicherheit in komplexen Umgebungen
http://www.goebel-consult.de

Monatliche Kolumne: http://www.cissp-gefluester.de/
Goebel Consult ist Mitglied bei http://www.7-it.de


David Lai

unread,
Mar 26, 2012, 9:16:13 AM3/26/12
to pyins...@googlegroups.com

Hi Hartmut,
    This issue bother me for almost 2 weeks. i try the release version with --onefile or --onedir, it's not work. and i also try the development version with --onefile or --onedir, still no work.
   I am just wondering about  how should i use find_module() when application run. is there something different between the executable which Pyinstaller produce   and the normal execution method which running application directly using "python myapp" ?
  The following is all the information you need to locate the problem:

my own app has the directory structure like the following:
    application.py (the main entry)
    server.py
    app (a directory)
      |__common 
      |            |____commonservice.py
      |__helpers
      |            |____...(many .py files and some sub-directory)
      |__controllers
      |            |____request_login.py
      |            |____redirect.py
      |            |____...(many .py files and some sub-directory)
      |__....

1. release version 
[problem description] 
can not import the  modules in the "app/controllers"  directory of my own app, both --onefile and --onedir got the same problem.
The modules in the controllers is import by load_source() in the source code application.py  when the app is running but if pyinstaller can't support this, import all of it during the building period of exectuable is another good idea but i don't know how to do it using pyinstaller
except for the find_module problem, the executable file(--onefile mode) can run successfully !

[python version] 2.7.2  http://www.python.org/download/releases/2.7.2/
[pyinstaller version] 1.5.1 http://www.pyinstaller.org/
[platform version] SuSE Linux 10 SP2 x86_64(2.6.16.60-0.21-smp)

[sample code]
 g = globals()
 modules = [] 
 for strPath, lstDirs, lstFiles in os.walk(controllers_dir):

        if lstFiles == []:
            continue
    
        modules.extend([(f[:-3], os.path.join(strPath, f)) for f in lstFiles if f.endswith('.py')])

for (name, file) in modules:
   if name not in g:
            import_module(name, file)

def import_module(name, file):
        m = load_source(name, file)
    g[name] = m

[hook import]
  i tried make some hook file in the hooks directory in pyinstaller directory which filename like hook-app.controllers.py, hook-app.controllers.redirect.py and etc. the hook file content like hiddenimports = ['app.controllers.redirect']

whether i choose --onefile or --onedir, the outPYZ1.pyz does not include any of my modules in the app/controllers directory
? O outPYZ1.pyz
 Name: (ispkg, pos, len)
{'BaseHTTPServer': (False, 862035, 8577),
 'ConfigParser': (False, 592334, 9029),
 'Cookie': (False, 1248832, 8924),
 ......
 'app': (True, 2203686, 106),
 'app.common': (True, 298627, 113),
 'app.common.bzCompress': (False, 169204, 692),
 'app.common.commonservice': (False, 911797, 6360),

2.development version
[problem description] 
i try development version , supprisingly, by adding some config item in .spec  app, both --onefile and --onedir can import the modules in "app/controllers"! 
 hiddenimports = ['app.controllers.redirect', 'app.controllers.request_login'] make the import work!
but if i call find_module in main entry , it still not found the module which has been import succesfully in outPYZ1.pyz
but the most worst thing is when i run the exectuable produced by development version, it throw some exception which is OK in release version .

Traceback (most recent call last):
  File "<string>", line 181, in <module>
  File "<string>", line 99, in config_session
  File "/opt/buildengr/build/pyi.linux2/application/out01-PYZ.pyz/web.db", line 1142, in database
  File "/opt/buildengr/build/pyi.linux2/application/out01-PYZ.pyz/web.db", line 1011, in __init__
  File "/opt/buildengr/build/pyi.linux2/application/out01-PYZ.pyz/web.db", line 1007, in import_driver
ImportError: Unable to import sqlite3 or pysqlite2.dbapi2 or sqlite


[python version] 2.7.2  http://www.python.org/download/releases/2.7.2/
[pyinstaller version] 1.5.1 http://www.pyinstaller.org/
[platform version] SuSE Linux 10 SP2 x86_64(2.6.16.60-0.21-smp)


so the issue is so weird now, if i used release version, running executable is OK but can not import some modules and find_module;  if i used development version, running executable throws exception but can import modules, still can't fine_module.

your help will be fully appreciate , if you need any other thing pls let me know, thanks in advance.
 :)
Reply all
Reply to author
Forward
0 new messages