Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 212
Mikhael Pramodana Avatar asked May 10 '16 16:05

Mikhael Pramodana


3 Answers

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 Sandeep's suggestion 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
like image 162
Pelonomi Moiloa Avatar answered Oct 13 '22 11:10

Pelonomi Moiloa


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

like image 43
Mark Cuddihy Avatar answered Oct 13 '22 11:10

Mark Cuddihy


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!!

like image 33
sai sandeep balbari Avatar answered Oct 13 '22 11:10

sai sandeep balbari