Packaging supporting R code in a python module?

后端 未结 3 1155
天命终不由人
天命终不由人 2021-02-02 17:45

I am trying to package some of my Python code that calls R code using rpy2. That R code currently sits in a separate file which I source from the Python script. For

3条回答
  •  抹茶落季
    2021-02-02 18:28

    Well, imagine yourself as the setuptools packager and think of what you would expect the programmer to do.

    • Setuptools knows nothing about R, its files' structure or that your code uses them somehow.
    • Your R interpreter knows nothing about importing files from Python .egg's

    For the first problem, you have two choices:

    1. Tell setuptools to just include some additional files without bothering what they are
    2. Teach setuptools about R, how to determine what R files your program uses and how to track and include their dependencies

    The first option is implementable by passing include_package_data = True to setup() and providing masks of files to include in package_data (setuptools docs, "Including Data Files" section). Paths relative to packages' directories can be used. The files will be accessible at run time at the same relative paths through the "Resource Management API" ("Accessing Data Files at Runtime" section).

    The second option would require you to add your code to setuptools before invoking setup(). For example, you may add a file finder to add relevant .R files to the results of find_packages(). Or just generate the list of files for the previous paragraph by arbitrary means.

    For the second problem, the easiest way is to force setuptools to install the package as a directory rather than an .egg by specifying zip_safe = False. You might use eager_resources option instead that extracts a group of resources on demand ("Automatic Resource Extraction" section).

    As for installing third-party R packages, an automatable technique is described at R Installation and Administration - Installing packages

提交回复
热议问题