How to use raster package in python with rpy2


Working with R in Python using rpy2 on windows 7. I need to open some rasters as RasterLayer using the function raster() from the raster package. I manage to install the package, but not to use its function.

I install the packages that I need (rgdal, sp, raster, lidR, io) using


names_to_install is a list of the packages that are still not installed. This works fine.

I know how to try the "basic" functions, like sum, and it works:

import rpy2.robjects as robjects
function_sum = robjects.r['sum']

But the same doesn't seem to work with the raster function from the raster package:

function_raster = robjects.r['raster']

since I get the error:

LookupError: 'raster' not found

I also tried the following:

raster_package = importr('raster')

with the intention to be able to run the next and load my raster file:


but the first line (import('raster')) causes the crash of python and I get the error:

Process finished with exit code -1073741819 (0xC0000005)

This doesn't happen with other loaded packages like rgdal, but with the raster package and with the lidR package I get the error.

I looked up this error, seems to be access violation, but I don't know what I can do about it and why it only happens with certain packages.

I expect to be able to call the raster function from the package raster.


I tried it on a computer with windows 10 and the error doesn't show anymore when running

raster_package = importr('raster')

Still would be nice to know what is the problem with Windows 7 and if there is any solution.

asked on Stack Overflow Oct 28, 2019 by anplaceb • edited Oct 28, 2019 by anplaceb

1 Answer


rpy2 does not currently have Windows support. This is not a final situation, most of what is likely needed is contributions to finalize this:

answered on Stack Overflow Nov 2, 2019 by lgautier

User contributions licensed under CC BY-SA 3.0