How to import r-packages in Python - python

I'm a bit troubled with a simple thing. I was trying to install a package called hunspell, but I discovered it is originally an R package. I installed this version: https://anaconda.org/conda-forge/r-hunspell, but I'm not being able to import it. Is this package supposed to work with Python? Should I use rpy2 to import it? First time using cross-platform packages so I'm a bit confused.
Just to be clear, import hunspell brings ModuleNotFoundError: No module named 'hunspell' and import r-hunspell brings SyntaxError: invalid syntax.
I also noticed that this package, also installed an r-base package, but I'm also not sure how to import that.

After running in the command line:
pip install rpy2
or with the "!" if you're in a Jupyter Notebook. The following procedure will answer your issue, based on the official documentation:
# Using R inside python
import rpy2
import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector
from rpy2.robjects.packages import importr
utils = rpackages.importr('utils')
utils.chooseCRANmirror(ind=1)
# Install packages
packnames = ('hunspell', 'some other desired packages')
utils.install_packages(StrVector(packnames))
# Load packages
hunspell = importr('hunspell')
If you want to access specific functions in this module you could check out these answer or that answer too.

Related

Is there a python package - import mapping available?

When I run a python code and it imports a library that is missing an error is printed out that this library cannot be imported, for example
import cv2
import PIL
for order to install these two example packages, however, you have to install them as follows
pip install opencv-python
pip install pillow
So the names of the import and the package do not match.
Is there a central database/file etc. somewhere that contains the name of the package given the name of the import?

Check if more imports are installed and install missing only python

I wonder if there is a way to check if all modules I am importing in the script are already installed and if some are not installed to install it.
For example,
import encodings
import json as json
import pandas as pd
import openpyxl
if openpyxl is the only which is missing, install it only.
I know the way with requirements.txt, but that is not an option in this specific case.
I tried with try/catch, but it is only writing the message about missing package. I am not sure how to install package from the message.

Cannot import umap: cannot import name 'structref' from 'numba.experimental'

I tried to import umap in my jupyter notebook but had the following error:
ImportError: cannot import name 'structref' from 'numba.experimental' (C:\Users\name\Anaconda3\lib\site-packages\numba\experimental\__init__.py)
I tried to update conda but doesn't work. What can I do ?
The numba.experimental subpackage was added in version 0.51.0. You can check your version of number using:
import numba
numba.__version__
If it is less then 0.51.0, you will need to install a newer version.
conda install numba=0.51.*

ImportError: No module named scenedetect on Linux Ubuntu

I'm trying to use pyscenedetect library on python for videos but I get this error when using the python interface and when I use the command line interface I get the error "ModuleNotFoundError: No module named 'cv2'"
even though I believe I installed both correctly according to the documentations.
I have trying to look for different ways to import opencv for the second error but to no avail. As for the first error i can't find any answers to my problem.
import cv2
import numpy as numpy
import os
import scenedetect
from scenedetect.video_manager import VideoManager
from scenedetect.scene_manager import SceneManager
from scenedetect.frame_timecode import FrameTimecode
from scenedetect.stats_manager import StatsManager
from scenedetect.detectors import ContentDetector
If you have pip you can try
pip install opencv-python
If you have anaconoda, you can try
conda install -c conda-forge opencv
it's probable that you installed it on a different python installation in your PC.
To know where your python installation is you can launch python and:
import sys
sys.path
To get the list of everything you have installed you can:
pip freeze > installed_modules.txt
Try only running
import cv2
So that you can test it
I found the problem. As Ivan was pointing out, the problem was with openCV.
I used the following command:
sudo apt install python3-opencv

Import non Standard R packages lme4 into Python

According to documentation http://rpy2.readthedocs.io/en/version_2.8.x/robjects_oop.html it shows how to import R packages that is not standard in python. To my luck they do the example I need which is lme4
import rpy2.robjects as ro
from rpy2.robjects import FloatVector
from rpy2.robjects.packages import importr
import rpy2.rinterface as rinterface
stats = importr('stats')
base = importr('base')
lme4 = importr('lme4')
getmethod = ro.baseenv.get("getMethod")
StrVector = ro.StrVector
No matter what I did I got the error
RRuntimeError: Error in loadNamespace(name) : there is no package called 'lme4'
I'm in windows environment and I know that this package is installed under
"C:/Users/me/Documents/R/win-library/3.4" not the standard "C:/Program Files/R/R-3.4.3/library"
Please any help is greatly appreciated.
Note that the error message is coming from the R kernel (RRuntimeError). This suggests that the R kernel can't find the package lme4. I think you have two options here:
Launch the R kernel and install lme4 there (install.packages('lme4'))
IF you used pip/conda to install rpy2, it should have also installed R in your environment (you can confirm via pip freeze or conda list). In this case, you can use pip/conda to install lme4 via the package r-lme (conda install r-lme)

Categories