How to use pyInstaller to completely pack all the necessary Library?

12,437

Solution 1

You can copy the nltk_data folder from where it is downloaded by nltk to your app directory. In your script where you require the library use the suggestion above with:

basedir = os.path.abspath(os.path.dirname(__file__))
import nltk
nltk.data.path.append(basedir + 'nltk_data')

Then build your pyinstaller file with

pyinstaller -F --add-data "nltk_data;nltk_data" app.py

Solution 2

I believe the command you are looking for is --onefile. This packages all required packages into the executable. This guide should help you.

pyinstaller --onefile --windowed app.py

If you have any external files that are required by the script this makes the process a bit more difficult as you will need to change your references to their location. This answer or this one can help with that

Solution 3

I faced the same issue and I was able to solve it as shown:

  1. Copy the 'nltk data' contents that you are using into a nltk_data_folder

  2. Write these two lines in the python code:

{

import nltk
nltk.data.path.append(r'nltk_data_folder')

}

  1. place nltk_data_folder in the 'Application_folder' where the .exe file is present

  2. You can now copy this 'Application_folder' (containing nltk_data_folder, .exe file and other supporting files) on any other PC and run the .exe file.

Hope this helps!!

Share:
12,437
Mikhael Pramodana
Author by

Mikhael Pramodana

Updated on June 27, 2022

Comments

  • Mikhael Pramodana
    Mikhael Pramodana almost 2 years

    I have already used pyinstaller to create a standalone aplication of my python aplication

    pyinstaller --windowed app.py
    

    and it actually run in my computer and work just as intended, but when I tried it on my friend's computer, it didn't work. It runs but somehow it can't process the text.

    here are the library used:

    import tkinter as Tk
    import tkinter.ttk as ttk
    import tkinter.scrolledtext as ScrollTxt
    from tkinter import END,E,W,filedialog,messagebox
    from nltk.tokenize import sent_tokenize, RegexpTokenizer, word_tokenize
    import math
    import fileinput
    from textblob import TextBlob as tb
    from nltk.tag import pos_tag, map_tag
    from nltk.corpus import stopwords
    

    if you want to see the result file: https://www.dropbox.com/s/mfdnaaoik7w0r23/TextSummaryV73.rar?dl=0

    anybody knows what's wrong or what's missing?

    I think it's either nltk or textblob, can anyone help how to add these files into the package?

    EDIT: I have added nltk and textblob into the Python Application's directory using spec files. now the problem is, how to make the program know that these two imports are already inside the directory?

  • Mikhael Pramodana
    Mikhael Pramodana almost 8 years
    it's not the --onefile, I already tried that one but still not working. And I read your link, but doesn't understand how to change the spec file, I tried changing it directly to the .spec file but didn't work. can you explain it to me how? where should I write the command to change the spec file.
  • Mark Cuddihy
    Mark Cuddihy almost 8 years
    You use the cmd "pyi-makespec --onefile yourapp.py" This generates the .spec file. You then manually edit the spec file using a text editor such as notepad++. You will need to add the files to the datas. E.G: " datas=[('somefile.jpeg', '.')], " if the file is in the same dir as the spec file.
  • Mikhael Pramodana
    Mikhael Pramodana almost 8 years
    I tried manually edit the spec file, then I run 'pyinstaller.exe --onefile --windowed filename.py'. The spec file changed and the datas became empty again. what should I do so the spec file does not change?? @TakoFingers
  • Mark Cuddihy
    Mark Cuddihy almost 8 years
    I believe the error is you ran "pyinstaller.exe --onefile --windowed filename.py" instead of "pyinstaller.exe --onefile --windowed filename.SPEC" After editing the .spec file you run pyinstaller on the .spec file, not the .py file.
  • Mikhael Pramodana
    Mikhael Pramodana almost 8 years
    ok I successfully added the necessary files. now how do I make the my program knows the files are there? in this case nltk and textblob library.
  • WhatsThePoint
    WhatsThePoint over 6 years
    Welcome to StackOverflow: if you post code, XML or data samples, please highlight those lines in the text editor and click on the "code samples" button ( { } ) on the editor toolbar or using Ctrl+K on your keyboard to nicely format and syntax highlight it!