Python setup.py include .json files in the egg - python

I want to package .json files as well in the python egg file.
For example: boto package has endpoints.json file. But when I run python setup.py bdist_egg it does not include the json file in the egg. How do I include the Json file in the egg?
How do I include *.json file in the egg?
Below is the setup.py code
from setuptools import setup, find_packages, Extension
setup(
name='X-py-backend',
version='tip',
description='X Python backend tools',
author='meme',
packages=find_packages('python'),
package_dir={'': 'python'},
data_files=[('boto', ['python/boto/endpoints.json'])],
namespace_packages = ['br'],
zip_safe=True,
)
setup(
name='X-py-backend',
version='tip',
packages=find_packages('protobuf/target/python'),
package_dir={'': 'protobuf/target/python'},
namespace_packages = ['br'],
zip_safe=True,
)

You only need to list the file on data_files parameter. Here is the example.
setup(
name='X-py-backend',
version='tip',
description='XXX Python backend tools',
author='meme',
packages=find_packages('python'),
package_dir={'': 'python'},
data_files=[('boto', ['boto/*.json'])]
namespace_packages = ['br'],
zip_safe=True
)
You can see the details here. https://docs.python.org/2/distutils/setupscript.html#installing-additional-files
Another way to do this is to use MANIFEST.in files. you need to create a MANIFEST.in file in your project root. Here is the example.
include python/boto/endpoints.json
Please visit here for more information.https://docs.python.org/2/distutils/sourcedist.html#manifest-template

Well this works for me.
setup.py:
from setuptools import setup, find_packages
setup(
name="clean",
version="0.1",
description="Clean package",
packages=find_packages() + ['config'],
include_package_data=True
)
MANIFEST.in:
recursive-include config *
where there is a config file under the project root directory which contains a whole bunch of json files.
Hope this helps.

Related

How to package a pure data file project by setup.py?

One of my projects only contain data file (.txt) files, and the structure:
fsa/
e/
f/
MANIFEST.in
setup.py
where 'fsa' is the root directory, and e, f are subdirectories, which contains all txt files.
My setup.py:
setup(name='fsa', #to uninstall, use this package name
version='1.0',
description='a test file',
packages=find_packages(),
include_package_data=True,
zip_safe=False)
My MANIFEST.in:
recursive-include . *
I want to recursively include all files under the root directory, so that I use the 'recursive-include' command.
However, either my setup.py or MANIFEST has some problems. It doesn't install the package properly.

How to add all *.py files to the package with setup.py?

I have a directory with a lot of *.py files (scripts) and subdirs with *.py files.
How to add all *.py files from root directory to the package?
Now my setup.py is
from setuptools import setup, find_packages
setup(
name='my-awesome-helloworld-script', # This is the name of your PyPI-package.
version='0.1', # Update the version number for new releases
scripts=['????????????????????'], # The name of your scipt, and also the command you'll be using for calling it
# Packages
packages=find_packages(),
)
As you can see, I've solved addition question of folders with find_packages().
But how to add *.py files from root directory?
I am packaging with command python.exe setup.py sdist.
Thank you!
Solved with code:
from setuptools import setup, find_packages
pckgs=find_packages()
pckgs.append('.')
setup(
name='my-awesome-helloworld-script', # This is the name of your PyPI-package.
version='0.1', # Update the version number for new releases
# scripts=[''], # The name of your scipt, and also the command you'll be using for calling it
# Packages
packages=pckgs,
)

How to include package sub-folders in my project distribution?

I have a project with this structure:
SomeProject/
bin/
CHANGES.txt
docs/
LICENSE.txt
MANIFEST.in
README.txt
setup.py
someproject/
__init__.py
location.py
utils.py
static/
javascript/
somescript.js
And a "setup.py" as follows:
#!/usr/bin/env python
import someproject
from os.path import exists
try:
from setuptools import setup, find_packages
except ImportError:
from distutils.core import setup, find_packages
setup(
name='django-some-project',
version=someproject.__version__,
maintainer='Some maintainer',
maintainer_email='some#manteiner.com',
packages=find_packages(),
include_package_data=True,
scripts=[],
url='https://github.com/xxx/some-project',
license='LICENSE',
description='Some project description.',
long_description=open('README.markdown').read() if exists("README.markdown") else "",
install_requires=[
"Django >= 1.4.0"
],
)
Then, when I upload it using the command:
python setup.py sdist upload
It seems ok, but there is no "static" folder with this "javascript" subfolder in the package. My "setup.py" was inspired on github.com/maraujop/django-crispy-forms that has a similar structure. Any hint on what is wrong on uploading this subfolders?
You should be able to add those files to source distributions by editing the MANIFEST.in file to add a line like:
recursive-include someproject/static *.js
or just:
include someproject/static/javascript/*.js
This will be enough to get the files included in source distributions. If the setuptools include_package_data option you're using isn't enough to get the files installed, you can ask for them to be installed explicitly with something like this in your setup.py:
package_data={'someproject': ['static/javascript/*.js']},
Use following
packages = ['.','templates','static','docs'],
package_data={'templates':['*'],'static':['*'],'docs':['*'],},

Include non python files in RPM with setuptools

I have some fixture directories that contain xml files that I would like included with my python project when building the RPM with bdist_rpm. I thought I could do this by having MANIFEST.in do a recursive-include * *, however, it does not include anything other than *.py files. Is there anyway to have bdist_rpm include non python files in the package or specifically include *.xml files as well?
Where are you trying to install them? If you put them inside a package directory, like this...
myproject/
mypackage/
__init__.py
resources/
file1.xml
file2.xml
...you can use the package_data option in your setup.py file, like this:
from setuptools import setup, find_packages
setup(
name='myproject',
version='0.1',
description='A description.',
packages=find_packages(),
include_package_data=True,
package_data = { '': [ '*.xml' ] },
install_requires=[],
)
This will recursively include any *.xml files inside of any packages. They'll get installed with the rest of your package(s) somewhere inside of the Python library path. You can do the same thing with a MANIFEST.in that looks like this:
recursive-include * *.xml
If you're trying to install them into specific filesystem locations outside of the Python library, I'm not sure if you can do that via setup.py.
You can use data_files parameter of setup to do what you need. Something like this:
setup(
...
package_data = { '/usr/share/yourapp/xmls': [ 'xmls/1.xml', 'xmls/2.xml' ] },
...
)
This would install following files:
/usr/share/yourapp/xmls/1.xml
/usr/share/yourapp/xmls/2.xml
I generally create the list of files in a function like this:
def get_xmls():
xmlfiles = []
for filename in os.listdir('xmls/'):
if filename.endswith('.xml'):
xmlfiles.append('xmls/%s' % filename)
return xmlfiles

Can't figure out how to include *.html files within a site-packages folder

I cannot figure out how to write my setup.py script in order to include *.html files within the installed package.
Here is my attempt:
import os
from setuptools import setup, find_packages
setup(name='django-testapp',
version='0.1',
author='RadiantHex',
license='BSD',
keywords='testapp,django',
packages=['testapp']],
include_package_data=True,
data_files = os.walk('testapp'),
zip_safe = False,
)
The *.html files are contained within the testapp folder.
Any ideas?
Add following 'package_data' argument to the setup():
setup(...,
package_data={
'testapp' : ['testapp/*.html']
}, ...)

Categories