py2exe data_files

跟風遠走 提交于 2019-12-20 06:37:21

问题


I am trying to build an executable for my python program like so:

from distutils.core import setup
import py2exe, sys, os 
import matplotlib
import numpy
from glob import glob

sys.argv.append('py2exe')

datafiles = [('files', glob(r'C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\*.*'))]

setup(windows=['main.py'], data_files= datafiles, options={"py2exe": {"includes": ["matplotlib"]}})

This works, however, I need to include these matplotlibfiles obtained by this command as well in order to make the programm work:

matplotlib.get_py2exe_datafiles()

But somehow I am not able to include them into the data_files... I tried stuff like the following, but I am getting errors like "tuple' object has no attribute 'split'"

mpl = [('files', [matplotlib.get_py2exe_datafiles()])]
datafiles.append(mpl)

Also, after compiling the working version without the matplotlibfiles, I get a warning that my project is depending on several other dlls - is there any way to force them all at once into the program?

Thanks for your help!


回答1:


Could it be that matplotlib.get_py2exe_datafiles() isn't returning files in the way that you'd like? What is the output of this?

Perhaps you need to use list() instead, and drop the extra [] around your mpl:

mpl = ('files', list(matplotlib.get_py2exe_datafiles()))
datafiles.append(mpl)

From the docs, this is what the datafiles should look like when you're done:

# data_files specifies a sequence of (directory, files) pairs in the following way:

setup(...,
      data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
                  ('config', ['cfg/data.cfg']),
                  ('/etc/init.d', ['init-script'])]
     )



回答2:


I'm little bit wondering that you want to append the mpl list to the existing datafilesone.

Having a look on following py2exe-wiki-help http://www.py2exe.org/index.cgi/MatPlotLib is showing that you have to use directly the list of matpotlib.get_py2exe_datafiles()

import matplotlib
...
setup(
   ...
data_files=matplotlib.get_py2exe_datafiles(), # <-- here
)

But you append mpl (a list) to the still existing datafiles list which will result not in a continuing list but in a matrix:

>>> datafiles = ['<datafile_one>', '<datafile_two>']
>>> mpl = [('files', ['<mpl_file_one>', '<mpl_file_two>', ...])]
>>> print(datafiles.append(mpl)]
['<datafile_one>', '<datafile_two>', [('files', ['<mpl_file_one>',  '<mpl_file_two>', ...])]

... and this seems to be not correct.

I guess you want to extend(mpl) the list of you visual studio dll files (second index slot) in your datafiles list, do you?

[('files', ['<datafile_one>', '<datafile_two>', '<mpl_file_one>',  '<mpl_file_two>', ...])]

So finally I think that you should try the following way:

datafiles = glob(r'C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\*.*'))]
datafiles.extend(matplotlib.get_py2exe_datafiles())
...
setup(windows=['main.py'], 
    data_files= [('files', datafiles)], #<-- important: tuple will be build here finally
    ...
)

-Colin-




回答3:


I managed to do get the following working:

datafiles = [("Microsoft.VC90.CRT", glob(r'C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\*.*'))]
datafiles.extend(matplotlib.get_py2exe_datafiles()) 

setup(windows=['main.py'], data_files= datafiles, options={"py2exe": {"includes": ["matplotlib"]}})

Thanks for your responses, which pointed me into the right direction!



来源:https://stackoverflow.com/questions/12260618/py2exe-data-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!