So, I have a file called 'function.py' which includes the simple function:
def square(x):
return x*x
I have a second bit of code like this:
from test import square
print(square(2))
If I store the second bit of code in a python file and run it in the terminal it works and gives the expected answer.
However, if I add a Python chunk in a Rmarkdown document like this:
```{python}
from test import square
print(square(2))
```
I get the error:
"Traceback (most recent call last): File "/var/folders/g7/462tmml173nfzj0j8437t9_m0000gn/T/RtmptMA22N/chunk-code-48764cec023f.txt", line 1, in from test import square ImportError: cannot import name square"
The Rmarkdown file and the python file are in the same directory. Answers about the specific error message are about dependencies, but I don't see how that's relevant in my case?
I've searched the web and read documentation, but I think I am missing something important. Thank you for the help!
Edit: Solved by specifically changing the path to the current working directory.
import sys, os
sys.path.append(os.getcwd())
import test
print(test.square(2))