【问题标题】:Should I put pyc files under version control?我应该将 pyc 文件置于版本控制之下吗?
【发布时间】:2015-08-20 05:25:21
【问题描述】:

我在 Python 中创建了一个 GitHub 项目。第一次运行项目后,里面出现了一些.pyc文件。我应该将它们置于版本控制之下并将它们提交到我的 fork 吗?

【问题讨论】:

  • 不! …………
  • 不,它是相同代码的“字节编译”版本。见stackoverflow.com/questions/3878479/…
  • @skyking 为什么不会你将.gitignore置于源代码控制之下?
  • 我不明白这是一个意见问题;期望用户单独定义一个忽略文件是不必要的工作,并且可能导致错误的提交。如果您的项目生成的文件或路径不应该是 repo 的一部分,您应该有一个签入的.gitignore 文件。如果有需要忽略的系统特定文件,应在~/.gitconfig 中指定。
  • .gitignore 用于需要所有人共享的模式,.git/info/exclude 用于私人模式列表。

标签: python version-control


【解决方案1】:

你不应该。 .pyc 文件包含字节码,对于不同的 Python 版本和实现可能会有所不同。

只需在您的.gitignore 或全局gitignore 中添加*.pyc 行。

还可以查看几乎所有平台的great collection of gitignore 文件。您可以将它用于您的 python 项目:

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

# C extensions
*.so

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

# 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/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

【讨论】:

  • 就我个人而言,我更愿意根据需要忽略文件,而不是检查一堆实际上没有使用的忽略模式。
【解决方案2】:

这些文件是compiled versions of the code already in the repo,以便 Python 可以更快地执行代码。由于它们是实际源代码的直接计算结果,因此签入它们没有任何好处 - 每次更新源代码时都需要更新它们。此外,不保证(据我所知)不同的机器或 Python 版本会生成兼容的 .pyc 文件,这意味着分发您生成的 .pyc 文件可能会破坏其他人的环境。

相反,您可以修复 .gitignore 文件以忽略 .pyc 文件并将 that 提交到您的 fork(甚至返回上游 repo)。这样以后就不会有人注意到或担心这些文件了。

【讨论】:

    【解决方案3】:

    该文件没有什么不好,但它是无用的垃圾,它只是为了加快 python 应用程序的执行,并且每次您进行更改时都会重新构建它,因此它会随着时间的推移而增长,您可能会修复它想将__pycache__ 行添加到您的.gitignore 文件中

    【讨论】:

      【解决方案4】:

      没有。您不得将 pyc 置于版本控制之下

      一般规则是“永远不要将构建工件放入源代码控制中,因为您在源代码控制中有源代码并且可以|必须重复过程”

      PYC 是对应 PY 文件的此类工件

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-07
        • 2020-11-22
        • 2012-01-04
        • 2012-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多