title: How to use a Python function defined in a separate file in a Python chunk in Rmarkdown?
body: <p>So, I have a file called 'function.py' which includes the simple function:</p>
  
  <pre><code>def square(x):
    return x*x
  </code></pre>
  
  <p>I have a second bit of code like this:</p>
  
  <pre><code>from test import square
  print(square(2))
  </code></pre>
  
  <p>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.</p>
  
  <p>However, if I add a Python chunk in a Rmarkdown document like this:</p>
  
  <pre><code>```{python}
  from test import square
  print(square(2))
  ```
  </code></pre>
  
  <p>I get the error: </p>
  
  <blockquote>
    <p>"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"</p>
  </blockquote>
  
  <p>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? </p>
  
  <p>I've searched the web and read documentation, but I think I am missing something important. Thank you for the help!</p>
  
  <p><strong>Edit:</strong>
  Solved by specifically changing the path to the current working directory.</p>
  
  <pre><code>import sys, os
  sys.path.append(os.getcwd())
  import test
  print(test.square(2))
  </code></pre>