39

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?

xamox
  • 2,419
  • 4
  • 27
  • 29

7 Answers7

51

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()
)
hynekcer
  • 13,899
  • 5
  • 58
  • 92
defermat
  • 613
  • 5
  • 6
  • 2
    Be careful here, because you might accidentally include test directories, etc. unless you explicitly `exclude=...` these. – Arminius Jan 09 '18 at 23:21
9

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='my@e.mail',
    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.

Camilo Terevinto
  • 29,179
  • 6
  • 82
  • 109
Séraphin
  • 500
  • 5
  • 15
7

You'll have to use a MANIFEST.in file for that.

I believe you'll want something like this:

$ cat MANIFEST.in
recursive-include examples/ *.py
David Wolever
  • 139,281
  • 83
  • 327
  • 490
  • I think you can also directly use Python functions in setup.py to find files and directories, but I can't quite find the exact answer at the moment. – Jeff Tratner Jun 07 '12 at 05:20
  • 1
    Hrm, I will have to give it a try as I did: recursive-include examples *.py and didn't use the /. I read that MANIFEST.in is being deprecated though. I think I may just have to follow the python function call method. – xamox Jun 07 '12 at 17:58
  • 2
    Oh… Well, if you figure out how to do it with Python, please post here. I'd like to know. – David Wolever Jun 07 '12 at 18:34
4

Yes, you can include all the subdirectories.

You just need to pass the below args to setup() function:

packages=find_packages()

include_package_data=True

Along with this you need to have a MANIFEST.in file, with contents as

recursive-include examples *

This ensures all the files are recursively included.

If you want to exclude certain extensions specifically, you can do so by specifying exclude array in the find_packages() argument.

Ex: To exclude .txt files

packages=find_packages(exclude=['.txt'])

You can read more about include_package_data here.

And also here is another link which tells you when you should not use include_package_data

Community
  • 1
  • 1
ajays20078
  • 338
  • 1
  • 2
  • 10
2

None of the suggested answers worked for me in a similar situation.

I needed to make a distribution with my package, which included several sub-modules in a sub-directory, so that these were the files I needed to go into sdist:

ipyexperiments/*py
ipyexperiments/utils/*py

and no matter what I tried, the subdir utils's modules were not getting included in sdist.

What worked for me is leaving config.py's default:

# config.py
from setuptools import setup, find_packages
[...]
setup(
    packages = find_packages(),
    [...]
)

but adding to MANIFEST.in:

# MANIFEST.in
graft ipyexperiments

and everything under ipyexperiments was included.

If you don't already have MANIFEST.in, create it at the same directory as config.py.

I also added to MANIFEST.in

prune tests
global-exclude *.py[co]

to exclude all of tests directory and any unwanted *pyc and *.pyo files anywhere.

stason
  • 4,417
  • 3
  • 28
  • 44
1

You can make use of include parameter of find_packages():

...
setup(name="module",
      packages=find_packages(include=('module*',)),
...
Jahid
  • 19,822
  • 8
  • 86
  • 102
0

Following what David Wolever said, just to make it a little more clear. If you want to include everything under a sub-directory folder you have to explicitly specify each file in the MANIFEST.in,

recursive-include examples/ *.py *.png *.sh etc.....

It would be nice if the manifest.in would just understand examples/ and include everything but oh well.