Skip to main content

Python: Make Standalone EXE for any Python Script

Hi,

First of all we need to install py2exe. You will find setup files which will install this without any issues.

Find then here:

http://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/

Make sure to use proper version. If you download a 32 bit version and it says "Python Installation not find in registry, or something like that", then install a 64 bit, as you may have 64 bit python. Act accordingly.

Now we have to check if it is working. Open python console and run like this:



If you see no error, you are good to go.

Now create a folder and make a file inside this folder named as printscr.py. Write any python script there. We will be using below script that prints output 9 times.

for i in range(1, 10):
    print('What have I done !!!!')
raw_input()


Now create another new file with name setup.py. Write below lines in this file:

from distutils.core import setup
import py2exe

setup(console=['printscr.py'])

Now open command prompt, navigate to your folder and run below command:

python setup.py py2exe

You will see some output flows, but you don't need to bother with details. Now, if everything went well, you will see few folders that have just appeared magically. 

Go to "dist" folder, here you will find your exe names as  printscr.exe. This is it. Run this file and check that everything is working as intended.


Comments

  1. If you don't want console to display, you'll have to change "console=" to "window="

    ReplyDelete

Post a Comment

Popular posts from this blog

Python: Rename all files in a folder with names as numbers in ascending order

Hi Friends,

This script will rename files as 1.zip, 2.zip, 3.xls etc for all files in same folder as scripts:

import os
x = 0
for filename in os.listdir(os.path.dirname(os.path.abspath(__file__))):
    ext = filename.split('.')
    extension = ext[1]
    os.rename(filename, str(x) + '.' + extension)
    x = x + 1

=====================================================================

Explanation:

1. extension = ext[1] collects the extension of file. If you skip this line, the files will be renamed without any extension.

2. Script should be in same folder as files. There should not be any folder in the folder.

Tunneling through putty: Accessing a internal website or service outside the network

Hello friends, there are many situation where you have to open a service or website that is neither in your network nor running with a public IP. The site or service is running properly inside the network, but you have to use it outside the network. Is it simple? Yes. 

First you need to download putty.exe. Go, download and then read further. 

All right, let us assume you are working on a local PC, lets say its IP is 192.168.1.50.  There is another website 192.168.3.33, which you cannot open on your local PC, as this is on different network. If you can access it directly, because of something you have already done, then no need to read further. Lets say you have another server, 192.168.3.110,  be it different or same as the website hosting server, but it must be on 192.168.3.x network. Then do as below figures:




You will use the port as per the website. In my experience, even if you use any different port, it will work.
Now, put IP of website in destination, port in source port and then add…

Python: Send mail with attachment ( Any file format)

So you want to send mail with any file as attachment. This script works with any type of attachment. I have given the example of .kml file, an unusual one. You can use pdf, docx etc.



import mimetypes
import email
import email.mime.application
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import smtplib

# Create a text/plain message
msg = email.mime.Multipart.MIMEMultipart()
msg['Subject'] = 'Groove'
msg['From'] = 'mysender@gmail.com'
msg['To'] = 'receiver@gmail.com'

# The main body is just another attachment
body = email.mime.Text.MIMEText("""Hello, how are you? """)
msg.attach(body)

# KML attachment
filename='cache/20140210204804.kml'
fp=open(filename,'rb')
att = email.mime.application.MIMEApplication(fp.read(),_subtype="kml")
fp.close()
att.add_header('Content-Disposition','attachment',filename=filename)
msg.attac…