Search code examples
pythonversion-controlvirtualenvpyenv

How do I manage python versions in source control for application?


We have an application that uses pyenv/virtualenv to manage python dependencies. We want to ensure that everyone who works on the application will have the same python version. Coming from ruby, the analog is Gemfile. To a certain degree, .ruby-version.

What's the equivalent in python? Is it .python-version? I've seen quite a few .gitignore that have that in it and usually under a comment ".pyenv". What's the reason for that? And what's the alternative?


Solution

  • Recent versions of setuptools (24.2.0+) allow you to control Python version at the distribution level.

    For example, suppose you wanted to allow installation only on a (compatible) version of Python 3.6, you could specify:

    # in setup.py
    from setuptools import setup
    
    setup(
        ...
        python_requires='~=3.6',
        ...
    )
    

    The distribution built by this setup would have associated metadata which would prevent installation on incompatible Python version. Your clients need a current version of pip for this feature to work properly, older pip (<9.0.0) will not check this metadata.

    If you must extend the requirement to people using older version of pip, you may put an explicit check on sys.version somewhere in the module level of the setup.py file. However, note that with this workaround, the package will still be downloaded by pip - it will fail later, on a pip install attempt with incorrect interpreter version.