Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to include subdirectories using dist utils (setup.py) as part of package data?

Basically my python package is setup like:

module \_examples   \_folder1      \_file1.py      \_file2.py   \_folder2     \_file1.py     \_file2.py 

Basically I want to just use:

package_data  = {              'module': ['examples/*'],   }, 

because my project always has people adding examples and I want it to be easy to list them from within my application. I can get it work for any FILE within examples, but not re-curse down through sub-directories. Is this possible?

like image 412
xamox Avatar asked Jun 07 '12 02:06

xamox


People also ask

How do you include data in a python package?

Place the files that you want to include in the package directory (in our case, the data has to reside in the roman/ directory). Add the field include_package_data=True in setup.py. Add the field package_data={'': [... patterns for files you want to include, relative to package dir...]} in setup.py .

What is the command used for building a source distribution Python setup py?

To build a source distribution, use the command line to navigate to the directory containing setup.py, and run the command python setup.py sdist.

How do I create a Python package using setup py?

Installing Python Packages with Setup.py To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.


2 Answers

I believe what you're looking for is something like this for you setup.py, which will recursively find any packages in the project, also be sure and include __init__.py files to subdirectories for each package you want.

from setuptools import setup, find_packages  setup(name='MySoftware',       packages=find_packages() ) 
like image 167
defermat Avatar answered Sep 21 '22 00:09

defermat


Introduction

I came across this post and spent some time figuring out how to add specific sub-modules to my package, so I will post my solution here.

Solution

In my package root folder, I have a setup.py file see doc
In this file, I have the following code:

from setuptools import setup  with open("README.md", "r") as fh:     long_description = fh.read()  setup(     name='package name',     version='0.4.1',     description='short description',     long_description=long_description,     long_description_content_type="text/markdown",     url='repository url',     author='My name',     author_email='[email protected]',     license='MIT',     packages=['PackageName','PackageName.SubModule'],     zip_safe=False,     install_requires=[         'dependecy1',     ],     classifiers=[         'Development Status :: 3 - Alpha',         'License :: OSI Approved :: MIT License',         'Programming Language :: Python :: 3.7'     ] )  

The interesting part to answer the question, here is : packages=['PackageName','PackageName.SubModule'],

By following this syntax, you can include sub-modules to your main package distribution.

More info about all the others arguments can be found in the doc.

like image 28
Séraphin Avatar answered Sep 23 '22 00:09

Séraphin