setup.py sdist exclude packages in subdirectory

Question:

I have the following project structure I would like to package:

├── doc
│   └── source
├── src
│   ├── core
│   │   ├── config
│   │   │   └── log.tmpl
│   │   └── job
│   ├── scripts
│   └── test
└── tools

I would like to package core under src but exclude test. Here is what I tried unsuccessfully:

      setup(name='core',
      version=version,  
      package_dir = {'': 'src'}, # Our packages live under src but src is not a package itself
      packages = find_packages("src", exclude=["test"]), # I also tried exclude=["src/test"]
      install_requires=['xmltodict==0.9.0',
                        'pymongo==2.7.2',
                        'ftputil==3.1',
                        'psutil==2.1.1',
                        'suds==0.4',
                        ],
      include_package_data=True,
      )

I know I can exclude test using the MANIFEST.in file, but I would be happy if you could show me how to do this with setup and find_packages.

Update:

After some more playing around, I realized that building the package with python setup.py install does what I expected (that is, it excludes test). However, issuing python setup.py sdist causes everything to be included (that is, it ignores my exclude directive). I don’t know whether it is a bug or a feature, but there is still the possibility of excluding files in sdist using MANIFEST.in.

Asked By: oz123

||

Answers:

Assuming that your folder is called tests and not test, it should work with the following code:

  setup(name='core',
  version=version,  
  package_dir = {'': 'src'}, # Our packages live under src but src is not a package itself
  packages = find_packages('src', exclude=['tests'])
  install_requires=['xmltodict==0.9.0',
                    'pymongo==2.7.2',
                    'ftputil==3.1',
                    'psutil==2.1.1',
                    'suds==0.4',
                    ],
  include_package_data=True,
  )
Answered By: ssalentin

find_packages("src", exclude=["test"]) works.
The trick is to remove stale files such as core.egg-info directory. In your case you need to remove src/core.egg-info.

Here’s setup.py I’ve used:

from setuptools import setup, find_packages

setup(name='core',
      version='0.1',
      package_dir={'':'src'},
      packages=find_packages("src", exclude=["test"]), # <- test is excluded
      ####packages=find_packages("src"), # <- test is included
      author='J.R. Hacker',
      author_email='[email protected]',
      url='http://stackoverflow.com/q/26545668/4279',
      package_data={'core': ['config/*.tmpl']},
)

To create distributives, run:

$ python setup.py sdist bdist bdist_wheel

To enable the latter command, run: pip install wheel.

I’ve inspected created files. They do not contain test but contain core/__init__.py, core/config/log.tmpl files.

Answered By: jfs

In your MANIFEST.in at project root, add

prune src/test/

then build package with python setup.py sdist

Answered By: All Іѕ Vаиітy

I probably just use wild cards as defined in the find_packages documentation. *test* or *tests* is something I tend to use as we save only test filenames with the word test. Simple and easy ^-^.

setup(name='core',
  version=version,  
  package_dir = {'': 'src'}, # Our packages live under src but src is not a package itself
  packages = find_packages("src", exclude=['*tests*']), # I just use wild card. Works perfect ^-^
  install_requires=['xmltodict==0.9.0',
                    'pymongo==2.7.2',
                    'ftputil==3.1',
                    'psutil==2.1.1',
                    'suds==0.4',
                    ],
  include_package_data=True,
  )

FYI:

I would also recommend adding following into .gitignore.

build
dist
pybueno.egg-info

And move build and pushing package to pypi or your private repository bit into CI/CD to make whole setup look clean and neat.

Answered By: bh4r4th
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.