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
.
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
.
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.
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 ( .
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.
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.
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.
In your MANIFEST.in
at project root, add
prune src/test/
then build package with python setup.py sdist
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With