Search code examples
pythonpython-packaging

Should I include non-Python files in my package?


I have a package in which its version is in a file VERSION.txt,in setup.py I have the following:

with open('VERSION.txt', 'r') as v:
    version = v.read().strip()

 setuptools.setup(
     ...
     version=version,
     ...
 )

If I install and try to run it I get FileNotFoundError: [Errno 2] No such file or directory: 'VERSION.txt', obviously because I am not including the VERSION.txt in my package, but I don't think I should. The way this code is working would need to add include_package_data=True and creating a MANIFEST.in.

Is there any other approach where I could, in a development environment, still have this VERSION.txt but not sending it inside my package?


Solution

  • The version information is usually included in a package, since it can be very useful for a piece of software to know and communicate its own version. A good source to check out how best to do it are project templates, for example the list of official cookiecutters.


    To give my personal favorite, I like putting a variable __version__ = '0.1.2' into my topmost __init__.py file:

    mypackage
    ├───src
    │   └───mypackage
    │       └───__init__.py  # only content here is the version string
    └───setup.py
    
    

    Which I can then import in the setup.py ...

    from src.mypackage import __version__
    
    setuptools.setup(
        ...
        version=__version__,
        package_dir={'': 'src'},
        packages=find_packages(where='src'),
        ...
    )
    

    ... and also easily access when my package is installed

    >>> from mypackage import __version__
    >>> print(__version__)
    0.1.2