python main 函数-启动-传递参数 python 打包 exe C# 进程传参

Part1:Python main 传递参数

在Python编程中,我们经常需要从命令行或其他外部环境中获取参数。Python提供了一种简单而灵活的方式来处理这些参数,即通过main函数传参

1.python main 函数-启动-传递参数

test.py

python 复制代码
import sys

def main():
    # 获取命令行参数
    arguments = sys.argv[1:]
    
    # 处理参数
    for arg in arguments:
        print(arg)

if __name__ == "__main__":
    main()
  1. 如果直接传参给 脚本
python 复制代码
python test.py arg1 arg2 arg3

输出:

python 复制代码
['arg1', 'arg2', 'arg3']

输入 aa bb cc cc

Part2: Python 打包 exe

1、使用pyinstaller

pyinstaller是一个流行的Python打包工具,它可以将Python代码打包成独立的可执行文件。

使用pyinstaller打包Python代码非常简单,只需要在命令行中输入以下命令:

python 复制代码
pyinstaller test.py

2.使用py2exe

py2exe是一个Python打包工具,它可以将Python代码打包成Windows可执行文件。使用py2exe打包Python代码也很简单,只需要在命令行中输入以下命令:

python 复制代码
python test.py py2exe

Part3: C# 进程传参:

python 复制代码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleAppone
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo process = new ProcessStartInfo();
            process.FileName = @"D:/test.exe";
            string arg1 = "arg1";
            string arg2 = "arg2";
            process.Arguments = string.Format("{0} {1}", arg1, arg2);  //多个参数用空格隔开
            process.WindowStyle = ProcessWindowStyle.Normal;
            Process.Start(process);
            Console.Read();
 
        }
    }
}
相关推荐
费弗里15 分钟前
Python全栈应用开发利器Dash 3.x新版本介绍(2)
python·dash
吴佳浩33 分钟前
Python入门指南-AI番外-MCP完整教程:从零开始学会Model Context Protocol
人工智能·python·mcp
加油吧zkf1 小时前
目标检测新纪元:DETR到Mamba实战解析
图像处理·人工智能·python·目标检测·分类
程序员阿超的博客1 小时前
Python 数据分析与机器学习入门 (五):Matplotlib 数据可视化基础
python·信息可视化·数据分析·matplotlib·数据可视化·python教程·pyplot
站大爷IP1 小时前
Python 办公实战:用 python-docx 自动生成 Word 文档
python
MO2T2 小时前
使用 Flask 构建基于 Dify 的企业资金投向与客户分类评估系统
后端·python·语言模型·flask
慢热型网友.2 小时前
用 Docker 构建你的第一个 Python Flask 程序
python·docker·flask
Naiva2 小时前
【小技巧】Python + PyCharm 小智AI配置MCP接入点使用说明(内测)( PyInstaller打包成 .exe 可执行文件)
开发语言·python·pycharm
云动雨颤2 小时前
Python 自动化办公神器|一键转换所有文档为 PDF
运维·python
梅孔立2 小时前
yum update 报错 Cannot find a valid baseurl for repo: centos-sclo-rh/x86_64 等解决办法
linux·python·centos