Search code examples
pythonpipsetuptoolsdistutilspython-packaging

How to include additional files into a package?


How can I include files that are not written in python to the packaging? Or to make it more concrete: Assuming I have the following package structure, how do I include all the files of the templates directory?

- projectname
  - __init__.py
  - classfile.py
  - templates
    - file1.prototxt
    - file2.caffemodel

I have the following setup.py

from distutils.core import setup
setup(
  name = 'projectname',    
  packages = ['projectname'], 
  version = '0.1',      
  license='MIT',        
  description = 'my description',  
  author = 'FULL NAME',                
  author_email = 'example@gmail.com',     
  url = 'https://github.com/repo',   
  download_url = 'https://github.com/repo/.../v_01.tar.gz',
  keywords = ['keywords'],  
  install_requires=[      
          'opencv-python',
          'numpy'
      ],
  classifiers=[
    'Development Status :: 3 - Alpha',      
    'Intended Audience :: Developers',      
    'Topic :: Software Development :: Build Tools',
    'License :: OSI Approved :: MIT License',  
    'Programming Language :: Python :: 3',     
  ],
)

As I read somewhere on stackoverflow, I tried adding the following to the setup.py file but it didn't make a difference.

package_data = {
      'templates': ['*'],
  }

Solution

  • Preamble. The word "package" is heavily overloaded. I'll use the word meaning "importable package", i.e. an directory with __init__.py.

    I see two problems with your code. First, you use distutils which is an obsolete outdated package. It will be removed from Python. You should switched to setuptools long ago. So replace the line

    from distutils.core import setup
    

    with

    from setuptools import setup
    

    The 2nd problem is in

    package_data = {
        'templates': ['*'],
    }
    

    The keys in the dictionary are packages, not directory names. Your templates is not a package (no __init__.py) but a data directory. So the way to include it is

    package_data = {
        'projectname': ['templates/*'],
    }