RStudio Import a python custom module into working envoirnment

Hi,

I have a python project that has the following setup

Folder
       --> main.py
       --> helper.py

I open up a new RStudio session and open the two files

# main.py

import pandas as pd
import janitor as jt
import helper as hl

# helper.py

def small_func():
  print("hello world")
  

When i run the main.py file i get the following


reticulate::repl_python()
Python 3.10.12 (C:/Users/user1/Miniconda3/envs/test-env/python.exe)
Reticulate 1.26 REPL -- A Python interpreter in R.
Enter 'exit' or 'quit' to exit the REPL and return to R.
>>> import helper as hl
ModuleNotFoundError: No module named 'helper'
>>> import pandas as pd
>>> import janitor as jt
>>> import helper as hl
ModuleNotFoundError: No module named 'helper'

Do i have to do something else in RStudio so my helper file will be seen?

Thanks

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

You have to place the directory with the Python files in Python sys.path for them to be visible as "modules" by Python. This applies regardless of whether you're using Python through reticulate or directly. E.g,

dir_with_py_files <- normalizePath(".")
sys <- import("sys", convert = FALSE)
sys$path$insert(0L, dir_with_py_files)
my_helper_module <- import("helper")