The following code does not work. It seems that the R warning message raises a python error.
# enable use of python objects in rpy2
import rpy2.robjects.numpy2ri
import numpy as np
from rpy2.robjects import r
# create an example array
a = np.array([[5,2,5],[3,7,8]])
# this line leads to a warning message, which in turn raises an
# error message if run within a script.
result = r['chisq.test'](a)
Running that code example in ipython works, however, running it inside a script raises the errorTypeError: 'module' object is unsubscriptable. I assume this is due to the warning message.
What is the best way to avoid this problem?
Thanks in advance!
Put a print statement right before the error:
print(r)
result = r['chisq.test'](a)
The error message TypeError: 'module' object is unsubscriptable is claiming that r is referencing a module. When you run the script with the print statement, you'll see something like
<module 'rpy2' from '/usr/lib/python2.6/dist-packages/rpy2/__init__.pyc'>
Traceback (most recent call last):
File "/home/unutbu/pybin/test.py", line 14, in <module>
result = r['chisq.test'](a)
TypeError: 'module' object is unsubscriptable
Note that the first line says that r is referencing the module rpy2.
This should give you a clue as to what is going wrong. Once you find the name of the trouble-making module, check your import statements to see how r is getting reassigned to that module.
For example, if you have
from rpy2.robjects import r
...
import rpy2 as r
then the second import statement is overriding the first, and the name r is thereafter referencing the module rpy2 instead of rpy2.robjects.r.
Related
I've read the previous posts here on the subject problem, although not specifically for mechanicalsoup, which mostly indicated a possible conflict between the module and class name, but I don't see how that would apply here.
I created a .py text file with two lines:
Also tried from mechanicalsoup import mechanicalsoup which resulted in an error.
In case it is helpful, this is the output from print(mechanicalsoup):
module mechanicalsoup from
'C:\Users\peterk\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mechanicalsoup\__init__.py'
import mechanicalsoup
browser=mechanicalsoup.stateful_browser()
When run, the output is:
C:\Temp>bf.py
Traceback (most recent call last):
File "C:\Temp\bf.py", line 2, in
browser=mechanicalsoup.stateful_browser()
TypeError: 'module' object is not callable
Let me try to help you out. Firstly install latest mechanicalsoup by following this LINK
Then try this.
import mechanicalsoup
browser=mechanicalsoup.StatefulBrowser()
browser.open("http://httpbin.org/")
And if you want to use stateful_browser use it like
browser=mechanicalsoup.stateful_browser
But it has no attribute open which is actually your aim. That is why use the StatefulBrowser as mentioned in their documentation
I am using a feature selection algorithm called mRMRe in R , but I need to call it from Python. I have successfully installed the package and being able to call it from Python. I need to access some functions within the R mRMRe package like mRMR.data to convert the dataframe into a format as needed by the algo.
from rpy2.robjects.packages import importr
utils = importr('utils') #-- Only once.
utils.install_packages('mRMRe')
# Now we begin by loading in the R packages
pymRMR = importr('mRMRe')
pymRMR
Out[53]: rpy2.robjects.packages.Package as a <module 'mRMRe'>
However when I try to call it's function mRMR.data I get an error:
AttributeError: module 'mRMRe' has no attribute 'mRMR'
same is the case if I try to do with a different library:
datasets = importr('datasets')
datasets.data.fetch('mtcars')
Traceback (most recent call last):
File "<ipython-input-56-b036c6da58e1>", line 2, in <module>
datasets.data.fetch('mtcars')
AttributeError: module 'datasets' has no attribute 'data'
I got this datasets part from enter link description here
I am not sure what I am doing wrong. I earlier had imported as used R's medcouple function from mrfDepth as below:
import rpy2.robjects as ro
#now import the importr() method
from rpy2.robjects.packages import importr
utils = importr('utils') #-- Only once.
utils.install_packages('mrfDepth')
# Now we begin by loading in the R packages
mrfdepth = importr('mrfDepth')
mc = mrfdepth.medcouple(yr)[0]
return mc
Can someone please help me to resolve this?
You're only importing the base module, and need to import it entirely. You'd think Python would do that automatically, apparently it doesn't. See this SO answer.
from mRMRr import *
from datasets import *
Edit: Ah, yeah that applies to explicit python modules. I think the syntax of calling on functions of sub-packages is possibly different. Try this.
import rpy2.robjects.packages as packages
datasets = packages.importr('datasets')
mtcars = packages.data(datasets).fetch('mtcars')['mtcars']
I used to import some R packages ans use them inside my python code but recently I improvised a method where you can simply use your R code and give the required tasks to it. Have a look here https://stackoverflow.com/a/55900840/5350311 it can be useful for your case.
Sorry for the very silly question. I am a self-study beginner in python and I am having issues with using a function and calling it. I am coming from a MATLAB background so i was trying to do something similar.
Tools used: Python 2 in a Linux environment
As a test, I created a function that i called prthis (for "print this") within a file called also prthis.py. This function just takes a number as an input, and then outputs two numbers, respectively the same one and its square. I defined it like this:
#----------------------------------------
# content of the file prthis.py
#----------------------------------------
def prthis(x):
y=x*x
nb=x
return (y, nb)
#------------------------------------------
then, within the python prompt, I try to call the newly created prthis function, and I do this:
>>> import prthis
>>> g,t = prthis(7)
The import seems to be succesful, but when I try the function on two outputs variable called g and t , like above, i get the following error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
Perhaps I am too much MATLAB-izing my thinking. Does anyone has a suggestion on how to deal with this?
PS: it's my first question ever on stackexchange, so please could you let me know how to thank/accept valuable answers from other users? I do not wish to look like ungrateful to those who would try to help.
you are importing a module, not the function. If you want to import just the function you could do this:
from prthis import prthis
g,t = prthis(7)
but if you import the full module you have to define the module you are calling the function from as well:
import prthis
g,t = prthis.prthis(7)
you are successfully able to import prthis but this is not the correct way either you should try "from prthis import prthis. Refer this for a better understanding of calling a function.
What does it mean to "call" a function in Python?
I have compiled the aruco library as stated here github link for aurco library
I have checked it has compiled successfully as i can import it in python without any error and to check i have run the example.py script also it's working but when i wrote this code
import cv2
import numpy as np
import aruco
Dictionary = aruco.getPredefinedDictionary(aruco.PREDEFINED_DICTIONARY_NAME(DICT_5X5_250=6))
aruco.drawMarker(Dictionary,5,250,markerImage,1)
aruco.drawMarker(Dictionary,10,250,markerImage,1)
aruco.drawMarker(Dictionary,20,250,markerImage,1)
aruco.drawMarker(Dictionary,25,250,markerImage,1)
aruco.drawMarker(Dictionary,50,250,markerImage,1)
aruco.drawMarker(Dictionary,100,250,markerImage,1)
aruco.drawMarker(Dictionary,200,250,markerImage,1)
cv2.imshow("markers",markerImage)
cv2.waitKey(0)
cv2.imgwrite(marker.jpg,markerImage)
it throws error
Traceback (most recent call last): File "drawmarker.py", line 7, in
Dictionary = aruco.getPredefinedDictionary(aruco.PREDEFINED_DICTIONARY_NAME(DICT_5X5_250=6))
AttributeError: 'module' object has no attribute
'getPredefinedDictionary'
can someone please let me know what am i doing wrong, is this module not imported in python version of aruco ?
maybe you should try this "aruco.DICT_5X5_250" as parameter, like...
dict = aruco.getPredefinedDictionary( aruco.DICT_5X5_250 )
it worked for me :)
I have a segmenting.py module in a package called processing.
I am trying to call a function in the module in my main. It is extremely simple.
In main.py
from processing import segmenting
segmenting.test()
In segmenting.py
def test():
print 'succeed'
However, I end up with errors as follows:
>>> from processing import segmenting
>>>
>>> segmenting.test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'test'
>>>
Where went wrong?
The most likely cause is that you didn't restart your interactive interpreter after editing (and saving!) segmenting.py. Modules are imported only once and cached. If you edit the source code and then run the import statement again, the module is simply retrieved from the cache and doesn't pick up your changes. See also the reload() built-in.