I haven't used RPy2 for a number of years (and then I only played around a bit). However, I now want to start using Python and R a bit more seriously and decided to re-explore the RPy2 library.
I'm using a Mac running El Capitan. I created a new virtual environment (called env34) using Python 3.4.4 and installed the latest version of pip (19.1.1) using get-pip.py. I then pip installed numpy, pandas and jupyter into the activated environment. I also pip installed RPy2 (but had to include a trusted-host flag):
(env34) $ pip install rpy2 --trusted-host=https://pypi.org/simple/cffi
I also installed the latest version of R (3.6.0) in the default location in Applications folder and edited .bash_profile to create a RHOME environment variable by adding the following:
RHOME="/Library/Frameworks/R.framework/Resources"
export RHOME
Now when I launch Python in the virtual environment, I can import rpy2 with no problems. However, I'm trying to follow the tutorial given at http://heather.cs.ucdavis.edu/~matloff/rpy2.html and when I try to import rpy2.robjects using:
>>> from rpy2.robjects import r
I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/path_to_virtual_environment/env34/lib/python3.4/site-packages/rpy2/robjects/__init__.py", line 14, in <module>
import rpy2.rinterface as rinterface
File "/path_to_virtual_environment/env34/lib/python3.4/site-packages/rpy2/rinterface.py", line 5, in <module>
import rpy2.rinterface_lib._rinterface_capi as _rinterface
File "/path_to_virtual_environment/env34/lib/python3.4/site-packages/rpy2/rinterface_lib/_rinterface_capi.py", line 8, in <module>
from . import conversion
File "/path_to_virtual_environment/env34/lib/python3.4/site-packages/rpy2/rinterface_lib/conversion.py", line 8, in <module>
from . import _rinterface_capi as _rinterface
ImportError: cannot import name '_rinterface_capi'
As far as I can see, all the files referenced in the message exist in the correct locations.
Can anyone interpret this error message and perhaps offer a workaround?
Thanks in advance.
Related
I am having trouble installing the cdstoolbox-remote on my Linux machine (tried both Ubuntu and Debian). I installed it using pip:
pip install cdstoolbox-remote
Which successfully installs the module, but I can't import the module in Python. Importing the module causes the following error:
>>> import cdstoolbox
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/an/miniconda2/envs/tools/lib/python3.7/site-packages/cdstoolbox/__init__.py",
line 8, in <module>
with open(__main__.__file__) as f:
AttributeError: module '__main__' has no attribute '__file__'
What can be done to get it working?
Do you really need cdstoolbox-remote? The standard API for downloading data from CDS would be cdsapi, which can be installed using
pip install cdsapi
or using conda (https://anaconda.org/conda-forge/cdsapi)
I also just tried installing cdstoolbox-remote and got the same problem. But as it is flagged as experimental I even consider it to be possible that the current version is not a stable version.
And cdsapi seems to work.
Actually, the CDSAPI can also be used to execute workflows as follows:
import cdsapi
c = cdsapi.Client()
with open("workflow.py") as f:
code = f.read()
r = c.workflow(code)
print(c.download(r))
For more, read this thread on the Copernicus services documentation.
I have installed PyCharm in order to work on Python projects. I have also installed Anaconda to create conda environments for my projects on my machine.
I have create a new project called 'hello-world-ml' and I have created a conda environment called 'hello-world-ml' for my python project.
Within, my python folder I have a script called 'hello-world.py'. Within this script I require the module 'sklearn'. I have activated my conda environment and installed sklearn using the command conda install scikit-learn. When I enter conda list. I can see that the module has been successfully installed within the conda environment.
I have also configure PyCharm to use the Python interpreter from the conda environment. So when I went to type from sklearn... it auto suggested what to import.
When I go to run my script, I get this error:
Traceback (most recent call last):
File "hello-world.py", line 1, in <module>
from sklearn import tree
File "C:\Users\Callu\Anaconda3\envs\hello-world-ml\lib\site-packages\sklearn\__init__.py", line 134, in <module>
from .base import clone
File "C:\Users\Callu\Anaconda3\envs\hello-world-ml\lib\site-packages\sklearn\base.py", line 11, in <module>
from scipy import sparse
File "C:\Users\Callu\AppData\Roaming\Python\Python36\site-packages\scipy\__init__.py", line 118, in <module>
from scipy._lib._ccallback import LowLevelCallable
File "C:\Users\Callu\AppData\Roaming\Python\Python36\site-packages\scipy\_lib\_ccallback.py", line 1, in <module>
from . import _ccallback_c
ImportError: cannot import name '_ccallback_c'
I am unsure how to fix this error. Any help would be greatly appreciated.
In addition, when I deactivate the conda environment and try to run the script it works fine.
In Windows, I created a Conda virtual environment with the command
conda create -n test python=2.7 pandas scipy matplotlib numpy
Once it is created, I activated the virtual environment and went into a python interpreter. When trying to import numpy, I get the following error:
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda3\envs\test\lib\site-packages\numpy\__init__.py", line 180, in <module>
from . import add_newdocs
File "C:\Anaconda3\envs\test\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Anaconda3\envs\test\lib\site-packages\numpy\lib\__init__.py", line 8,
in <module>
from .type_check import *
File "C:\Anaconda3\envs\test\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "C:\Anaconda3\envs\test\lib\site-packages\numpy\core\__init__.py", line 14, in <module>
from . import multiarray
ImportError: DLL load failed: The specified module could not be found.
Any ideas what is going on here? Thanks!
Uninstall and install numpy again.
pip uninstall numpy
pip install numpy
Then try import again, it should work. That is what I did
Unlike #Rafael, for me, libiomp5md.dll wasn't the issue. I installed Dependency Walker to investigate what was going on. Even though the dll versions were different, but Dependency Walker said it was okay.
What was wrong though, was that mkl_intel_thread.dll had warnings (red icon). If you're using Win 8++, ignore the api-win and ext-ms issues as Dependency Walker wasn't updated for new Windows versions and doesn't recognise Windows new APIs.
My solution is to copy all mkl_*.dlls from the former to the latter:
\Anaconda2\Library\bin
\Anaconda2\Lib\site-packages\numpy\core
I was able to import numpy and sklearn after that.
It seems the proper way to fix this is to do:
conda install msvc_runtime
If you are in a virtual environment, add this package there.
For my case, I have also the import numpy DLL error in anaconda3, and Decrayer (Nov 28) provided the best solution: I had the same error except for Anaconda3: just added the path \Anaconda3\Library\bin to your Windows 10 path variable and then it worked.
I have been trying to install anaconda3.7.1 or 3.7.0 without luck, going to full length of restarting the PC after the key steps to make sure the DLL is no longer in use in memory. Ultimately, thanks to decrayer, now numpy works for me!
For me this was solved by adding the following paths to my system path variable.
C:\Users\UserName\Anaconda3\
C:\Users\UserName\Anaconda3\bin
C:\Users\UserName\Anaconda3\Scripts
C:\Users\UserName\Anaconda3\Library\mingw-w64\bin (not strictly necessary)
C:\Users\UserName\Anaconda3\Library\bin
Before adding them explicitly in the system path variable, you can perform a test by adding these paths temporarily:
base_path = r"C:\Users\UserName\Anaconda3"
path = os.pathsep.join([os.path.join(base_path, i) for i in [r"", r"bin", r"Scripts", r"Library\mingw-w64\bin", r"Library\bin"]])
os.environ["PATH"]+=os.pathsep+path
Thanks to this post on PyCharm support.
For the record, I had the same error here (Python 3.5 64-bit on Windows 10), and this page helped me find the solution. The problem was a conflict with libiomp5md.dll, which existed on two locations:
C:\Windows\System32\libiomp5md.dll
C:\Anaconda3\Library\bin\libiomp5md.dll
Python was trying to use the version in System32 folder, which was an old version. I removed it (renamed) and now it uses the correct version, on Anaconda3 folder, and now I can import numpy without the import error.
I'm trying to learn to use Python for scientific analyses (in particular of large NETCDF files), and have installed Anaconda on my MacBookPro OSX10.10. I'm trying to use the Spyder IDE interface, but am stalled by errors with getting the netCDF4 package to work properly.
I need to use the netCDF4 package which I installed using:
conda install netcdf4
First I couldn't get any of the Anaconda packages to import in either Spyder or Python run directly in the terminal, but tried running:
export PATH=~/anaconda/bin:$PATH
Now I get all Anaconda pre-installed packages to work in Spyder (no error when I try the import command), EXCEPT for netCDF 4 which still comes up with the following error:
import netCDF4
Traceback (most recent call last):
File "<ipython-input-7-f731da2de255>", line 1, in <module>
import netCDF4
File "/Users/eriko/anaconda/lib/python2.7/site-packages/netCDF4/__init__.py", line 6, in <module>
from ._netCDF4 import (__version__, __netcdf4libversion__, __hdf5libversion__,
ImportError: cannot import name __netcdf4libversion__
However, netCDF4 imports without any error in the terminal version of Python?
The 'Code Analysis' in Spyder flags the following error in scripts where I added the import netCDF4 command:
'netCDF4' imported but unused
E402 module level import not at top of the file
W292 no newline at the end of file
I've spent too many hour Googling without finding a solution to this, so I hope the Stackoverflow community can help.
I am trying to use ryp with my arcpy scripts but I have the following error:
import rpy2.robjects as robjects Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module> import rpy2.robjects as robjects
File "C:\Python26\ArcGIS10.0\lib\site-packages\rpy2\robjects\__init__.py", line 12, in <module> import rpy2.rinterface as rinterface
File "C:\Python26\ArcGIS10.0\lib\site-packages\rpy2\rinterface\__init__.py", line 39, in <module> import win32api
ImportError: No module named win32api
This error comes even after the installation of the pywin32 for my version of python.
I've noticed that this seems to be a common error that is usually solved with the installation of pywin32.
I also have a problem with the matplotlib installation, every time i try to use it (import matplotlib.pyplot as plt), python crashes...
Versions:
Python 2.6.6
matplotlib installation: matplotlib-1.1.0.win32-py2.6.exe
You will need to run these scripts with PROPER Python. It seems to me that the ArcPy distribution does not include the win32api module (It also does not exist from example in Python on Mac or Linux).
I would install PythonXY which includes R bindings, and see if your scripts run there. If they run there, then I (guess) I am correct, and ArcPy does not include these modules.
A nice BONUS of PythonXY is it's an excellent Python IDE (Spyder), but the real bonus is what the commenter above me said:
different compiler versions can cause hell of a lot of Problems.
So, in PythonXY you get a whole bundle compiled with the same compiler.
Let us know if these made your RPy script run.