在Git项目中,.gitignore 文件用于指定哪些文件和目录应该被Git忽略,不纳入版本控制。对于Python项目,通常需要忽略一些特定的文件和目录,如编译生成的文件、虚拟环境、日志文件等。以下是一个常见的Python项目 .gitignore 配置示例:

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
#  Usually these files are written by a python script from a template
#  before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/
docs/_build/doctrees
docs/_build/html
docs/_build/latex
docs/_build/man
docs/_build/texinfo
docs/_build/text
docs/_build/epub

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints
*.ipynb

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# VS Code
.vscode/

# macOS
.DS_Store

# Windows
Thumbs.db
ehthumbs.db
Desktop.ini
$RECYCLE.BIN/
*.cab
*.msi
*.msm
*.msp

说明

  • 编译生成的文件__pycache__/*.py[cod] 是Python生成的缓存文件,应该忽略。
  • 包管理:例如 build/, dist/, *.egg-info/ 等目录和文件是生成的安装包和分发文件,也应该忽略。
  • 测试和覆盖率报告:例如 .tox/, .coverage, coverage.xml 等文件是测试工具生成的报告文件,应该忽略。
  • 虚拟环境:例如 .env, venv/ 等目录是虚拟环境,通常不需要纳入版本控制。
  • 日志文件:例如 *.log 是日志文件,不需要纳入版本控制。
  • 编辑器和IDE设置:例如 .vscode/ 是VS Code的设置目录,应该忽略。

可以根据自己的项目需求,进一步调整和扩展 .gitignore 文件的内容。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐