Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distributable .exe that imports WordCloud fails because of missing file #381

Closed
Michael-E-Rose opened this issue May 27, 2018 · 12 comments
Closed

Comments

@Michael-E-Rose
Copy link

Description

I compiled a python3.5 script that includes WordCloud using pyinstaller on Windows 10 in order to have a distributable exe file. The exe fails because it is looking for a file stopwords in folder Wordcloud in the user's temp directory. The script runs all fine when exectued in script or bash mode. The file is being looked up in a folder with cryptic name, which changes on every run. The foldername always starts with _MEI followed by some numbers.

Steps/Code to Reproduce

Compile python script on windows with pyinstaller and include line ``from wordcloud import WordCloud`

Error message

C:\Users\w6504hk\Downloads\Oblaka\dist>Oblaka
Traceback (most recent call last):
  File "Oblaka.py", line 18, in <module>
  File "<frozen importlib._bootstrap>", line 968, in _find_and_load
  File "<frozen importlib._bootstrap>", line 957, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "c:\program files\python35\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\wordcloud\__init__.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 968, in _find_and_load
  File "<frozen importlib._bootstrap>", line 957, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "c:\program files\python35\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\wordcloud\wordcloud.py", line 29, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\w6504hk\\AppData\\Local\\Temp\\_MEI52242\\wordcloud\\stopwords'

I think this behaviour is not intended. It is related to wordcloud or might it be caused by pyinstaller?

@amueller
Copy link
Owner

There is a file called stopwords in wordcloud that it reads It seems pyinstaller doesn't bundle that correctly. A simple but hacky way to work around this would be to include the stopwords in the py file.

@Michael-E-Rose
Copy link
Author

That's correct, it doesn't bundle that. The question for me however is: Why does wordcloud require this file, if I use my own precompiled words?

from wordcloud import WordCloud

tuples = ... # my own list of (wordfreq, word)-tuples
wordcloud = WordCloud(normalize_plurals=False)
wordcloud.generate_from_frequencies(tuples)

It doesn't need the stopwords. It seems to me from the source code that STOPWORDS is being read anyways, regardless of whether they are being used or not. A better solution, to my mind, would be to only load the STOPWORDS file if they are actually being used. I can write a PR on that, if you agree.

@amueller
Copy link
Owner

amueller commented Jun 5, 2018

Yes, loading in init seems fine. Though you might still have issues with the font unless you use a different one.

@Michael-E-Rose
Copy link
Author

PR is live. We can do the same with the font if you deem it relevant. On my distributable I had no problems with the font but loading the fontfile only when needed seems safer.

@amueller
Copy link
Owner

The font is only loaded when it's needed.

@amueller
Copy link
Owner

Here's instructions on how you have to use pyinstaller: https://pyinstaller.readthedocs.io/en/v3.3.1/spec-files.html#adding-files-to-the-bundle

@king-mountain
Copy link

find the stopwords file in folder env\lib\site-packages\wordcloud\
copy it to your script folder,then,
You can edit the spec file of your python script,as below:
datas+=[ ('stopwords', 'wordcloud') ]
for me it works.

@philip928lin
Copy link

find the stopwords file in folder env\lib\site-packages\wordcloud
copy it to your script folder,then,
You can edit the spec file of your python script,as below:
datas+=[ ('stopwords', 'wordcloud') ]
for me it works.

I am not familiar with spec file. What do you mean "You can edit the spec file of your python script,as below:
datas+=[ ('stopwords', 'wordcloud') ]"
Could you please provide me step-wise solution?

@amueller
Copy link
Owner

amueller commented Sep 3, 2019

@philip928lin I linked it above.

@vickypappu
Copy link

a good fix given here https://www.programmersought.com/article/5635871566/

@blazenada
Copy link

Hi,
After trying both methods (proposed by @vickypappu and @amueller) I'm receiving an error:
Traceback (most recent call last):
File "Createwordcloud.py", line 70, in
File "Createwordcloud.py", line 56, in makeImage
File "wordcloud\wordcloud.py", line 453, in generate_from_frequencies
File "wordcloud\wordcloud.py", line 503, in generate_from_frequencies
File "PIL\ImageFont.py", line 959, in truetype
File "PIL\ImageFont.py", line 956, in freetype
File "PIL\ImageFont.py", line 247, in init
OSError: cannot open resource
[10644] Failed to execute script 'Createwordcloud' due to unhandled exception!

Do you know how to handle it? :)
I've tried to add the font file name to Datas in spec file as well as added the font file to the directory.

@amueller
Copy link
Owner

It looks like there's some issue with the font path. If not, I would check with pyinstaller, as this is a pyinstaller usage issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants