When I try to import a python file as a module, I get an error.
I set up the example here https://github.com/AnthonyEbert/reticulate_module
What I get when I run the example.R file is
> library(reticulate)
> use_python("~/../../usr/bin/python3")
> py_config()
python: /home/ebertac/../../usr/bin/python3
libpython: /usr/lib/python3.5/config-3.5m-x86_64-linux-gnu/libpython3.5.so
pythonhome: /usr:/usr
version: 3.5.3 (default, Jan 19 2017, 14:11:04) [GCC 6.3.0 20170118]
numpy: /home/ebertac/.local/lib/python3.5/site-packages/numpy
numpy_version: 1.13.1
python versions found:
/home/ebertac/../../usr/bin/python3
/usr/bin/python
/usr/bin/python3
> py_run_file("dummy.py")
Error in py_run_file_impl(file, local, convert) :
ImportError: No module named 'important_module'
Detailed traceback:
File "<string>", line 2, in <module>
This works in the terminal.
> python3 dummy.py
I've gotten used to treating import in python in a similar way to source in R. Perhaps this isn't the correct way to do things? I'm sure that python in reticulate is using the correct working directory as I've checked with os$listdir().
Thank you
We have the reticulate::import_from_path function that can do this.
@jjallaire Is there a way to make this importing of a python file work in Rmarkdown as well?
It should indeed work in R Markdown. Note that you can also now use the source_python function for this sort of thing
Sorry I wasn't being clear. I meant how to import within a Rmarkdown's {python} chunk.
Current behavior: I first run import sys ; sys.path.append("."), only then can I import the python file in my working directory.
Expected behavior: I expect the current working directory to be included in the module search path automatically.
EDIT: great work on reticulate btw! This is very minor nit picking from me.
If we just executed sys.path.append(".") by default whenever reticulate initializes Python would this always work (i.e. is the . treated as a permanent alias or is it expanded immediately). Do you know how the default python interpreter shell handles this?
To be robust, I have always seen Python users recommend using (SO link)
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__)))
My understanding is that the os.path module makes sure that the path is constructed properly regardless of the OS being used.
I'm not so sure about injecting this code automatically before executing python chunks, as it's possible that chunks in many different Rmds could be executed in a single session (thus polluting the sys.path considerably). Even though it's a bit of boilerplate we might be better off having the user control this explicitly with code like what you suggest above.
reticulate::import_from_path did not work for me. sys.path.append(".") I agree with @anhqle that ideally this would be default behavior. If that's not possible, it would be helpful to document his solution in a vignette somewhere.
Most helpful comment
We have the
reticulate::import_from_pathfunction that can do this.