我们应该忽略.pythonversion文件吗?

2024-09-28 05:25:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个.python-version文件,当我用github创建Python repo并指定它应该有一个.gitignore时,它会将.python-version文件添加到其中。在我看来,这个文件不应该被忽略,因为在不同机器上运行代码的其他人会想知道他们需要什么版本的Python。在

为什么是.gitignored?在


Tags: 文件代码版本github机器versionrepogitignore
3条回答

.python-version应该被gitignored的原因是它的版本太具体了。Python的小版本(例如2.7.1 vs 2.7.2)通常彼此兼容,因此您不希望锁定到特定的小版本。此外,许多Python应用程序或库应该使用一系列Python版本,而不仅仅是一个特定版本。使用.python-version表示您希望其他开发人员使用精确的、特定的Python版本,这通常不是一个好主意。在

如果您想指出所需的最低Python版本,或者其他版本范围,那么我认为在自述文件中记录这一点是更合适的解决方案。在

尽管过于具体,您仍然可以将该文件的版本(意思是:不包括在默认的.gitignore)中,如下所示:

  • 它将仅由^{}使用
  • 它是对README的一个很好的补充,为了说明为特定项目推荐什么版本的python
  • 它可以很容易地被重写(如果您正在使用pyenv),也可以简单地忽略(如果您没有pyenv)。在

正如文章“How to manage multiple Python versions and virtual environments ”所述:

When setting up a new project that is to use Python 3.6.4 then pyenv local 3.6.4 would be ran in its root directory.
This would both set the version, and create a .python-version file, so that other contributors’ machines would pick it up.

但是:

pyenv looks in four places to decide which version of Python to use, in priority order:

  1. The PYENV_VERSION environment variable (if specified).
    You can use the pyenv shell command to set this environment variable in your current shell session.
  2. The application-specific .python-version file in the current directory (if present).
    You can modify the current directory's .python-version file with the pyenv local command.
  3. The first .python-version file found (if any) by searching each parent directory, until reaching the root of your filesystem.
  4. The global version file. You can modify this file using the pyenv global command.
    If the global version file is not present, pyenv assumes you want to use the "system" Python. (In other words, whatever version would run if pyenv weren't in your PATH.)

在使用python虚拟环境时,它也可能有点问题,因为人们可能希望使用与3.7.2/envs/myvenv不同的虚拟环境名称。在

相关问题 更多 >

    热门问题