I've run into a rather strange error when doing unit testing with the numpy.testing module. I'm running an iPython notebook in a VM. In my code, I have one test where I compare my output to that in R. This requires me to load the rpy2 modules like so:
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
fastclime = importr('fastclime')
grdevices = importr('grDevices')
However when I run ! py.test, I get the following error:
==================================== ERRORS ====================================
_____________________ ERROR collecting test_fastclime_R.py _____________________
test_fastclime_R.py:6: in <module>
import rpy2.robjects as robjects
../../anaconda/lib/python2.7/site-packages/rpy2/robjects/__init__.py:15: in <module>
import rpy2.rinterface as rinterface
../../anaconda/lib/python2.7/site-packages/rpy2/rinterface/__init__.py:101: in <module>
from rpy2.rinterface._rinterface import *
E ImportError: /home/bitnami/anaconda/bin/../lib/libreadline.so.6: undefined symbol: PC
====================== 10 passed, 1 error in 0.19 seconds ======================
I suspect that this has something to do with some environment variable not being linked to my working directory, but I have no idea how to fix it. Any suggestions are greatly appreciated! Thank you!
The solution was to
1) delete __pycache__ directory if a previous version was created
2) Install readline in anaconda in the command line:
conda install -c asmeurer readline
3) Inside the .py file include import readline
another solution is to remove conda's readline from the environment and to use pip's one:
conda remove --force readline
pip install readline
Related
I am running a Python package called pymatgen in Jupyter. Jupyter and pymatgen are installed in a conda environment. I have manually installed ruamel using conda's python3, but the same error occurs. The strange thing is that the same code (in this case just a library load statement) gives no errors when run with ipython. Both Jupyter and ipython are running from the miniconda installation (I check with the command which). I noticed some postings on the web stating that ruamel has problems with conda due to the remapping of a "." to an underscore character, but none of the proposed solutions helped the above problem. I also don't understand why ipython is fine and Jupyter fails. Any suggestions?
from pymatgen.core.structure import Structure, Lattice
from pymatgen.core.periodic_table import Element
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-753da7cc5963> in <module>
----> 1 from pymatgen.core.structure import Structure, Lattice
2 from pymatgen.core.periodic_table import Element
~/.local/lib/python3.7/site-packages/pymatgen/__init__.py in <module>
13 import os
14 import warnings
---> 15 import ruamel.yaml as yaml
16 from fnmatch import fnmatch
17
ModuleNotFoundError: No module named 'ruamel'
Use the below command to install it:
pip install ruamel.yaml
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.
When I run my tests in CI, I get the following error:
ImportError while importing test module '/home/tests/test_process.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
...
.tox/py27/lib/python2.7/site-packages/matplotlib/pyplot.py:31: in <module>
import matplotlib.colorbar
.tox/py27/lib/python2.7/site-packages/matplotlib/colorbar.py:36: in <module>
import matplotlib.contour as contour
.tox/py27/lib/python2.7/site-packages/matplotlib/contour.py:14: in <module>
import matplotlib._contour as _contour
E ImportError: numpy.core.multiarray failed to import
----- Captured stderr -----
ImportError: No module named _multiarray_umath
What's going on here? I haven't made any changes to my code, but all the sudden
my build started failing.
Solution
Install numpy using pip seperately, before installing your sdist.
For tox, add numpy directly to your deps array.
Why did this happen?
Numpy recently published numpy-1.16.0rc2 to pypy, which is what (in conjunction with a bug/oversight in easy_install) broke your build:
pip knows not to install RCs by default, but easy_install (which matplotlib uses to do their builds) does not. If you were to do sdist with a whole bunch of -vvvvvvs, you'd see something like this:
gcc ... -I/tmp/pip-install-Eh8d9d/matplotlib/.eggs/numpy-1.16.0rc2-py2.7-linux-x86_64.egg/numpy/core/include ... -o build/temp.linux-x86_64-2.7/src/_contour.o
In particular, note that matplotlib is being built against numpy-1.16.0rc2-py2.7. But then in another place you might see something like
Successfully installed ... numpy-1.15.4 ...
So then when you try and run your program, matplotlib will try to access modules that don't exist in the non-RC version of numpy, and fail.
If you already have numpy installed, easy_install won't try and fetch its own version, and will instead use the (correct) existing version.
See also
http://numpy-discussion.10968.n7.nabble.com/Issue-with-setup-requires-and-1-16-release-candidates-td46600.html
The solution is that you need to upgrade numpy.
If you are using pip
pip install numpy --upgrade
Hope it helps.
I tried to install statsmodels in python. After installation, I checked with pip freeze. The package can be seen in the list.
When I am trying:
from statsmodels.tsa.api import ExponentialSmoothing, SimpleExpSmoothing, Holt
I am getting error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name ExponentialSmoothing
I have tried the following link also :
link
As of today (10 May 2018), the problem is solved by simply installing version 0.9.0 rather than the default 0.8.0:
pip install statsmodels==0.9.0rc1
I met the same situation, and the install process recommended in Nish's url didn't work for me. Here's how did I solve the problem (I'm using Mac OS).
Remove statsmodels library first, if you have installed: pip uninstall statsmodels
In your terminal, type git init, to initiate git
Then type git clone git://github.com/statsmodels/statsmodels.git
Change the directory to statsmodels using “cd statsmodels”
Next type python setup.py install
python setup.py build_ext --inplace
Now type python in your terminal and then type from statsmodels.tsa.api import ExponentialSmoothing, to see whether it can import successfully
If using conda, this will make statsmodel 0.9.0
conda update statsmodels
It is the wrong import,
Try
from statsmodels.tsa.holtwinters import ExponentialSmoothing, SimpleExpSmoothing, Holt
You can follow the steps mentioned below:
Step 1: Remove statsmodel using pip uninstall statsmodel
Step 2: Install git from here: https://git-scm.com/downloads
Step 3: Follow steps mentioned under "Installing library(statsmodels)" from link mentioned below:
https://www.analyticsvidhya.com/blog/2018/02/time-series-forecasting-methods/?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+AnalyticsVidhya+%28Analytics+Vidhya%29
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)