
在python项目的早期开发实践中,setup.py文件是项目配置和构建的核心。通过执行python setup.py clean --all命令,开发者可以方便地清除项目构建过程中产生的所有临时文件和目录。然而,随着python生态系统的发展,pep 517和pep 518的引入推动了标准化构建后端(如build模块)和pyproject.toml文件的普及。现在,项目通常使用pyproject.toml来声明其构建系统依赖和元数据,并通过python -m build等命令进行构建。
这种转变带来了一个问题:对于不再包含setup.py文件的项目,传统的clean命令已不再适用。因此,理解并掌握手动或脚本化清理这些构建产物的方法,对于维护项目整洁和解决潜在的构建问题至关重要。
在没有setup.py的情况下,构建工具(如build、setuptools等)或Python解释器本身在运行过程中会生成一系列临时文件和目录。这些是清理工作的核心目标:
__pycache__ 目录
*`.pyc` 文件**
立即学习“Python免费学习笔记(深入)”;
.swp 文件
build 目录
dist 目录
*.egg-info 或 *.dist-info 目录
测试缓存目录 (例如 .pytest_cache)
类型检查缓存目录 (例如 .mypy_cache)
由于没有统一的clean命令,我们需要通过手动或脚本化的方式来删除这些文件和目录。
对于小型项目或不频繁的清理操作,可以直接通过文件管理器导航到项目目录,然后手动删除上述提到的文件和目录。
这是最常用且高效的方法,适用于各种操作系统。
a. Linux / macOS (使用 find 和 rm)
在终端中,进入项目根目录,然后执行以下命令:
# 清理 __pycache__ 目录
find . -type d -name "__pycache__" -exec rm -rf {} +
# 清理 *.pyc 文件
find . -type f -name "*.pyc" -delete
# 清理 .swp 文件
find . -type f -name "*.swp" -delete
# 清理 build 目录
rm -rf build
# 清理 dist 目录
rm -rf dist
# 清理 *.egg-info 或 *.dist-info 目录
find . -type d -name "*.egg-info" -exec rm -rf {} +
find . -type d -name "*.dist-info" -exec rm -rf {} +
# 清理测试和类型检查缓存
rm -rf .pytest_cache
rm -rf .mypy_cache
# 综合清理命令 (推荐在项目根目录执行)
echo "Cleaning Python build artifacts..."
find . -type d -name "__pycache__" -exec rm -rf {} +
find . -type f -name "*.pyc" -delete
find . -type f -name "*.swp" -delete
rm -rf build dist .pytest_cache .mypy_cache
find . -type d -name "*.egg-info" -exec rm -rf {} +
find . -type d -name "*.dist-info" -exec rm -rf {} +
echo "Cleaning complete."b. Windows (使用 for /D 和 del)
在PowerShell或命令提示符中,进入项目根目录,然后执行以下命令:
# 清理 __pycache__ 目录
Get-ChildItem -Path . -Recurse -Directory -ErrorAction SilentlyContinue | Where-Object { $_.Name -eq "__pycache__" } | Remove-Item -Recurse -Force
# 清理 *.pyc 文件
Get-ChildItem -Path . -Recurse -Include *.pyc -ErrorAction SilentlyContinue | Remove-Item -Force
# 清理 .swp 文件
Get-ChildItem -Path . -Recurse -Include *.swp -ErrorAction SilentlyContinue | Remove-Item -Force
# 清理 build 目录
Remove-Item -Path "build" -Recurse -Force -ErrorAction SilentlyContinue
# 清理 dist 目录
Remove-Item -Path "dist" -Recurse -Force -ErrorAction SilentlyContinue
# 清理 *.egg-info 或 *.dist-info 目录
Get-ChildItem -Path . -Recurse -Directory -ErrorAction SilentlyContinue | Where-Object { $_.Name -like "*.egg-info" -or $_.Name -like "*.dist-info" } | Remove-Item -Recurse -Force
# 清理测试和类型检查缓存
Remove-Item -Path ".pytest_cache" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path ".mypy_cache" -Recurse -Force -ErrorAction SilentlyContinue
# 综合清理命令 (推荐在项目根目录执行)
Write-Host "Cleaning Python build artifacts..."
Get-ChildItem -Path . -Recurse -Directory -ErrorAction SilentlyContinue | Where-Object { $_.Name -eq "__pycache__" } | Remove-Item -Recurse -Force
Get-ChildItem -Path . -Recurse -Include *.pyc, *.swp -ErrorAction SilentlyContinue | Remove-Item -Force
Remove-Item -Path "build", "dist", ".pytest_cache", ".mypy_cache" -Recurse -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path . -Recurse -Directory -ErrorAction SilentlyContinue | Where-Object { $_.Name -like "*.egg-info" -or $_.Name -like "*.dist-info" } | Remove-Item -Recurse -Force
Write-Host "Cleaning complete."虽然.gitignore不能直接清理已存在的文件,但它能阻止这些临时文件被误提交到版本控制系统,从而保持仓库的整洁。在项目的.gitignore文件中添加以下条目:
# Byte-code files __pycache__/ *.pyc # Build artifacts build/ dist/ *.egg-info/ *.dist-info/ # Editor swap files *.swp # Testing and type checking caches .pytest_cache/ .mypy_cache/ # Other common temporary files .coverage .venv/ # If you manage virtual environment inside the project folder venv/
对于大型项目或团队协作,将清理命令封装成一个脚本或使用任务运行器(如Makefile、invoke、npm scripts)是一个更好的选择。
a. Python 清理脚本 (clean.py)
可以在项目根目录创建一个clean.py脚本:
import shutil
import glob
import os
def clean_build_artifacts():
"""Removes common Python build artifacts and temporary files."""
print("Cleaning Python build artifacts...")
# Directories to remove
dirs_to_remove = [
"build",
"dist",
"__pycache__",
".pytest_cache",
".mypy_cache",
]
for d in dirs_to_remove:
if os.path.exists(d):
print(f" Removing directory: {d}")
shutil.rmtree(d)
# Files to remove
file_patterns_to_remove = [
"**/*.pyc",
"**/*.swp",
"**/*.egg-info",
"**/*.dist-info",
]
for pattern in file_patterns_to_remove:
# Use glob.glob with recursive=True for Python 3.5+
for f in glob.glob(pattern, recursive=True):
if os.path.isfile(f) or os.path.isdir(f): # Check if it's a file or dir
print(f" Removing: {f}")
if os.path.isfile(f):
os.remove(f)
else: # It's a directory
shutil.rmtree(f)
print("Cleaning complete.")
if __name__ == "__main__":
clean_build_artifacts()然后通过 python clean.py 执行清理。
b. Makefile (Linux/macOS)
在项目根目录创建Makefile文件:
.PHONY: clean
clean:
@echo "Cleaning Python build artifacts..."
find . -type d -name "__pycache__" -exec rm -rf {} +
find . -type f -name "*.pyc" -delete
find . -type f -name "*.swp" -delete
rm -rf build dist .pytest_cache .mypy_cache
find . -type d -name "*.egg-info" -exec rm -rf {} +
find . -type d -name "*.dist-info" -exec rm -rf {} +
@echo "Cleaning complete."然后通过 make clean 执行清理。
随着Python项目构建工具和实践的演进,setup.py的传统清理方式已逐渐被淘汰。适应这一变化,掌握手动识别和清理构建产物的方法,并通过命令行工具、.gitignore或自动化脚本来管理项目整洁,是现代Python开发者必备的技能。定期清理不仅能释放磁盘空间,还能避免因旧的构建缓存导致的潜在问题,确保项目始终在一个干净、可控的环境中运行和构建。
以上就是Python项目构建文件清理指南:告别setup.py的现代化实践的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号