Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include license file in setup.py script?

I have written a Python extension module in C++. I plan to distribute the module with setuptools. There will be binary distributions for 32- and 64-bit Windows (built with setup.py bdist_egg) and a source distribution for UNIX-like platforms (built with setup.py sdist).

I plan to license the module under the BSD license. In my source tree, the file LICENSE.txt is in the top folder along with setup.py. How should I include it in the installation package?

I tried the following setup.py script:

from setuptools import setup, Extension from glob import glob  setup(     name = 'Foo',     version = '0.1.0',     ext_modules = [Extension('Foo', glob('Source/*.cpp'))],     package_data = {'': ['LICENSE.txt']} ) 

It did not work, the license file is not included in the installation package. Maybe because the setup.py file does not define any packages, only a single extension module.

How do I fix this?

like image 295
Johan Råde Avatar asked Apr 02 '12 14:04

Johan Råde


People also ask

How do I add a license to a Python code?

To add license checking to a Python application you import the Licensing.py file into your application. You can then call the checkLicense function by calling Init to load the license information and check that the user has a valid license.

What goes in a setup py file?

The setup.py file may be the most significant file that should be placed at the root of the Python project directory. It primarily serves two purposes: It includes choices and metadata about the program, such as the package name, version, author, license, minimal dependencies, entry points, data files, and so on.

Does pip install use setup py?

@joeforker, pip uses setup.py behind the scenes.


2 Answers

Write a setup.cfg file and in there specify:

[metadata] license_files = LICENSE.txt 

For this to work it seems like wheel is required to be installed. That is:

pip install wheel 

If you have wheel already installed and it doesn't work, try to update it:

pip install --upgrade wheel 

Then when installing the package via pip install <path> the LICENSE file gets included.

like image 181
Al Conrad Avatar answered Oct 02 '22 23:10

Al Conrad


Since setuptools 42.0.0 you could use the license_files key to specify a list of license files to be included into a distribution.

Note that due to implementation details you actually don't need to put this key into setup.cfg file (as documentation and another answer suggest). You could simply supply it as an argument to setup() function:

from setuptools import setup  setup(     ...     license_files = ('LICENSE.txt',),     ... ) 

Also note that while these files will be included in both binary (wheel) and source distributions, they won't be installed with your package from setup.py-style source distribution if the user doesn't have a wheel package installed!
To ensure the license files will be installed along with your package you need to make some additional modifications to your setup script:

from setuptools import setup from setuptools.command.egg_info import egg_info   class egg_info_ex(egg_info):     """Includes license file into `.egg-info` folder."""      def run(self):         # don't duplicate license into `.egg-info` when building a distribution         if not self.distribution.have_run.get('install', True):             # `install` command is in progress, copy license             self.mkpath(self.egg_info)             self.copy_file('LICENSE.txt', self.egg_info)          egg_info.run(self)   setup(     ...     license_files = ('LICENSE.txt',),     cmdclass = {'egg_info': egg_info_ex},     ... ) 

If your project is a pyproject.toml-style project and you think it will be installed by PEP 517-compatible frontend (e.g. pip>=19), a wheel will be forcibly built from your sources and the license files will be installed into .dist-info folder automatically.

like image 33
EvgenKo423 Avatar answered Oct 02 '22 22:10

EvgenKo423