zoukankan      html  css  js  c++  java
  • python创建包demo

    参考链接:https://www.pythoncentral.io/how-to-create-a-python-package/

    项目打包和发布:https://zhuanlan.zhihu.com/p/161930778

    python脚本带参数打包及调用方式

    #xx.py
    import sys
     
    arg1 = sys.argv[1]
    arg2 = sys.argv[2]
    print(arg1)
    print(arg2)
    
    #1. 调用
    python XX.py arg1 arg2
    打包后 XX.exe arg1 arg2
    打包后并获得print输出
    output=os.popen('path\XX.exe arg1 arg2').read()
    
    #2.一般打包都是使用pyinstaller -F -w xxx.py,传入参数之后没有任何提示和print,换成pyinstaller -F xxx.py即可
    -w指令
    直接发布的exe应用带命令行调试窗口,在指令内加入-w命令可以屏蔽
    
    -F指令
    注意指令区分大小写。这里是大写。使用-F指令可以把应用打包成一个独立的exe文件,否则是一个带各种dll和依赖文件的文件夹
    
    -p指令
    这个指令后面可以增加pyinstaller搜索模块的路径。因为应用打包涉及的模块很多。这里可以自己添加路径。不过经过笔者测试,site-packages目录下都是可以被识别的,不需要再手动添加
    
    -i指令
    定制生成的exe文件图标   pyinstaller xx.ico -F xx.py
    
    pyinstaller官方帮助文档:https://pyinstaller.readthedocs.io/en/stable/usage.html
    

      

    Python PyInstaller安装和使用教程(详解版)

    pyinstaller 打包发布经验总结

    pyinstaller 使用--add-data 打包额外资源

    -h,--help查看该模块的帮助信息
    -F,-onefile 产生单个的可执行文件
    -D,--onedir 产生一个目录(包含多个文件)作为可执行程序
    -a,--ascii 不包含 Unicode 字符集支持
    -d,--debug 产生 debug 版本的可执行文件
    -w,--windowed,--noconsolc 指定程序运行时不显示命令行窗口(仅对 Windows 有效)
    -c,--nowindowed,--console 指定使用命令行窗口运行程序(仅对 Windows 有效)
    -o DIR,--out=DIR 指定 spec 文件的生成目录。如果没有指定,则默认使用当前目录来生成 spec 文件
    -p DIR,--path=DIR 设置 Python 导入模块的路径(和设置 PYTHONPATH 环境变量的作用相似)。也可使用路径分隔符(Windows 使用分号,Linux 使用冒号)来分隔多个路径
    -n NAME,--name=NAME 指定项目(产生的 spec)名字。如果省略该选项,那么第一个脚本的主文件名将作为 spec 的名字
    #1. app/app.py
    from say_hello import *
    def main():
        print('程序开始执行')
        print(say_hello('孙悟空'))
    # 增加调用main()函数
    if __name__ == '__main__':
        main()
    
    cmd> pyinstaller -F app.py
    
    
    # 2. 下面再创建一个带图形用户界面,可以访问 MySQL 数据库的应用程序。
    
    在 app 当前所在目录再创建一个 dbapp 目录,并在该目录下创建 Python 程序,其中 exec_select.py 程序负责查询数据,main.py 程序负责创建图形用户界面来显示查询结果。
    
    exec_select.py 文件包含的代码如下:
    # 导入访问MySQL的模块
    import mysql.connector
    def query_db():
        # ①、连接数据库
        conn = conn = mysql.connector.connect(user='root', password='32147',
            host='localhost', port='3306',
            database='python', use_unicode=True)
        # ②、获取游标
        c = conn.cursor()
        # ③、调用执行select语句查询数据
        c.execute('select * from user_tb where user_id > %s', (2,))
        # 通过游标的description属性获取列信息
        description = c.description
        # 使用fetchall获取游标中的所有结果集
        rows = c.fetchall()
        # ④、关闭游标
        c.close()
        # ⑤、关闭连接
        conn.close()
        return description, rows
    
    
    #main.py
    from exec_select import *
    from tkinter import *
    def main():
        description, rows = query_db()
        # 创建窗口
        win = Tk()
        win.title('数据库查询')
        # 通过description获取列信息
        for i, col in enumerate(description):
            lb = Button(win, text=col[0], padx=50, pady=6)
            lb.grid(row=0, column=i)
        # 直接使用for循环查询得到的结果集
        for i, row in enumerate(rows):
            for j in range(len(row)):
                en = Label(win, text=row[j])
                en.grid(row=i+1, column=j)
        win.mainloop()
    if __name__ == '__main__':
        main()
    
    cmd> Pyinstaller -F -w main.py
    

      

    java调用Python文件:https://www.cnblogs.com/wxs121/p/12994584.html

    package test;
    /*
    键盘
    
    * ┌───┐   ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐
    * │Esc│   │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S L│P/B│  ┌┐    ┌┐    ┌┐
    * └───┘   └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘  └┘    └┘    └┘
    * ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐
    * │~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│_ -│+ =│ BacSp │ │Ins│Hom│PUp│ │N L│ / │ * │ - │
    * ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤
    * │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{ [│} ]│ | \ │ │Del│End│PDn│ │ 7 │ 8 │ 9 │   │
    * ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │
    * │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  │               │ 4 │ 5 │ 6 │   │
    * ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤     ┌───┐     ├───┼───┼───┼───┤
    * │ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│  Shift   │     │ ↑ │     │ 1 │ 2 │ 3 │   │
    * ├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││
    * │ Ctrl│    │Alt │         Space         │ Alt│    │    │Ctrl│ │ ← │ ↓ │ → │ │   0   │ . │←─┘│
    * └─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘
    * 
    * Code is far away from bug with the keyboard protecting.
    */
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    /**
     * Copyright (C), 2020-2020
     *
     * @Author 嗜学劣人
     * @Date: 2020/5/30 19:35
     * @FileName: PythonSpeak
     * @Description: java调用Python示例
     */
    public class PythonSpeak {
        public static void main(String[] args) {
    
    
            Process proc;
            try {
                proc = Runtime.getRuntime().exec("E:\\Program Files\\python\\python.exe D:\\pythonPprojct\\testSpeak2.py");
                BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                String line = null;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
                in.close();
                proc.waitFor();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
        }
    
    }

    java 调用Python打包的exe 文件:https://blog.csdn.net/qq_40985985/article/details/106193876

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class TestOutput {
    
        public static void main(String[] arguments) throws IOException, InterruptedException {
            System.out.println(getProcessOutput());
        }
    
        public static String getProcessOutput() throws IOException, InterruptedException {
            // 参数1 exe的绝对路径 参数2 -i  参数3 las文件目录  23执行exe程序必须的参数
            ProcessBuilder processBuilder = new ProcessBuilder(
                    "D:\\lazConvertor.exe",
                    "-i",
                    "F:\\las_test\\");
            processBuilder.redirectErrorStream(true);
            Process process = processBuilder.start();
            System.out.println("start: " + process.isAlive());
            StringBuilder processOutput = new StringBuilder();
            try (BufferedReader processOutputReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));) {
                String readLine;
                while ((readLine = processOutputReader.readLine()) != null) {
                    processOutput.append(readLine + System.lineSeparator());
                }
                process.waitFor();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            } finally {
                if (process != null) {
                    process.destroy();
                }
            }
            return processOutput.toString().trim();
        }
    }
  • 相关阅读:
    ffmpeg用法
    文本文件存储格式
    一个守护进程实例
    构造函数初始化列表问题
    Windows系统下远程Linux系统
    printStackTrace
    getParameter
    HTML5新增的属性和废除的属性
    oracle导出表结构及注释
    <input type="text" > size与width区别
  • 原文地址:https://www.cnblogs.com/iupoint/p/15657436.html
Copyright © 2011-2022 走看看