How include static files to setuptools - python package
As pointed out in the comments, there are 2 ways to add the static files:
1 - include_package_data=True + MANIFEST.in
A MANIFEST.in
file in the same directory of setup.py
that looks like this:
include src/static/*
include src/Potato/*.txt
With include_package_data = True
in setup.py
.
2 - package_data in setup.py
package_data = {
'static': ['*'],
'Potato': ['*.txt']
}
Specify the files inside the setup.py
.
Do not use both include_package_data
and package_data
in setup.py
.
include_package_data
will nullify the package_data
information.
Official docs:
https://setuptools.readthedocs.io/en/latest/userguide/datafiles.html
How to use static files included with setuptools?
change foobar.py
to:
import os
import pickle
def out():
with open(os.path.join(os.path.dirname(__file__), "pickle_files", "test.p"), "wb") as f:
pickle.dump({"hello": "world"}, f)
def f_in():
with open(os.path.join(os.path.dirname(__file__), "pickle_files", "test.p"), "rb") as f:
d = pickle.load(f)
print(d["hello"])
when you launch it from other place you have different starting point for decoding path.
including folder and files inside a Python package
As this question answers:
There are 2 ways to add the static files:
1) Include_package_data=True + MANIFEST.in
A MANIFEST.in file in the same directory of setup.py, that looks like this:
include src/static/*
include src/Potato/*.txt
2) Package_data in setup.py
package_data = {
'static': ['*'],
'Potato': ['*.txt']
}
Specify the files inside the setup.py.
Including static data in setup.py (setuptools)
I solved the problem by processing the static files separately, not using setuptools.
from sys import argv
try:
if argv[1] == 'install':
from os.path import join
from distutils.sysconfig import get_python_lib
from shutil import copytree
OrigSkeleton = join('src', 'skeleton')
DestSkeleton = join(get_python_lib(), 'cumulus', 'skeleton')
copytree(OrigSkeleton, DestSkeleton)
except IndexError: pass
Include static files in installed directory
Your developemt directory structure is wrong. It must be the same as installed package:
/mypackage
setup.py
MANIFEST.in
/mypackage
__init__.py
code.py
/static
app.js
setup.py
:
setup (
packages = ['mypackage'],
package_data = {
'mypackage': ['static/app.js']
},
...
)
Now generate distributions: python.setup.py sdist bdist_wheel
Including non-Python files with setup.py
Probably the best way to do this is to use the setuptools
package_data
directive. This does mean using setuptools
(or distribute
) instead of distutils
, but this is a very seamless "upgrade".
Here's a full (but untested) example:
from setuptools import setup, find_packages
setup(
name='your_project_name',
version='0.1',
description='A description.',
packages=find_packages(exclude=['ez_setup', 'tests', 'tests.*']),
package_data={'': ['license.txt']},
include_package_data=True,
install_requires=[],
)
Note the specific lines that are critical here:
package_data={'': ['license.txt']},
include_package_data=True,
package_data
is a dict
of package names (empty = all packages) to a list of patterns (can include globs). For example, if you want to only specify files within your package, you can do that too:
package_data={'yourpackage': ['*.txt', 'path/to/resources/*.txt']}
The solution here is definitely not to rename your non-py
files with a .py
extension.
See Ian Bicking's presentation for more info.
UPDATE: Another [Better] Approach
Another approach that works well if you just want to control the contents of the source distribution (sdist
) and have files outside of the package (e.g. top-level directory) is to add a MANIFEST.in
file. See the Python documentation for the format of this file.
Since writing this response, I have found that using MANIFEST.in
is typically a less frustrating approach to just make sure your source distribution (tar.gz
) has the files you need.
For example, if you wanted to include the requirements.txt
from top-level, recursively include the top-level "data" directory:
include requirements.txt
recursive-include data *
Nevertheless, in order for these files to be copied at install time to the package’s folder inside site-packages, you’ll need to supply include_package_data=True
to the setup()
function. See Adding Non-Code Files for more information.
How to include package data with setuptools/distutils?
I realize that this is an old question, but for people finding their way here via Google: package_data
is a low-down, dirty lie. It is only used when building binary packages (python setup.py bdist ...
) but not when building source packages (python setup.py sdist ...
). This is, of course, ridiculous -- one would expect that building a source distribution would result in a collection of files that could be sent to someone else to built the binary distribution.
In any case, using MANIFEST.in
will work both for binary and for source distributions.
Accessing static files included in a Python module
dir()
won't tell you anything about static files. The correct way (or one of them, at least) to get access to this data is with the resource_*
functions in pkg_resources
(part of setuptools), e.g.:
import pkg_resources
pkg_resource.resource_listdir('glm_plotter', 'templates')
# Returns a list of files in glm_plotter/templates
pkg_resource.resource_string('glm_plotter', 'templates/index.html')
# Returns the contents of glm_plotter/templates/index.html as a byte string
Setuptools - shipping additional files
You'll have to use a MANIFEST.in
file to list data files that you want included in a source distribution. package_data
is only consulted for binary distributions (a great shortcoming).
See The MANIFEST.in
template in the distutils documentation.
Related Topics
Index of Duplicates Items in a Python List
How to Override the [] Operator in Python
What Does a for Loop Within a List Do in Python
Unsupported Operand Type(S) for +: 'Int' and 'Str'
How to Save an Image Locally Using Python Whose Url Address I Already Know
Scrolling to Element Using Webdriver
Why Is Bubble Sort Implementation Looping Forever
How to Print One Character at a Time on One Line
How to Make File Creation an Atomic Operation
Matplotlib Fill Between Multiple Lines
Libxml Install Error Using Pip
How Does Condensed Distance Matrix Work? (Pdist)
Get Fully Qualified Class Name of an Object in Python
Python: How to Make the Ansi Escape Codes to Work Also in Windows