[Python] Run external program inside a conda environment in R

Discussão em 'Python' iniciado por Stack, Abril 20, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to run stitchr in R. For programs that run in Python, I use reticulate. I create a conda environment named r-reticulate, where I want to install stitchr and run it.

    I try the following:

    if (!('r-reticulate' %in% reticulate::conda_list()[,1])){
    reticulate::conda_create(envname = 'r-reticulate', packages = 'python=3.10')
    }
    reticulate::use_condaenv('r-reticulate')
    reticulate::py_install("stitchr", pip = TRUE)

    system("stitchr -h") # this does not work


    But obviously enough, the system() call does not work, with the message error in running command.

    What would be the right way to do this?

    I had success in the past with anndata, for example. But this is an R package wrapper, so I can just do:

    reticulate::use_condaenv('r-reticulate')
    reticulate::py_install("anndata", pip = TRUE)

    data_h5ad <- anndata::read_h5ad("file.h5ad")


    How can I approach the stitchr case?

    EDIT:

    So I retrieved stitchr.py location during the package installation: /usr/local/Caskroom/miniconda/base/envs/r-reticulate/lib/python3.10/site-packages/Stitchr/stitchr.py

    I tried all the following but nothing works (see error messages):

    pyloc="/usr/local/Caskroom/miniconda/base/envs/r-reticulate/lib/python3.10/site-packages/Stitchr/stitchr.py"
    reticulate::source_python(pyloc)


    Error in py_run_file_impl(file, local, convert) : ImportError: attempted relative import with no known parent package Run reticulate::py_last_error() for details.
    ​

    reticulate::py_run_file(pyloc)


    Error in py_run_file_impl(file, local, convert) : ImportError: attempted relative import with no known parent package Run reticulate::py_last_error() for details.
    ​

    reticulate::py_run_string(paste(pyloc, "-h"))


    Error in py_run_string_impl(code, local, convert) : File "", line 1 /usr/local/Caskroom/miniconda/base/envs/r-reticulate/lib/python3.10/site-packages/Stitchr/stitchr.py -h SyntaxError: invalid syntax Run reticulate::py_last_error() for details.
    ​

    I am absolutely clueless on how to proceed here.

    Continue reading...

Compartilhe esta Página