别人怎么用我的Python程序?这三个方法轻松实现
陈立鑫
优草派
很多小伙伴想知道别人如何使用我的python程序,那么小编就通过这篇文章来给大家提供几个实现方法,大家如果感兴趣一定要耐心读完,小编讲得非常详细。
其实我们在这里说的给别人用,并不是指将你的代码开源,也不是指给另一个程序员用。比如你写了一个小工具,想给别人用,总不能叫别人也去装python吧。
如果想让别人不装Python环境就可以使用Python编写的程序,那么我们就可以将Python程序打包.exe可执行程序,然后发给他人。
Python打包生成.exe文件的工具有:
1、py2exe
使用:
先写一个简单的脚本,文件名:helloworld.py
#!/usr/bin/env python # -*- coding: utf-8 -*- def say_hello(name): print("Hello, " + name) if __name__ == "__main__": name = input("What's your name:") say_hello(name)
还需要个用于发布程序的设置脚本:mysetup.py,在其中的 setup 函数前插入语句 import py2exe。
from distutils.core import setupimport py2exe setup(console=["helloworld.py"])
2、cx_Freeze
使用:cxfreeze main.py --target-dir dist Usage: cxfreeze [options] [SCRIPT] Freeze a Python script and all of its referenced modules to a baseexecutable which can then be distributed without requiring a Pythoninstallation.Options: --version show program's version number and exit -h, --help show this help message and exit -O optimize generated bytecode as per PYTHONOPTIMIZE; use -OO in order to remove doc strings -c, --compress compress byte code in zip files -s, --silent suppress all output except warnings and errors --base-name=NAME file on which to base the target file; if the name of the file is not an absolute file name, the subdirectory bases (rooted in the directory in which the freezer is found) will be searched for a file matching the name --init-script=NAME script which will be executed upon startup; if the name of the file is not an absolute file name, the subdirectory initscripts (rooted in the directory in which the cx_Freeze package is found) will be searched for a file matching the name………………
3、PyInstaller
使用:pyinstaller demo.py
【原创声明】凡注明“来源:优草派”的文章,系本站原创,任何单位或个人未经本站书面授权不得转载、链接、转贴或以其他方式复制发表。否则,本站将依法追究其法律责任。