python - cx_freeze module dependency -


so python script depends on module created. module reads in text file. script, module, , file reads in same directory when run source , works great.

i compiled cx_freeze, , when run it, imported module fails. module tries read file , says can't find it, , stops right there.

the text file included in library.zip, , build folder (probably unnecessarily, figured couldn't hurt). decided print working directory in module before reading file see happening, , looks working directory isn't build folder, user home directory.

of course text file isn't in user's home directory. how can fix this?

just concrete, here's example. files in same directory.

script.py:

import hello 

hello.py

import os print(os.getcwd()) f = open('hello.txt','r') print(f.read()) f.close() 

hello.txt

hello! 

setup.py

import sys import os cx_freeze import setup, executable  # dependencies automatically detected, might need fine tuning.  includes = ['hello.txt'] zip_inc = ['hello.txt']  build_exe_options = {"packages": ["os"], "include_files": includes, "zip_includes": zip_inc}  setup(  name = "test",         version = "0.1",         description = "test",         options = {"build_exe": build_exe_options},         executables = [executable("script.py")]) 

i built command:

python setup.py build 

then ran file called script in build directory. i'm in mac os x if matters. output follows:

/users/pianowow traceback (most recent call last):   file "/library/frameworks/python.framework/versions/3.3/lib/python3.3/site-packages/cx_freeze/initscripts/console3.py", line 27, in <module>     exec(code, m.__dict__)   file "script.py", line 1, in <module>   file "/library/frameworks/python.framework/versions/3.3/lib/python3.3/importlib/_bootstrap.py", line 1558, in _find_and_load     return _find_and_load_unlocked(name, import_)   file "/library/frameworks/python.framework/versions/3.3/lib/python3.3/importlib/_bootstrap.py", line 1525, in _find_and_load_unlocked     loader.load_module(name)   file "/users/pianowow/desktop/test/hello.py", line 3, in <module>     f = open('hello.txt','r') filenotfounderror: [errno 2] no such file or directory: 'hello.txt' 

i've got pull request update docs - here's updated section on using data files:

applications need data files besides code, such icons. using setup script, can list data files or directories in include_files option build_exe. they'll copied build directory alongside executable. find them, use code this:

def find_data_file(filename):     if getattr(sys, 'frozen', false):         # application frozen         datadir = os.path.dirname(sys.executable)     else:         # application not frozen         # change bit match store data files:         datadir = os.path.dirname(__file__)      return os.path.join(datadir, filename) 

an alternative embed data in code, example using qt's resource system.

[from file]


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -