Reticulate fails automatic configuration in R package - python

I'm working on a R package, that makes use of reticulate to call some functions of a Python package I implemented, installable through pip.
Following its documentation, I setup the reticulate automatic configuration of Python dependencies as follows, in the DESCRIPTION file of my package:
Config/reticulate:
list(
packages = list(
list(package="my_python_package", pip=TRUE)
)
)
where my_python_package is the Python package I need to use.
If I install the package locally, where I have the required Python package already installed, everything works fine.
However, if I try to install and use the R package in an environment without the Python package already installed, I get the following error:
Error in py_module_import(module, convert = convert) :
ModuleNotFoundError: No module named 'my_python_package'
Detailed traceback:
File "/home/runner/work/_temp/Library/reticulate/python/rpytools/loader.py", line 39, in _import_hook
module = _import(
as if reticulate is not able to configure correctly the environment.
Also, the Python package should not be the problem, since when it is installed, I am able to import it and use its functions with no errors.
From the reticulate documentation it seems Config/reticulate: ... is all is needed, but maybe I am missing something.

I noticed this issue https://github.com/rstudio/reticulate/issues/997 on the reticulate GitHub repository. Apparently, the automatic configuration through Config/reticulate only works if a conda environment is already loaded.
Therefore, I think the only way to configure the correct environment is on the .onLoad function:
check if Anaconda is installed, otherwhise launch the Miniconda installation through reticulate::install_miniconda()
check if the environment is already present, otherwise create create it through reticulate::conda_create(envname),
install the Python dependencies if necessary through reticulate::conda_install(envname, packages),
load the configured environment.
In this way, the first time the package is loaded, the environment will be correctly created.
After that, the environment will be automatically loaded.

Related

Anki python scripting: Multiple modules missing

I'm trying to follow the tutorial at https://www.juliensobczak.com/tell/2016/12/26/anki-scripting.html
And I'm getting the "listcards.py" basic script set up, having cloned anki and installed the virtual environment as well as the requirements from the anki/requirements.txt file.
However, when I run the script from the tutorial entitled "listcards.py", I get a notice that the module 'anki.sched' is not found. ("ModuleNotFoundError: No module named 'anki.sched')
While I could pip install each package, I have a feeling that there must be an underlying reason that these packages are missing- is there a way to have python automatically pull in the named module even if it isn't pre-installed in the manner of how node.js installed referenced dependencies automatically, so that I won't have to manually install every missing package?
I ran into this same problem. anki.sched is a package contained within the anki repository you cloned, so it does exist on your machine. You won't be able to install it using pip.
The solution for me was to write the absolute path of the anki repository you cloned in sys.path.append rather than a relative path. For example, if your script exists in /Users/anki/scripts and your cloned anki repository exists in /Users/anki/anki write this in your script before importing anki modules:
sys.path.append("/Users/anki/anki")
rather than this (which is what is provided in the tutorial):
sys.path.append("../anki")
I'm not 100% sure why the latter fails, but Anki must be looking for the anki.sched module in the wrong place due to the relative reference.
What I did, and I know this is probably not the correct way, is to simply wipe out the root anki folder and copy all the application scripts to it, then the imports worked.

Pip error when trying to install module

I'm trying to install PyDrive [a wrapper library of the google drive api for python] and pip is giving me this error. It did the same thing when trying to install things like matplotlib or mega.py [a mega.nz api for python].
Here's the error:
Anyone got a clue what's going on?
Cheers
You could try renaming that pip.py to something else.
There is a library called pip somewhere on your system (and it may also be bundled within pip.exe). That is different from the "entry point" script that actually runs pip from the command line. When you run pip, it will try to import the library called pip. If there is a script called pip.py in the Scripts directory (representing the entry-point script, not the library), it may import that instead of the real library. If this is indeed the problem, renaming pip.py to something else will remove the name conflict and allow pip to properly import the library it needs.
I'm not sure how you wound up with pip.py in your Scripts directory in the first place. I don't think it should be there. My Python installation on Windows doesn't have it.

Using MacPorts to install modules via a certain path

I realize this is a very newbie question, but previous threads haven't been encouraging (see details below).
I've been using anaconda for most of my work via the IPython Notebook, python 2.7
However, there is a module which I could only properly download with MacPorts. Hence the current problem: when I try to use the Python command line in Terminal or use IPython notebooks, I will enter
import py-module
and I get
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named py-module
So, obviously the $PATH is wrong. All my previous modules have been saved with conda to /anaconda/bin, but using sudo port install py-module is going to save in a different file, /opt/local.
Is it possible to set the path such that MacPorts will install a module into anaconda/bin
such that I can import this module on ipython notebook via conda?
Previous threads have suggested simply choosing one or the other, i.e. removing anaconda entirely
rm -r ~/anaconda
and then just reinstalling all packages via MacPort. But I would prefer not to do this. Can't I re-direct the path such that
sudo port install py-module
installs the module into `/anaconda/bin/' ?
For previous takes, see
Getting PyGame to import in Python interpreter after MacPorts install
While you could adjust your PYTHONPATH environment variable or modify the path using sys.path.insert, you should not try to mix a Python module built for MacPorts' Python with Anaconda. This may work just fine if the module is python-only, but as soon as the module contains compiled code it is not certain that the two installations are binary-compatible. In the best case, loading the module will fail if it isn't compatible, in the worst case, it will randomly crash.
So, yes, you should decide for one Python and just use that.

Usage of rpy2 with a custom installation of R

Dear fellow programmers,
I would like to use R inside of a ipython notebook. On the system (Centos) I am working on exists an older version of R (2.15). I was able to execute R-code with this version in a ipython notebook, however, I need a newer version of R with extra packages and because I don't have administrative right for that machine, I have a local machine on which I installed the newest version of R (3.1.2). I compiled the source on my pc and flagged it in a way that it can be assessed by extensions. After that I installed rpy2 with pip and this installation went without any errors.
I also can import the rpy2 package, but when I try to load libraries, I get an error message:
Error in dyn.load(file, DLLpath = DLLpath, ...) :
unable to load shared object '/my/homedir/Software/R-3.1.2/library/stats/libs/stats.so':
libRlapack.so: cannot open shared object file: No such file or directory
During startup - Warning message:
package \u2018stats\u2019 in options("defaultPackages") was not found
I looked it up, all libraries are present and I don't get an error when I use R without ipython.
Anyone has ideas?
Best,
Daniel
Have you tried this?
http://www.lfd.uci.edu/~gohlke/pythonlibs/#rpy2
It's very weird when you say you need admin right to update R version because R usually doesn't require admin right to be installed. Make sure you install that under your user folder, instead of somewhere in the system drive.

Best practice for upgrading Python modules

I've been learning Python for several months but now finding some problems with my 2.7 installation as I've looked into modules such as nltk.
However, when I want to list modules using help ("modules) I have the main error which I think explains the problem is:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/distribute-0.6.28-py2.7.egg/setuptools/command/install_scripts.py:3: UserWarning: Module numpy was already imported from /Library/Python/2.7/site-packages/numpy-override/numpy/__init__.pyc, but /Library/Python/2.7/site-packages/numpy-1.8.0.dev_5c944b9_20120828-py2.7-macosx-10.8-x86_64.egg is being added to sys.path
from pkg_resources import Distribution, PathMetadata, ensure_directory
I also receive the following error to do with deprecated modules:
/Library/Python/2.7/site-packages/statsmodels-0.5.0-py2.7-macosx-10.8-intel.egg/scikits/statsmodels/__init__.py:2: UserWarning: scikits.statsmodels namespace is deprecated and will be removed in 0.5, please use statsmodels instead
I'm still trying to get to grips with paths. How can I avoid this issue in future?
You have installed packages under your operating system Python library. This is big no no. What you should have done is to create an isolated, disposable, Python environment with virtualenv tool:
http://opensourcehacker.com/2012/09/16/recommended-way-for-sudo-free-installation-of-python-software-with-virtualenv/
This way, when you upgrade your packages or need to get rid of them you can always reset the state of all of your Python packages by simply deleting the environment and creating new one.
Python packages installed via pip or easy_install commands are easy to install, but impossible to uninstall...
But when the damage has already happened you nede to manually try to clean up /Library/Python/2.7/site-packages/ by deleting files by and trying not to destroy your system Python in the process.

Categories

Resources