Today I created a setup.py-file and found one working script and two none working scenarios.
In the two non working scenarios the package_data
after is missing in the build. I created the build with python setup.py sdist
.
import os
from distutils.core import setup
def find_packages(path):
package_path = os.path.join(os.path.dirname(__file__), path)
packages = []
for f in os.walk(package_path):
directory_name = f[0]
if directory_name.endswith('egg-info') or directory_name.endswith('path'):
continue
directory_name = directory_name.replace(package_path, '').replace('/', '.').strip()
if not len(directory_name):
continue
if directory_name.startswith('.'):
directory_name = directory_name.replace('.', '', 1)
packages.append(directory_name)
return packages
setup (
name = 'mypkg',
packages = find_packages('src'),
package_dir = {'mypkg': 'src/mypkg'},
include_package_data = True,
package_data = {
'': ['*.txt'],
'mypkg': ['data/*.dat'],
}
)
from setuptools import setup #, find_packages
from setuptools.command import sdist
setup (
name = 'mypkg',
packages = ['mypkg'],
package_dir = {'mypkg': 'src/mypkg'},
include_package_data = True,
package_data = {
'': ['*.txt'],
'mypkg': ['data/*.dat'],
}
)
from setuptools import find_packages
from setuptools.command import sdist
setup (
name = 'mypkg',
packages = find_packages('src'),
package_dir = {'mypkg': 'src/mypkg'},
include_package_data = True,
package_data = {
'': ['*.txt'],
'mypkg': ['data/*.dat'],
}
)
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 .
A Python file that relies only on the standard library can be redistributed and reused without the need to use setuptools. But for projects that consist of multiple files, need additional libraries, or need a specific version of Python, setuptools will be required.
Type “ pip install setuptools ” (without quotes) in the command line and hit Enter again. This installs setuptools for your default Python installation.
In my case, the problem wasn't in setup.py but the missing MANIFEST.in, which needs to also declare package data.
you can not use glob syntax directly in package_data declaration.
but you can declare a variable containing this data before passing it to setup function:
from glob import glob
data = glob('data/*.dat')
txt_files = glob('*.txt')
...
setup(...
package_data = {
'': txt_files,
'mypkg': data,
}
...
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