|
|
@@ -0,0 +1,307 @@
|
|
|
+# Python脚本使用说明
|
|
|
+
|
|
|
+本文档提供了在项目中使用Python脚本的基本指南,包括Python安装、设置PATH、调用、调试、单元测试和常用库的使用方法。
|
|
|
+
|
|
|
+## 0. Python安装与环境配置
|
|
|
+
|
|
|
+### 0.1 下载Python
|
|
|
+
|
|
|
+1. 访问Python官方网站:[https://www.python.org/downloads/](https://www.python.org/downloads/)
|
|
|
+2. 下载适合您操作系统的最新版本Python(推荐Python 3.8+)
|
|
|
+
|
|
|
+### 0.2 安装Python
|
|
|
+
|
|
|
+- **Windows**:
|
|
|
+
|
|
|
+ 1. 运行下载的安装程序
|
|
|
+ 2. 勾选"Add Python to PATH"选项
|
|
|
+ 3. 点击"Install Now"完成安装
|
|
|
+
|
|
|
+### 0.3 设置PATH环境变量(如果安装时未勾选)
|
|
|
+
|
|
|
+- **Windows**:
|
|
|
+
|
|
|
+ 1. 右键点击"此电脑" → "属性" → "高级系统设置" → "环境变量"
|
|
|
+ 2. 在"系统变量"中找到"Path",点击"编辑"
|
|
|
+ 3. 点击"新建",添加Python安装目录(例如:`C:\Python39`)和Scripts目录(例如:`C:\Python39\Scripts`)
|
|
|
+ 4. 点击"确定"保存更改
|
|
|
+
|
|
|
+### 0.4 验证安装
|
|
|
+
|
|
|
+1. 打开命令行终端
|
|
|
+2. 运行以下命令:
|
|
|
+
|
|
|
+ ```bash
|
|
|
+ python --version
|
|
|
+ # 或
|
|
|
+ python3 --version
|
|
|
+ ```
|
|
|
+3. 如果显示Python版本号,则安装成功
|
|
|
+
|
|
|
+### 0.5 升级pip
|
|
|
+
|
|
|
+pip是Python的包管理工具,建议保持最新版本:
|
|
|
+
|
|
|
+```bash
|
|
|
+# Windows
|
|
|
+python -m pip install --upgrade pip
|
|
|
+
|
|
|
+# macOS/Linux
|
|
|
+pip install --upgrade pip
|
|
|
+# 或
|
|
|
+pip3 install --upgrade pip
|
|
|
+```
|
|
|
+
|
|
|
+## 1. 脚本调用
|
|
|
+
|
|
|
+### 1.1 直接运行
|
|
|
+
|
|
|
+```bash
|
|
|
+# 在命令行中直接运行Python脚本
|
|
|
+python script.py
|
|
|
+
|
|
|
+# 或使用Python 3
|
|
|
+python3 script.py
|
|
|
+```
|
|
|
+
|
|
|
+### 1.2 带参数运行
|
|
|
+
|
|
|
+```bash
|
|
|
+# 传递命令行参数
|
|
|
+python script.py arg1 arg2
|
|
|
+
|
|
|
+# 在脚本中获取参数
|
|
|
+import sys
|
|
|
+print(sys.argv[1]) # 获取第一个参数
|
|
|
+```
|
|
|
+
|
|
|
+### 1.3 作为模块导入
|
|
|
+
|
|
|
+```python
|
|
|
+# 在其他Python文件中导入模块
|
|
|
+import my_module
|
|
|
+my_module.function()
|
|
|
+
|
|
|
+# 或从模块中导入特定函数
|
|
|
+from my_module import function
|
|
|
+function()
|
|
|
+```
|
|
|
+
|
|
|
+## 2. 调试
|
|
|
+
|
|
|
+### 2.1 使用print语句
|
|
|
+
|
|
|
+```python
|
|
|
+print("调试信息:", variable)
|
|
|
+```
|
|
|
+
|
|
|
+### 2.2 使用pdb调试器
|
|
|
+
|
|
|
+```python
|
|
|
+import pdb
|
|
|
+
|
|
|
+# 在代码中设置断点
|
|
|
+pdb.set_trace()
|
|
|
+
|
|
|
+# 常用调试命令
|
|
|
+# n: 执行下一行
|
|
|
+# c: 继续执行直到下一个断点
|
|
|
+# p: 打印变量值
|
|
|
+# q: 退出调试
|
|
|
+```
|
|
|
+
|
|
|
+### 2.3 使用IDE调试
|
|
|
+
|
|
|
+- **PyCharm**: 设置断点,使用调试模式运行
|
|
|
+- **VS Code**: 详细调试指南
|
|
|
+ 1. 安装Python扩展:在VS Code扩展市场搜索并安装"Python"扩展
|
|
|
+ 2. 打开项目文件夹:File > Open Folder
|
|
|
+ 3. 创建调试配置:
|
|
|
+ - 点击左侧调试图标(或按Ctrl+Shift+D)
|
|
|
+ - 点击"创建launch.json文件"
|
|
|
+ - 选择"Python"环境
|
|
|
+ - 选择"Python File"配置
|
|
|
+ 4. 设置断点:在代码行号左侧点击添加断点
|
|
|
+ 5. 开始调试:
|
|
|
+ - 点击绿色开始按钮
|
|
|
+ - 或按F5键
|
|
|
+ 6. 调试控制:
|
|
|
+ - 继续(F5):执行到下一个断点
|
|
|
+ - 单步跳过(F10):执行当前行,不进入函数
|
|
|
+ - 单步进入(F11):执行当前行,进入函数
|
|
|
+ - 单步跳出(Shift+F11):从当前函数跳出
|
|
|
+ 7. 查看变量:在调试面板中查看变量值
|
|
|
+ 8. 监视表达式:添加表达式以实时监视其值
|
|
|
+ 9. 控制台:在调试控制台中执行Python代码
|
|
|
+
|
|
|
+## 3. 单元测试
|
|
|
+
|
|
|
+### 3.1 使用unittest模块
|
|
|
+
|
|
|
+```python
|
|
|
+import unittest
|
|
|
+
|
|
|
+class TestMyFunction(unittest.TestCase):
|
|
|
+ def test_function(self):
|
|
|
+ result = my_function()
|
|
|
+ self.assertEqual(result, expected_value)
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ unittest.main()
|
|
|
+```
|
|
|
+
|
|
|
+### 3.2 运行测试
|
|
|
+
|
|
|
+```bash
|
|
|
+# 运行所有测试
|
|
|
+python -m unittest test_module.py
|
|
|
+
|
|
|
+# 运行特定测试类
|
|
|
+python -m unittest test_module.TestMyFunction
|
|
|
+
|
|
|
+# 运行特定测试方法
|
|
|
+python -m unittest test_module.TestMyFunction.test_function
|
|
|
+```
|
|
|
+
|
|
|
+### 3.3 使用pytest
|
|
|
+
|
|
|
+```bash
|
|
|
+# 安装pytest
|
|
|
+pip install pytest
|
|
|
+
|
|
|
+# 运行测试
|
|
|
+pytest test_module.py -v
|
|
|
+```
|
|
|
+
|
|
|
+## 4. 项目内常用库
|
|
|
+
|
|
|
+### 4.1 标准库
|
|
|
+
|
|
|
+- **os**: 操作系统接口
|
|
|
+- **sys**: 系统相关参数和函数
|
|
|
+- **json**: JSON数据处理
|
|
|
+- **datetime**: 日期时间处理
|
|
|
+- **re**: 正则表达式
|
|
|
+- **collections**: 容器数据类型
|
|
|
+- **logging**: 日志记录
|
|
|
+
|
|
|
+### 4.2 第三方库
|
|
|
+
|
|
|
+- **requests**: HTTP请求
|
|
|
+- **pandas**: 数据处理和分析
|
|
|
+- **numpy**: 数值计算
|
|
|
+- **matplotlib**: 数据可视化
|
|
|
+- **scikit-learn**: 机器学习
|
|
|
+- **flask**: Web应用框架
|
|
|
+- **django**: 高级Web框架
|
|
|
+
|
|
|
+### 4.3 安装第三方库
|
|
|
+
|
|
|
+```bash
|
|
|
+# 安装单个库
|
|
|
+pip install requests
|
|
|
+
|
|
|
+# 安装多个库
|
|
|
+pip install requests pandas numpy
|
|
|
+
|
|
|
+# 从requirements.txt文件安装
|
|
|
+pip install -r requirements.txt
|
|
|
+```
|
|
|
+
|
|
|
+## 5. 最佳实践
|
|
|
+
|
|
|
+### 5.1 代码风格
|
|
|
+
|
|
|
+- 遵循PEP 8编码规范
|
|
|
+- 使用有意义的变量和函数名
|
|
|
+- 添加适当的注释
|
|
|
+
|
|
|
+### 5.2 错误处理
|
|
|
+
|
|
|
+```python
|
|
|
+try:
|
|
|
+ # 可能出错的代码
|
|
|
+ result = risky_operation()
|
|
|
+except Exception as e:
|
|
|
+ # 处理错误
|
|
|
+ print(f"错误: {e}")
|
|
|
+finally:
|
|
|
+ # 无论是否出错都会执行的代码
|
|
|
+ cleanup()
|
|
|
+```
|
|
|
+
|
|
|
+### 5.3 性能优化
|
|
|
+
|
|
|
+- 使用列表推导式代替循环
|
|
|
+- 避免在循环中进行IO操作
|
|
|
+- 使用生成器处理大文件
|
|
|
+- 适当使用缓存
|
|
|
+
|
|
|
+## 6. 示例脚本
|
|
|
+
|
|
|
+```python
|
|
|
+#!/usr/bin/env python3
|
|
|
+"""
|
|
|
+示例Python脚本
|
|
|
+"""
|
|
|
+
|
|
|
+import argparse
|
|
|
+import logging
|
|
|
+
|
|
|
+# 配置日志
|
|
|
+logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
+logger = logging.getLogger(__name__)
|
|
|
+
|
|
|
+def main():
|
|
|
+ """主函数"""
|
|
|
+ parser = argparse.ArgumentParser(description='示例脚本')
|
|
|
+ parser.add_argument('--input', type=str, required=True, help='输入文件路径')
|
|
|
+ parser.add_argument('--output', type=str, default='output.txt', help='输出文件路径')
|
|
|
+ args = parser.parse_args()
|
|
|
+
|
|
|
+ logger.info(f"处理文件: {args.input}")
|
|
|
+
|
|
|
+ try:
|
|
|
+ with open(args.input, 'r', encoding='utf-8') as f:
|
|
|
+ content = f.read()
|
|
|
+
|
|
|
+ # 处理内容
|
|
|
+ processed_content = content.upper()
|
|
|
+
|
|
|
+ with open(args.output, 'w', encoding='utf-8') as f:
|
|
|
+ f.write(processed_content)
|
|
|
+
|
|
|
+ logger.info(f"处理完成,输出到: {args.output}")
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"处理失败: {e}")
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ main()
|
|
|
+```
|
|
|
+
|
|
|
+## 7. 测试示例
|
|
|
+
|
|
|
+```python
|
|
|
+#!/usr/bin/env python3
|
|
|
+"""
|
|
|
+测试示例
|
|
|
+"""
|
|
|
+
|
|
|
+import unittest
|
|
|
+from my_script import process_content
|
|
|
+
|
|
|
+class TestProcessContent(unittest.TestCase):
|
|
|
+ def test_process_content(self):
|
|
|
+ """测试内容处理函数"""
|
|
|
+ input_content = "hello world"
|
|
|
+ expected_output = "HELLO WORLD"
|
|
|
+ result = process_content(input_content)
|
|
|
+ self.assertEqual(result, expected_output)
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ unittest.main()
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+本说明文档提供了Python脚本使用的基本指南,可根据项目的具体需求进行调整和扩展。
|