Access Data in Package Subdirectory

Access data in package subdirectory

You can use __file__ to get the path to the package, like this:

import os
this_dir, this_filename = os.path.split(__file__)
DATA_PATH = os.path.join(this_dir, "data", "data.txt")
print open(DATA_PATH).read()

Accessing a sub directory in a R package

You should be able to modify this to suit your needs:

> packagePath <- find.package("survival", lib.loc=NULL, quiet = TRUE)
> packagePath
[1] "/Library/Frameworks/R.framework/Versions/3.0/Resources/library/survival"

> list.files(packagePath)
[1] "CITATION" "data" "DESCRIPTION" "doc" "help" "html"
[7] "INDEX" "libs" "Meta" "NAMESPACE" "NEWS.Rd" "R"
> list.files(paste0(packagePath, "/data") )
[1] "Rdata.rdb" "Rdata.rds" "Rdata.rdx"

If you need a further look at how system.file works, just type

system.file 

Python Data File Access both Locally and in Distributed Package

Putting packages in setup.py in the argument setup(..., data_files=[('', YOUR_DATA_FILES)],...) (the '' is the significant part) copies them in-place, and they can be accessed as they would otherwise be, both in local testing and when distributed.

How to properly include data folder to python package

You can simply specify the relative path to the data you want to include. You need to put an __init__.py-file in both subfolders though, but then it should work.

package_data={'my_pkg' :['my_pkg/resources/nltk_data/*']}

To use the data in your script, use importlib (for example importlib.read_text) to open your desired file.

Including Package Data Python from Top-level when Package is in Subdirectory

Based on your call pkg_resources.resource_listdir("esm_tools", "config"), I assume you want to remap configs to esm_tools.config in the installed package:

site-packages
├── esm_tools
│ ├── __init__.py
│ ├── config
│ │ ├── machines
│ │ ├── scope

This means you have to do the following things:

  1. Tell setuptools to include a subpackage esm_tools.config (even if it doesn't really exist in source code base and is technically a namespace one, we'll construct it via further configuration)
  2. Tell setuptools where the sources for the new esm_tools.config package are located (this is again just a necessary measure to tell setuptools what to include in the dist. The package itself won't provide any Python sources since no Python files are located in configs).
  3. Tell setuptools to include package data for esm_tools.config from the correct path.

Example:

setup(
...,
packages=['esm_tools', 'esm_tools.config'], # 1
package_dir={'esm_tools.config': 'configs'}, # 2
package_data={'esm_tools.config': ['../configs/*']}, # 3
)

Note that this won't work with editable installs (neither via pip install --editable . nor with python setup.py develop), so you will have to construct more or less ugly local workarounds with symlinks or .pth files or whatever. The wheel dist (built via python setup.py bdist_wheel or pip wheel) will work out of the box, for source dists you'll have to include the configs dir via MANIFEST.in as package_data won't be read at sdist time:

# MANIFEST.in
...
graft configs

Import a file from a subdirectory?

Take a look at the Packages documentation (Section 6.4).

In short, you need to put a blank file named

__init__.py

in the lib directory.



Related Topics



Leave a reply



Submit