Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setup.py sdist exclude packages in subdirectory

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.

like image 832
oz123 Avatar asked Oct 24 '14 09:10

oz123


People also ask

What is Package_dir in setup py?

package_dir = {'': 'lib'} in your setup script. The keys to this dictionary are package names, and an empty package name stands for the root package. The values are directory names relative to your distribution root.

What is setup py Sdist?

In the simplest case, python setup. py sdist. (assuming you haven't specified any sdist options in the setup script or config file), sdist creates the archive of the default format for the current platform. The default format is a gzip'ed tar file ( .

What is Sdist package?

Source distribution file format A . tar. gz source distribution (sdist) contains a single top-level directory called {name}-{version} (e.g. foo-1.0 ), containing the source files of the package. The name and version MUST match the metadata stored in the file. This directory must also contain a pyproject.

What is Sdist and Bdist?

sdist is a "source distribution". bdist is a "binary distribution". For a pure Python project, those things are pretty close. If your project includes any extension modules, though, the sdist includes the source for those extension modules and use of the sdist will require a compiler.


3 Answers

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.

like image 137
jfs Avatar answered Nov 15 '22 01:11

jfs


In your MANIFEST.in at project root, add

prune src/test/

then build package with python setup.py sdist

like image 29
All Іѕ Vаиітy Avatar answered Nov 15 '22 01:11

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.

like image 1
bh4r4th Avatar answered Nov 15 '22 01:11

bh4r4th