python to exe

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.
Note: posts here are not necessarily endorsed by the community, and may represent amateur or even bad practices.

python to exe

Postby sajad » Mon Mar 31, 2014 1:42 pm

this file will help you to use py2exe easier

readme.txt:
Code: Select all
py_compiler by sajad banooie
#########################################################################################
befor use copy these files and folders to your python directory:
   interface.py
   colorama(folder)
then install py2exe from: http://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/
-----------------------------------------------------------------------------------------------------------------------------------
1-install_pyc command:
   this command will turn your .py excention file to a .pyc(compiled python file) and 1 arguments:

        ############################################################      
        #C:\Users\********\Documents>setup.py install_pyc test.py  #
        ############################################################
   
   tip:your file excention must be .py not pyw.

2-install_exe command:
   this command is used to compile your python file using py2exe to an executable file runing on evrey computers
   without python and need 2 arguments:
   

   for a console app:
      ##################################################################
           #C:\Users\********\Documents>setup.py install_exe test.py console#
      ##################################################################

   for a gui app:
      ###################################################################
           #C:\Users\********\Documents>setup.py install_exe test.py windowed#
      ###################################################################

tip: if there is space is your file name you have to use doblle quats("):

      hello world.py --> "hello world.py"

thanks

interface.py:
Code: Select all
import webbrowser
import textwrap
import sys
import time

import colorama
from colorama import Fore, Back, Style

colorama.init()

class Interface(object):
   
    def write(self, s, style=""):
        """
        Writes out s, in the given style and color.
        """
       
        wrapped = "\n\n".join("\n".join(textwrap.wrap(i)) for i in s.split("\n\n"))

        sys.stdout.write(style)
        sys.stdout.write(wrapped)
        sys.stdout.write(Fore.RESET + Back.RESET + Style.RESET_ALL)
        sys.stdout.write("\n")
        sys.stdout.flush()
   
    def info(self, prompt):
        """
        Displays `prompt` as an informational message.


        `wait`
            If true, the user is also prompted to press enter, to confirm that
            he has read the message.
        """

        print
        self.write(prompt, Style.BRIGHT)
        print

    def success(self, prompt):
        """
        Displays `prompt` as a success message.
        """

        print
        self.write(prompt, Fore.GREEN + Style.BRIGHT)
        print

       
    def yesno(self, prompt, default=None):
        """
        Prompts the user for a response to a yes or no question.
        """

        print
        self.write(prompt, Style.BRIGHT)

        while True:
           
            if default is True:
                prompt = "yes/no [yes]> "
            elif default is False:
                prompt = "yes/no [no]> "
            else:
                prompt = "yes/no> "
       
            choice = raw_input(prompt)
            choice = choice.strip().lower()
           
            if choice == "yes" or choice == "y":
                return True
            elif choice == "no" or choice == "n":
                return False
            elif choice == "" and default is not None:
                return default

        print

    def terms(self, url, prompt):
        self.info("Opening {} in a web browser.".format(url))

        webbrowser.open_new(url)       
        time.sleep(.5)
       
        if not self.yesno(prompt):
            self.fail("You must accept the terms and conditions to proceed.")
   
   
    def input(self, prompt, empty=None): #@ReservedAssignment
        """
        Prompts the user for input. The input is expected to be a string, which
        is stripped of leading and trailing whitespace. If `empty` is true,
        empty strings are allowed. Otherwise, they are not.
        """
       
        print
        self.write(prompt, Style.BRIGHT)

        while True:

            if empty:
                prompt = "[{}]> ".format(empty)
            else:
                prompt = "> "
           
            rv = raw_input(prompt)
            rv = rv.strip()

            if rv:
                return rv

            if empty is not None:
                return empty

        print

    def choice(self, prompt, choices, default=None):
        """
        Prompts the user with prompt, and then presents him with a list of
        choices.
       
        `choices`
            A list of (value, label) tuples.
           
        `default`
            If not None, should be one of the values. The value that we use
            return if the user just hits enter.
        """
       
        default_choice = None
       
        print
        self.write(prompt, Style.BRIGHT)
       
        for i, (value, label) in enumerate(choices):

            i += 1

            if value == default:
                default_choice = i
               
            self.write("{}) {}".format(i, label), Style.BRIGHT)
           
        print
       
        if default_choice is not None:
            prompt = "1-{} [{}]> ".format(len(choices), default_choice)
        else:
            prompt = "1-{}> ".format(len(choices))
       
        while True:
            try:
                choice = raw_input(prompt).strip()
                if choice:
                    choice = int(choice)
                else:
                    if default_choice:
                        choice = default_choice
                    else:
                        continue
            except:
                continue
           
            choice -= 1

            if choice >= 0 and choice < len(choices):
                return choices[choice][0]

        print
   
    def fail(self, prompt):
        """
        Causes the program to terminate with a message, and a failure code.
        """

        print
        self.write(prompt, Fore.RED + Style.BRIGHT)

        sys.exit(-1)

setup.py:
Code: Select all
import argparse
import interface
from py_compile import compile
from time import sleep
from distutils.core import setup
import py2exe
import os
def main():
    ap = argparse.ArgumentParser(description="Build an android package.")
    ap.add_argument("command", help="The command to run. One of install_sdk, configure, or build.")
    ap.add_argument("argument", nargs='*', help="The arguments to the selected command.")
    args = ap.parse_args()
    iface = interface.Interface()
    def check_args(n):
        if len(args.argument) != n:
            iface.fail("The {} command expects {} arguments.".format(args.command, n))
               
        return args.argument
    if args.command == 'install_pyc':
        if len(args.argument) < 1:
            iface.fail("The build command expects at least 1 arguments.")
        iface.success("building:")
        sleep(3)
        location = check_args(1)[0]
        try:
            if 'pyw' in location:
                a==1
            compile(location)
            iface.success("success")
        except:
            iface.fail('the build failed')
           
    elif args.command == 'install_exe':
        if 2<len(args.argument) <2:
            iface.fail("The build command expects at least 2 arguments.")
       
        try:
            iface.success('building:')
            location = check_args(2)[0]
            type = check_args(2)[1]
            f = open('build.py','w')
            if type == 'console':
                f.write('import py2exe\nfrom distutils.core import setup\n\nsetup(console=["'+location+'"])')
            if type == 'windowed':
                f.write('import py2exe\nfrom distutils.core import setup\n\nsetup(windows=["'+location+'"])')
            f.close()
            os.system('build.py py2exe')
            os.remove('build.py')
            l = location.split('.py')[0]
            if os.path.isfile('dist/'+l+'.exe')==True:
                iface.success("success")
                iface.write('now copy the requsted files to dist directory.')
               
            else:
                a==1
        except:
            iface.fail('the build failed')
if __name__ == "__main__":
    main()
Attachments
py compiler.rar
python to pyc and exe
(16.82 KiB) Downloaded 107 times
Last edited by Mekire on Mon Mar 31, 2014 3:06 pm, edited 1 time in total.
Reason: First post lock. Coded added to post for the moment.
sajad
 
Posts: 1
Joined: Mon Mar 31, 2014 1:34 pm

Re: python to exe

Postby Mekire » Mon Mar 31, 2014 2:59 pm

o__0

Please create a git repository for your project; no one wants to download a random rar from a forum.
There seem to be a lot of questionable practices throughout your code (aside from the fact that I can't really see the point).

Would you mind explaining to potential users why (you think) this would be useful?

-Mek
New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Mekire
 
Posts: 1706
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona


Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 2 guests