【笔记整理|2024年上半年】Python开发环境与工程化笔记整理
本文汇总了Python开发环境配置、性能优化、Web开发和工程化实践的技术要点,为高效开发提供全面指导。
Conda环境管理
环境配置
初始化设置
# Conda初始化脚本
__conda_setup="$('/home/user/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
eval "$__conda_setup"
if [ -f "/home/user/miniconda3/etc/profile.d/conda.sh" ]; then
. "/home/user/miniconda3/etc/profile.d/conda.sh"
else
export PATH="$PATH:/home/user/miniconda3/bin"
fi
unset __conda_setup
环境迁移和重建
从旧miniconda迁移到新anaconda时的常见问题:
InvalidArchiveError错误:
# 清理conda缓存解决依赖问题
conda clean -a
包冲突解决策略:
# 例如:acpype依赖AmberTools但Amber不包含acpype
# 通过conda安装会获取另一个ambertools
# 解决方案:在base环境中使用pip安装
pip install acpype
配置文件设置
conda config --file .condarc --add pkgs_dirs
环境变量配置
# Python环境路径示例
previous_path = "/home/user/anaconda3/envs/pmx/lib/python3.10/site-packages/pmx/data/mutff"
# Boost库路径示例(用于编译)
boost_path = "/home/user/anaconda3/envs/AMBER22/lib/cmake/Boost-1.78.0/BoostConfig.cmake"
包管理最佳实践
PyPI镜像配置
# 临时使用镜像
pip install -i https://mirrors.zju.edu.cn/pypi/web/simple some-package
# 永久配置镜像
pip config set global.index-url https://mirrors.zju.edu.cn/pypi/web/simple
包强制重装
pip install --upgrade --force-reinstall <package>
Web开发与爬虫技术
Selenium自动化
Selenium基础设置
from selenium import webdriver
# 创建WebDriver实例
driver = webdriver.Chrome()
连接错误处理
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=17823):
Max retries exceeded with url: /session/xxx/url
这种错误通常是由于目标计算机积极拒绝连接导致的。
页面滚动与交互
页面滚动实现
# 方法1:JavaScript执行滚动
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# 方法2:发送按键模拟用户滚动
from selenium.webdriver.common.keys import Keys
driver.find_element_by_tag_name('body').send_keys(Keys.PAGE_DOWN)
元素交互异常
ElementNotInteractableException
此异常表示要交互的元素不在允许交互的状态。可能原因:
- 元素被隐藏
- 元素被其他元素覆盖
- 元素尚未加载完成
静态vs动态内容抓取
静态网页数据抓取
可以使用requests库结合BeautifulSoup来检索静态网页数据。但如果目标网页使用JavaScript动态加载内容,requests可能无法获取完整的页面内容,这种情况下Selenium更适合。
动态加载内容识别
如果div元素通过JavaScript动态加载,使用requests库可能无法获取到这些内容,因为requests只能获取初始的静态HTML,不会执行JavaScript。
工具选择建议
- Beautiful Soup:适合解析静态HTML/XML内容,速度更快
- Selenium:主要用于动态网页交互和浏览器自动化
Cython性能优化
Cython编译与使用
Cython编译命令
python setup.py build_ext
Cython使用建议
可以考虑使用Cython优化一些简单的Python项目。但在非常复杂的场景下,某些语法特性不支持,可能会有绕不过去的坑。
跨平台编译
Windows和Linux需要分别执行编译,然后将编译结果拷贝到目标环境。
数据处理与文件操作
字符串处理技巧
bytes字符串替换
# 在bytes字符串中替换子串
byte_string = byte_string.replace(b"<br/>", b"\n\n")
数字字符串判断
s1 = "12345"
# 使用内置方法判断字符串是否为数字
s1.isdigit() # 判断是否为数字
s1.isnumeric() # 判断是否为数值
CSV文件处理
CSV文件写入
import csv
# 使用Python标准库csv模块写入CSV文件
with open('output.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['列1', '列2', '列3'])
writer.writerow(['数据1', '数据2', '数据3'])
文件移动操作
Python文件移动教程:https://www.learndatasci.com/solutions/python-move-file/
Python语言特性
条件表达式
Python没有直接的问号语句(如C语言中的 condition ? expression1 : expression2),但有等价的条件表达式
result = value1 if condition else value2
# 这等价于其他语言中的三元条件运算符
外部程序调用
import subprocess
# 在Python中调用外部程序(如antechamber)
def call_antechamber(input_file, output_file):
cmd = f"antechamber -i {input_file} -o {output_file}"
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
return result
退出函数使用
exit函数错误
# 错误:NameError: name 'exit' is not defined
exit()
# 正确:需要导入sys模块
import sys
sys.exit()
作用域问题
仅导入sys模块不足以使exit进入全局作用域,需要明确使用sys.exit()。
JSON数据处理
import json
# 加载JSON数据的标准方法
with open('data.json', 'r') as f:
data = json.load(f)
环境配置优化
PATH环境变量清理
# 清理重复的PATH条目
export PATH=$(echo -n $PATH | awk -v RS=: -v ORS=: '!($0 in a) {a[$0]; print}' | sed 's/:$//')
子进程配置
# subprocess.Popen默认使用/bin/sh
# 若要使用bash需要设置executable参数
subprocess.Popen(..., executable='/bin/bash')
Python subprocess使用bash:https://www.saltycrane.com/blog/2011/04/how-use-bash-shell-python-subprocess-instead-binsh/
代理配置
# 设置HTTP代理
export http_proxy="http://127.0.0.1:7890"
开发工具集成
Python外部程序调用
import subprocess
# 调用外部程序的标准方法
def run_external_command(command):
result = subprocess.run(command, shell=True, capture_output=True, text=True)
return result.stdout, result.stderr
包管理集成
使用subprocess调用系统包管理器:
# 调用antechamber等外部工具
def call_antechamber(input_file, output_file):
cmd = f"antechamber -i {input_file} -o {output_file}"
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
return result
PyCharm环境问题: PyCharm本身是一个代码编辑器(IDE),而不是一个网页浏览器。所以它不能像Chrome或Edge那样直接”打开”并渲染localhost:8501的页面内容。建议端口转发。
相关学习资源
Python打包
科学Python打包指南:https://learn.scientific-python.org/development/guides/packaging-simple/
故障排除与最佳实践
常见错误模式
- 环境冲突:不同conda环境中包版本不兼容
- 连接错误:Web爬虫中的网络连接问题
- 编译问题:Cython跨平台编译差异
- 字符编码:bytes和str处理不当
调试建议
- 隔离测试环境冲突
- 使用虚拟环境避免依赖污染
- 记录完整的编译配置
- 注意跨平台兼容性问题
开发环境检查清单
- Python版本:确保版本兼容性
- 依赖管理:使用requirements.txt或environment.yml
- 虚拟环境:为每个项目创建独立环境
- 代码质量:使用linter和formatter工具
- 性能监控:定期进行性能分析
本文基于2023年9月至2024年上半年的开发实践整理,涵盖Python工程化和开发环境配置的实用技术要点