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

"FileNotFoundError: [Errno 2]" when creating exe with langdetect #285

Closed
yucongo opened this issue Jul 6, 2017 · 7 comments
Closed

"FileNotFoundError: [Errno 2]" when creating exe with langdetect #285

yucongo opened this issue Jul 6, 2017 · 7 comments

Comments

@yucongo
Copy link

yucongo commented Jul 6, 2017

# applangdetect.py
import langdetect

print('test', langdetect.detect('this is a test'))
# end of applangdetect.py

pyinstaller applangdetect.py

applangdetect.exe

Traceback (most recent call last):
  File "applangdetect.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "d:\python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
 line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\langdetect\__init__.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "d:\python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
 line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\langdetect\detector_factory.py", line 10, in <module>
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "d:\python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
 line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\langdetect\detector.py", line 9, in <module>
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "d:\python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
 line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\langdetect\utils\ngram.py", line 23, in <module>
  File "site-packages\langdetect\utils\ngram.py", line 24, in NGram
  File "site-packages\langdetect\utils\messages.py", line 22, in get_string
  File "site-packages\langdetect\utils\messages.py", line 9, in __init__
FileNotFoundError: [Errno 2] No such file or directory: 'd:\\dl\\Dropbox\\mat-di
r\\snippets-mat\\wxpython\\dist\\applangdetect\\langdetect\\utils\\messages.prop
erties'
Failed to execute script applangdetect

[Windows 7, 32 bits, Python 3.4, PyInstaller==3.3.dev0+abfc80661, langdetect==1.0.7]

Simplest of all "print(langdetec.detect("test")) cannot be packed by pyintaller.

What should I do to fix this? Any hints will be appreciated.

(Also reported in pyinstaller/pyinstaller#1842, seems not fixed)

@tallforasmurf
Copy link

The problem appears to be that applangdetect is trying to find some file called messages.properties. Correct?

Since this is not a Python file and not brought in by an import in the source code, there is no way for PyInstaller to know it should be included. That is why pyinstaller/pyinstaller#1842 referred the user to the part of the manual that tells how to include data files. The manual is no longer at that URL, it is here: http://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-files-to-the-bundle

You need to figure out what non-Python files applangdetect will need at run-time and modify the .spec file to tell PyInstaller to include them. If you need help in that, you can use the PyInstaller mailing list at https://groups.google.com/forum/#!forum/pyinstaller .

A PyInstaller "hook" is a file that builds this information into the PyInstaller distribution so others who use applangdetect will not need to modify the spec file. Perhaps when you work this out you can contribute a hook file for it.

@lrq3000
Copy link

lrq3000 commented Nov 11, 2017

This worked for me:

a = Analysis(
    # your other stuff here...
    datas=[
        ('langdetect/utils', 'langdetect/utils'),  # for messages.properties file
        ('langdetect/profiles', 'langdetect/profiles'), # don't forget if you load langdetect as a submodule of your app, change the second string to the relative path from your parent module. The first argument is the relative path inside the pyinstaller bundle.
          ]
    # the rest of your analysis spec...
    )

@htgoebel
Copy link
Member

You may want to provide a hook for this.

@giuliodz
Copy link

I had the same issue. However I used some hooks for this as it was easier in my opinion

Solution guide

  1. In your project root directory (where you execute pyinstaller), create a subdirectory for the hooks. In this case I' ll name it app-hooks/
  2. Generate a file in that folder app-hooks/hook-langdetect.py:
from PyInstaller.utils.hooks import collect_all

datas, binaries, hiddenimports = collect_all('langdetect')
  1. Generate the .spec file by running Pyinstaller at least once.
  2. Modify it to include the hook:
# Leave it unchanged 
# ...
a = Analysis(
             # Leave it unchanged 
             # ...
             hiddenimports=['langdetect'], # <--- just to be sure. It should work without it too.
             hookspath=['./app-hooks/'], # <---- make it point to that hook folder
             # Leave it unchanged 
             # ...
            )
# Leave it unchanged 
# ...
  1. Now rebuild your app using the spec.file pyinstaller --onedir your_file_name.spec --clean

Now the issue is fixed and you can run your app.

@Aniskonig
Copy link

Did you fix it ? I have the same problem and when I followed the previous answer I didn't got the required hooks:

114 INFO: Determining a mapping of distributions to packages...
14033 WARNING: Unable to find package for requirement six from package langdetect.
14034 INFO: Packages required by langdetect:
[]

@bwoodsend bwoodsend transferred this issue from pyinstaller/pyinstaller Aug 4, 2021
@bwoodsend bwoodsend reopened this Aug 4, 2021
@bwoodsend
Copy link
Member

If your PyInstaller version is reasonably recent, you should now be able to use:

pyinstaller --collect-datas=langdetect your-code.py

@Aniskonig
Copy link

Thank you so much that's solved my problem

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

8 participants