pygal icon indicating copy to clipboard operation
pygal copied to clipboard

pygal and py2exe

Open ChMaga opened this issue 10 years ago • 9 comments

Jello, i am using pygal for dynamic charts but when i make an executable file with py2exe i get an error "no module named stackedline found". When i just run python code works. Any help? Thanks in advance Christoniki

ChMaga avatar Jan 10 '15 15:01 ChMaga

I suspect it might be related to #161

paradoxxxzero avatar Jan 20 '15 09:01 paradoxxxzero

I also have some problems using pygal in py2exe. First problem is that I have to explicitely include all .py files under pygal.graph, because the py2exe import autodetection doesn't seem to work for these files (this is the same problem than OP has). This peace of code might help here:

import pygal
import os,os.path

prefix=os.path.split(pygal.__file__)[0].replace("\\", "/")

hiddenimports = ['pygal.graph.'+os.path.splitext(x)[0] 
    for x in filter(lambda x: os.path.splitext(x)[1] == ".py" and x[0] != "_", 
                    os.listdir(prefix + "/graph"))]

datas = [(prefix+"/graph/*.svg", "pygal/graph"),
         (prefix+"/css/*.css",   "pygal/css"),
        ]

What is even worse, when using py2exe, is that the .svg and .css files are opened directly at module import time. It would be better if these files were actually python modules exporting a multiline string rather than plain svg/css content. py2exe fails with the job of opening packaged data files instead (interestingly, pyinstaller seems to find a way to cope with this situation).

never-eat-yellow-snow avatar Jun 06 '15 05:06 never-eat-yellow-snow

I'm not too keen on making resource files importable python files. I mean they are files with a type, not a python module. Aren't there any other options to include data files in py2exe ?

paradoxxxzero avatar Jun 23 '15 10:06 paradoxxxzero

You can include data files, but as far as I know you must open them in a quite different way. The idiom used in the code definitely does not work. Surprisingly, pyinstaller seems to cope with this if I remember right. Anyway, this is just a suggestion; I understand the drawbacks involved in this approach.

never-eat-yellow-snow avatar Jun 23 '15 16:06 never-eat-yellow-snow

I got it to work with py2exe, I just added two lines in svg.py Below line 103 I added these lines: css = css.replace("", "/") css = css.replace("your_exe_name.exe", "")

These lines format the css folder path correct. Replace "your_exe_name.exe" with the name of your .exe file. You alse need to add pygal to your .exe folder. Add C:\Python34\Lib\site-packages\pygal to data_files in your setup.py file these will be added to the .exe folder. You can also copy the folder manually.

Jesse-vd avatar Aug 14 '15 11:08 Jesse-vd

post at 5:24am OK, I think I put the code in the wrong place, now I put it in the spec file, while compiling, I got

Unable to find "c:\program files\python35\lib\site-packages\pygal\graph\*.svg" when adding binary and data files.

Hello there, I am newly to python, I am using PyInstaller to generate a exe file, and my source code used pygal module, it seems ok to compile the source file , but when I run my exe file, the program stops immediately, and I got the message from command line:

C:/Users/hawsi/Documents/Python/weather/zuuu.csv
Processing wind data...
Traceback (most recent call last):
  File "ai_helper.py", line 60, in start_analyse
  File "ai_helper.py", line 166, in analyse_wind
  File "func.py", line 37, in display_wind_rose
  File "site-packages\pygal\graph\public.py", line 114, in render_to_file
  File "site-packages\pygal\graph\public.py", line 52, in render
  File "site-packages\pygal\graph\base.py", line 218, in setup
  File "site-packages\pygal\svg.py", line 428, in pre_render
  File "site-packages\pygal\svg.py", line 114, in add_styles
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\hawsi\\AppData\\Local\\Temp\\_MEI73682\\pygal\\css\\base.css'

I guess it's a similar situation which mentioned above, but I try to add your source code in my main file, it dose not work, so could someone help me to find what the problem is, which I spent a whole day and work out nothing, it would be of great thanks .

31nm avatar Oct 08 '17 19:10 31nm

@31nm create (in the same folder you call pyinstaller) a file called hook-pygal.py with the following contents:

from PyInstaller.utils.hooks import collect_data_files


datas = collect_data_files('pygal')

then add the following option to your pyinstaller command: --add-hooks-dir=. This will load the hook and add all data files from pygal (including the css files it is complaining).

This actually was all I needed to do to make an executable out from pyinstaller using pygal. If this works for you too we can add this hook to Pyinstaller distribution as default.

Sup3rGeo avatar May 19 '18 09:05 Sup3rGeo

The name of the option is --additional-hooks-dir=. (not --add-hooks-dir=.). It worked for me

dmnized avatar Mar 22 '19 09:03 dmnized

Worked also for me. Thanks

Gone43 avatar Nov 03 '22 12:11 Gone43