美文网首页大数据 爬虫Python AI Sql每天学点机器学习Python学习
Python打包exe程序一行简单的代码为什么就是那么多人不知道

Python打包exe程序一行简单的代码为什么就是那么多人不知道

作者: 1a076099f916 | 来源:发表于2018-11-05 15:24 被阅读11次

    关于python程序打包的介绍就不多说了,大部分的python基础书上都有介绍。这里就直接演练。

    Python打包exe程序一行简单的代码为什么就是那么多人不知道?

    关注后私信小编 PDF领取十套电子文档书籍

    只是一个简单的demo,一个demo项目中,有一个hello文件,文件中有一个函数hello,函数的作用是读取testdd.txt文件中的数据然后输出。

    这个项目中还有其他的一些东西,以演示打包。

    整个项目结构如下:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
    singledemo
    demo
    model
    init.py
    entity.py
    static
    test.txt
    hello.py
    MANIFEST.in
    setup.py
    </pre>

    hello.py中的代码:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
    def hello():
    f = open('testdd.txt', 'r')
    content = f.read()
    f.close()
    print content
    </pre>

    setup.py中的代码如下:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

    coding:utf-8

    '''
    '''
    import os
    import sys
    from setuptools import setup, find_packages
    setup(
    name = "demo",
    version = "0.0.1",
    packages = find_packages(),
    include_package_data = True,
    entry_points = {
    'console_scripts' : [
    'demo = demo.hello:hello'
    ],
    },
    package_data = {
    'demo':['*.txt']
    },
    author = "the5fire",
    author_email = '嘻嘻嘻嘻嘻嘻嘻@email.com',
    url = "http://www.the5fire.com",
    description = 'a demo for setuptools',
    )
    </pre>

    还有一个文件需要注意,MANIFEST.in:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
    recursive-include demo *.txt
    </pre>

    虽然只有一句话,但是是要通过它来包括你要打包的非py文件。

    打包时候的命令有两个,

    一个是打包成egg文件:python setup.py bdist_egg 。执行完成后,会在同目录下多了两个文件夹:demo.egg-info和dist,egg文件就在dist中,这个文件可以上传到pypi.python.com上,供大家下载。或者上传到某网盘,通过pip install --no--index find-links=[url]来下载。

    另外一种是打包成压缩文件形式:python setup.py sdist 。执行结果同上,不过文件格式不同。

    打包完成之后,当然要安装了,上一篇介绍了virtualenv,创建一个虚拟环境以供测试。然后执行python setup.py install 就会在你的虚拟环境的bin下创建一个demo的可执行文件,你在虚拟环境中运行:demo,输出结果。

    很简单的东西,但是需要参考。

    相关文章

      网友评论

        本文标题:Python打包exe程序一行简单的代码为什么就是那么多人不知道

        本文链接:https://www.haomeiwen.com/subject/lhusxqtx.html