Package a Python program to a single Executable file with Pyinstaller

The advantage of packaging a program is that the user do NOT need any programming environment to use the packaged software.

Packaging a program is very common.For example, when you install most of the software, the ‘setup’ installer will install a packaged software into your computer as a folder and add a shortcut of the executable to your desktop.

However, packaging the program into a single file is much more difficult, I’ll explain why later, let us see how to do it for a Python project.

First things first, make sure you have pyinstaller. I’ve used other libraries like py2exe, but pyinstaller is the best in terms of its compatibility, stability, and user-friendly, the list goes on and on.

in the command window

-> pyinstaller  file_tobe_pack.py  -o image.ico  -F  –upx-dir upxfolder

-c  (default)  open a console window

-w  will NOT open a console window

The biggest problem when packaging, especially to a single file:

can not find some specific library dependencies

these are tons of solutions online, but much are so complex and not direct

these is a very simple solution:

Locate the library you import in your program (the one failed packaging) then move the source code to your current directory with the program you wrote, the CHANGE the name of the source code file and change the library name you import.

The packaging software will then search dependency files from the files you made.

Here’s an example:

 

 

30 days of code https://www.hackerrank.com

Day 6

 

Task
Given a string,S , of length N  that is indexed from 0 to N-1, print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line (see the Sample below for more detail).

Note: is considered to be an even index.

Sample Input

2
Hacker
Rank

Sample Output

Hce akr
Rn ak

My code

# Enter your code here. Read input from STDIN. Print output to STDOUT
x=int(raw_input())



for i in range(x):
    d=raw_input()
    a='';b=''
    for i in range(0,len(d),2):
        a+=d[i]
    print a,
    
    for i in range(1,len(d),2):
        b+=d[i]
    print b,

    print ''

official answer:

t = int(raw_input())
for _ in range(t):
    line = raw_input()
    first = ""
    second = ""

    for i, c in enumerate(line):
        if (i & 1) == 0:
            first += c
        else:
            second += c
    print first, second

Day 8

Sample Input

3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry

Sample Output

sam=99912222
Not found
harry=12299933

my answer

# Enter your code here. Read input from STDIN. Print output to STDOUT
x=raw_input()
y={}
for i in range(int(x)):
    k=raw_input()
    y[k.split(' ')[0]]=k.split(' ')[-1]
    

while True:
    try:
        c=raw_input()
    except:
        c=0
    if c:
        try:
            print c+'='+y[c]
        except:
            print 'Not found'
    else:
        break

official

Python3
import sys 

# Read input and assemble Phone Book
n = int(input())
phoneBook = {}
for i in range(n):
    contact = input().split(' ')
    phoneBook[contact[0]] = contact[1]

# Process Queries
lines = sys.stdin.readlines()
for i in lines:
    name = i.strip()
    if name in phoneBook:
        print(name + '=' + str( phoneBook[name] ))
    else:
        print('Not found')

Day 12: Inheritance

class Person:
	def __init__(self, firstName, lastName, idNumber):
		self.firstName = firstName
		self.lastName = lastName
		self.idNumber = idNumber
	def printPerson(self):
		print "Name:", self.lastName + ",", self.firstName
		print "ID:", self.idNumber



class Student(Person):
    def __init__(self, firstName, lastName, idNum ,scores):
        Person.__init__(self, firstName, lastName, idNum)
        self.scores=scores

    def calculate(self):
        avg = sum(scores) / len(scores)
        grade = ''
        if (90 <= avg <= 100):
            grade = 'O'
        if (80 <= avg < 90):
            grade = 'E'
        if (70 <= avg < 80):
            grade = 'A'
        if (55 <= avg < 70):
            grade = 'P'
        if (40 <= avg <= 55):
            grade = 'D'            
        if (avg < 40):
            grade = 'T'        
        
        return grade
        

line = raw_input().split()
firstName = line[0]
lastName = line[1]
idNum = line[2]
numScores = int(raw_input()) # not needed for Python
scores = map(int, raw_input().split())
s = Student(firstName, lastName, idNum, scores)
s.printPerson()
print "Grade:", s.calculate()

Day 14: Scope

The absolute difference between two integers

class Difference:
    def __init__(self, a):
        self.__elements = a

	# Add your code here
    def computeDifference(self):
        self.maximumDifference = max([a-b for a in self.__elements for b in self.__elements])


# End of Difference class

_ = raw_input()
a = [int(e) for e in raw_input().split(' ')]

d = Difference(a)
d.computeDifference()

print d.maximumDifference

Day 15: Linked List

class Node:
    def __init__(self,data):
        self.data = data
        self.next = None
class Solution:
    def display(self,head):
        current = head
        while current:
            print current.data,
            current = current.next	


    def insert(self,head,data):
        if (head == None):
            head = Node(data)
        else:
            current = head
            while True:
                if(current.next == None):
                    current.next = Node(data)
                    break
                current = current.next
        return head


mylist= Solution()
T=int(input())
head=None
for i in range(T):
    data=int(input())
    head=mylist.insert(head,data)    
mylist.display(head);

 

A Image Crawler I wrote Added GUI (graphical user interface) & packaged into a single .exe file

in 2014, I’ ve written a Python image crawler for a specific website Cure WorldCosplay,  a website that attracts cosplayers  all over the world post their own pictures . Which has about 10k active members and up to 10 million pictures posted.  

The pros is the program is packaged into a single executable file, no programming environment needed. But some virus detection software could report unsafe file.

 Here the program is!     

Click names below for download

With interface

Without interface


Theoretically, if you have enough disk space, you can download all the pics of that website (about 9800 Gigabyte), the only limit is your bandwidth. I have deployed 36 crawlers on a Linux server at the same time, they download pics 24/7 at the maximum internet bandwidth.

 

How to use it: