简体   繁体   中英

Converting .py extension to executable file

Suppose i have a main.py for a calculator application. I used different .py files for different function like(add.py for adding, mul.py for multiplication) and all these files are imported in main.py. When I click main.py it executes successfully and do all function like adding, mul, etc. What i want to do is to make a executable file for main.py so that i can run it in the computer that doesnot have a python installed or any of that module(add.py,..) exist in the hard drive. Is that possible?? THANKS FOR THE HELP

Finally I got the solution. PyInstaller works well in my case and it is easy to use too. Thank you for your help. (:

I have no experience of Python, but a simple Google search comes up with py2exe

As for OS independent, that's impossible, that's the whole point of an interpreter. The code is write once run anywhere, but once you compile it, you have to compile it for a specific platform

It is impossible to have an independent executable that runs on any OS, that's why Python is an interpreted language. What you can do is compile the Python scripts into an executable on each OS you want it to run on, using pyinstaller, so you are have a bunch of different independent programs for different OSs all built on the same Python script.

Pyinstaller has a advantage over py2exe because its easier to compile all the scripts into one file, instead of one directory.

Short of embedding code and a Python executable (like py2exe does), there's a couple of other possible options (although I've not tried any of them).

  • Write it with Cython and use a compiler to generate executables for all target platforms
  • Write it with Jython and generate .class files and run it on the JVM
  • Write it with IronPython and have it run on .NET/Mono

Like everyone else has mentioned before, it is not possible to create one executable for multiple OSes. If your question is how I understood it, trying to bundle several .py files that are used in a main .py file, then yes, that is achievable through py2exe . You'll need to create a setup.py file with the following:

from distutils.core import setup
import py2exe, sys, os

includes = ['add.py', 'mult.py', 'div.py', 'subt.py']
dll_excludes = ['mswsock.dll', 'powrprof.dll',]

setup(
        console=[{
                'script': 'math.py',
                'name': 'Calculator',
                'version': '1.0',
                'description': 'Basic calculator',
                'author': 'Firstname Lastname'
        }],
        options={
                'py2exe': {
                        'includes': includes,
                        'dll_excludes': dll_excludes,
                        'bundle_files': 1,  # 1 = .exe; 2 = .zip; 3 = separate
                }
        },
        zipfile=None,  # Put libs into .exe to save space.
)

Once you save this as setup.py then you can create your .exe by running the setup.py file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM