How to use a Python function defined in a separate file in a Python chunk in Rmarkdown?










0















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))









share|improve this question
























  • Have you read and tried the solutions detailed in this GitHub issue thread?

    – Mihai Chelaru
    Nov 14 '18 at 13:08











  • Is test a folder ?

    – Hcetipe
    Nov 14 '18 at 13:11











  • 1) The GitHub issue thread: yes, I have read this. The reticulate function that is given as solution is meant to be used in a R chunk, whereas I want to import within a Python chunk. The other solution of using "sys.path.append(os.path.join(os.path.dirname(file)))" is not quite clear to me; not sure how to implement it. 2) test is not a folder, test.py is a file with the function

    – D. de Jonge
    Nov 14 '18 at 13:39












  • You can try to put your test.py in a folder and then import like this : from yourfolder.test import *

    – Hcetipe
    Nov 14 '18 at 13:47












  • This solution does not work unfortunately. Am I missing something about what a module is exactly? Or am I missing something about how to define the location of the directory/file/function?

    – D. de Jonge
    Nov 14 '18 at 13:58















0















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))









share|improve this question
























  • Have you read and tried the solutions detailed in this GitHub issue thread?

    – Mihai Chelaru
    Nov 14 '18 at 13:08











  • Is test a folder ?

    – Hcetipe
    Nov 14 '18 at 13:11











  • 1) The GitHub issue thread: yes, I have read this. The reticulate function that is given as solution is meant to be used in a R chunk, whereas I want to import within a Python chunk. The other solution of using "sys.path.append(os.path.join(os.path.dirname(file)))" is not quite clear to me; not sure how to implement it. 2) test is not a folder, test.py is a file with the function

    – D. de Jonge
    Nov 14 '18 at 13:39












  • You can try to put your test.py in a folder and then import like this : from yourfolder.test import *

    – Hcetipe
    Nov 14 '18 at 13:47












  • This solution does not work unfortunately. Am I missing something about what a module is exactly? Or am I missing something about how to define the location of the directory/file/function?

    – D. de Jonge
    Nov 14 '18 at 13:58













0












0








0








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))









share|improve this question
















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))






python r-markdown






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 14:13







D. de Jonge

















asked Nov 14 '18 at 13:04









D. de JongeD. de Jonge

237




237












  • Have you read and tried the solutions detailed in this GitHub issue thread?

    – Mihai Chelaru
    Nov 14 '18 at 13:08











  • Is test a folder ?

    – Hcetipe
    Nov 14 '18 at 13:11











  • 1) The GitHub issue thread: yes, I have read this. The reticulate function that is given as solution is meant to be used in a R chunk, whereas I want to import within a Python chunk. The other solution of using "sys.path.append(os.path.join(os.path.dirname(file)))" is not quite clear to me; not sure how to implement it. 2) test is not a folder, test.py is a file with the function

    – D. de Jonge
    Nov 14 '18 at 13:39












  • You can try to put your test.py in a folder and then import like this : from yourfolder.test import *

    – Hcetipe
    Nov 14 '18 at 13:47












  • This solution does not work unfortunately. Am I missing something about what a module is exactly? Or am I missing something about how to define the location of the directory/file/function?

    – D. de Jonge
    Nov 14 '18 at 13:58

















  • Have you read and tried the solutions detailed in this GitHub issue thread?

    – Mihai Chelaru
    Nov 14 '18 at 13:08











  • Is test a folder ?

    – Hcetipe
    Nov 14 '18 at 13:11











  • 1) The GitHub issue thread: yes, I have read this. The reticulate function that is given as solution is meant to be used in a R chunk, whereas I want to import within a Python chunk. The other solution of using "sys.path.append(os.path.join(os.path.dirname(file)))" is not quite clear to me; not sure how to implement it. 2) test is not a folder, test.py is a file with the function

    – D. de Jonge
    Nov 14 '18 at 13:39












  • You can try to put your test.py in a folder and then import like this : from yourfolder.test import *

    – Hcetipe
    Nov 14 '18 at 13:47












  • This solution does not work unfortunately. Am I missing something about what a module is exactly? Or am I missing something about how to define the location of the directory/file/function?

    – D. de Jonge
    Nov 14 '18 at 13:58
















Have you read and tried the solutions detailed in this GitHub issue thread?

– Mihai Chelaru
Nov 14 '18 at 13:08





Have you read and tried the solutions detailed in this GitHub issue thread?

– Mihai Chelaru
Nov 14 '18 at 13:08













Is test a folder ?

– Hcetipe
Nov 14 '18 at 13:11





Is test a folder ?

– Hcetipe
Nov 14 '18 at 13:11













1) The GitHub issue thread: yes, I have read this. The reticulate function that is given as solution is meant to be used in a R chunk, whereas I want to import within a Python chunk. The other solution of using "sys.path.append(os.path.join(os.path.dirname(file)))" is not quite clear to me; not sure how to implement it. 2) test is not a folder, test.py is a file with the function

– D. de Jonge
Nov 14 '18 at 13:39






1) The GitHub issue thread: yes, I have read this. The reticulate function that is given as solution is meant to be used in a R chunk, whereas I want to import within a Python chunk. The other solution of using "sys.path.append(os.path.join(os.path.dirname(file)))" is not quite clear to me; not sure how to implement it. 2) test is not a folder, test.py is a file with the function

– D. de Jonge
Nov 14 '18 at 13:39














You can try to put your test.py in a folder and then import like this : from yourfolder.test import *

– Hcetipe
Nov 14 '18 at 13:47






You can try to put your test.py in a folder and then import like this : from yourfolder.test import *

– Hcetipe
Nov 14 '18 at 13:47














This solution does not work unfortunately. Am I missing something about what a module is exactly? Or am I missing something about how to define the location of the directory/file/function?

– D. de Jonge
Nov 14 '18 at 13:58





This solution does not work unfortunately. Am I missing something about what a module is exactly? Or am I missing something about how to define the location of the directory/file/function?

– D. de Jonge
Nov 14 '18 at 13:58












1 Answer
1






active

oldest

votes


















0














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))


Answer was based on Python: Best way to add to sys.path relative to the current running script.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300930%2fhow-to-use-a-python-function-defined-in-a-separate-file-in-a-python-chunk-in-rma%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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))


    Answer was based on Python: Best way to add to sys.path relative to the current running script.






    share|improve this answer



























      0














      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))


      Answer was based on Python: Best way to add to sys.path relative to the current running script.






      share|improve this answer

























        0












        0








        0







        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))


        Answer was based on Python: Best way to add to sys.path relative to the current running script.






        share|improve this answer













        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))


        Answer was based on Python: Best way to add to sys.path relative to the current running script.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 14:13









        D. de JongeD. de Jonge

        237




        237





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300930%2fhow-to-use-a-python-function-defined-in-a-separate-file-in-a-python-chunk-in-rma%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo