Error in importing a new python module in knitr - r

I want to use a python engine in knitr. I have installed a module named undaqTools for python 2.7 using Anaconda3. If I run the following in IPython, it works fine:
from undaqTools import Daq
I tried the same in a python code chunk but that didn't work. I found a relevant question in SO, linked here. So, I first did following in IPython:
import undaqTools
import os
path = os.path.dirname(undaqTools.__file__)
path
Out[5]: 'C:\\Anaconda3\\envs\\py27\\lib\\site-packages\\undaqtools-0.2.3-py2.7.egg\\undaqTools'
Therefore, I created a new chunk in knitr and did following:
```{python}
import sys
sys.path.append('C:/Anaconda3/envs/py27/lib/site-packages/undaqtools-0.2.3-py2.7.egg/')
from undaqTools import Daq
```
But that gives me following error:
Traceback (most recent call last):
File "C:\Users\durraniu\AppData\Local\Temp\RtmpGOOQHX\chunk-code195061e728da.", line 13, in <module>
from undaqTools import Daq
File "C:\Anaconda3\envs\py27\lib\site-packages\undaqtools-0.2.3-py2.7.egg\undaqTools\__init__.py", line 5, in <module>
ImportError: No module named 'daq'
How can I fix this?

Related

Pyinstaller-spacy : "OSError: [E050] Can't find model 'es_core_news_sm'. It doesn't seem to be a Python package or a valid path to a data directory."

I did an application with Tkinter and this has a function based in spacy. When a built the executable file using Pyinstaller I ran pyinstaller simple.py --additional-hooks-dir=.. My file hook collect the necessary packages also the pre-trained language spacy model es_cores_news_sm with the follow code:
from PyInstaller.utils.hooks import collect_all
# ----------------------------- SPACY -----------------------------
data = collect_all("spacy")
datas = data[0]
binaries = data[1]
hiddenimports = data[2]
from PyInstaller.utils.hooks import collect_data_files
datas += collect_data_files("es_core_news_sm")
The executable was create and open well. When I use the function based in spacy we obtain the follow error:
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter/__init__.py", line 1705, in __call__
File "simple.py", line 152, in calcular
File "predictors.py", line 16, in cash_pred
File "utils.py", line 41, in clean_text
File "spacy/__init__.py", line 47, in load
File "spacy/util.py", line 329, in load_model
OSError: [E050] Can't find model 'es_core_news_sm'. It doesn't seem to be a Python package or a valid path to a data directory.
Originally, I installed in my pip environment the pre-trained model with python -m spacy download es_core_news_sm. How can I collect well this pre-trained model well in Pyinstaller?

Not able to import simpletransformers.ner in python as it says ImportError: cannot import name 'BertweetTokenizer'

I am trying to do bert based NER and trying to import,
from simpletransformers.ner import NERModel, NERArgs
I have installed simpletranformers in pip and when I try to import,
import simpletransformers.ner
it says, ImportError: cannot import name 'BertweetTokenizer'. When I try to install BertweetTokenizer, it throws
ERROR: No matching distribution found for BertweetTokenizer
Not sure, what I am missing. Kindly help

Why the imported PowerFactory module in python can only execute single time?

The script is be able to run a software called PoiwerFctory externally by Python as follows:
#add powerfactory.pyd path to python path
import sys
sys.path.append("C:\\Program Files\\DIgSILENT\\PowerFactory 2017
SP2\\Python\\3.6")
#import powerfactory module
import powerfactory
#start powerfactory module in unattended mode (engine mode)
app=powerfactory.GetApplication()
#get the user
user=app.GetCurrentUser()
#active project
project=app.ActivateProject('Python Test') #active project "Python Test"
prj=app.GetActiveProject #returns the actived project
#run python code below
ldf=app.GetFromStudyCase('ComLdf') #caling loadflow command object
ldf.Execute() #executing the load flow command
#get the list of lines contained in the project
Lines=app.GetCalcRelevantObjects('*.ElmLne') #returns all relevant objects,
i.e. all lines
for line in Lines: #get each element out of list
name=line.loc_name #get name of the line
value=line.GetAttribute('c:loading') # return the value of elements
#Print the results
print('Loading of the line: %s = %.2f'%(name,value))
When the above code first time executed in Spyder, it will show proper resutls. However, if re-executing the script again, the following error is appeared:
Reloaded modules: powerfactory
Traceback (most recent call last):
File "<ipython-input-9-ae989570f05f>", line 1, in <module>
runfile('C:/Users/zd1n14/Desktop/Python Test/Call Digsilent in
Python.py', wdir='C:/Users/zd1n14/Desktop/Python Test')
File "C:\ProgramData\Anaconda3\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/zd1n14/Desktop/Python Test/Call Digsilent in Python.py",
line 12, in <module>
user=app.GetCurrentUser()
RuntimeError: 'powerfactory.Application' already deleted
Referred to How can I exit powerfactory using Python in Unattended mode?, this may because of PowerFactory in still running. And the only way which has been found so far is to re-start the Spyder and execute the script again, this is so inefficiency that if I want to re-write the code and debugging it.
It would be so much appropriated that if anyone could give me some advice for such problem.
I ran into the same Problem. Python is still connected to powerfactory and gives the Error if you try to connect again. What basicly worked for me was to kill the instance on the end of your skript with
del app
another idea during debugging could be:
try:
# Do something in your skript
finally:
del app
So the killing of the instance happens in any case.
The way to solve this is to reload the powerfacotry module by adding:
if __name__ == "__main__":
before import powerfacory.
The reason behind may referred to: What does if __name__ == "__main__": do?.

Can't import .sage file in same directory

I have Sage files called "a.sage" and "b.sage" in the same directory, and a.sage includes the line "from b import blah". However, when I run "sage a.sage" from the command line, I get "ImportError: No module named b".
I've tried preparsing b.sage and I've tried adding '.' to the end of SAGE_PATH in my .bashrc file. Can't find any more suggestions. I would appreciate someone's help with this, because I'm pulling my hair out!
The issue is that .sage files aren't Python modules.
Traceback (most recent call last):
File "a.sage.py", line 3, in <module>
from b import foo
ImportError: No module named b
If you want to import a module in this way, I think you will need to make a Python module.
$ mv b.sage b.py
$ sage a.sage
hi
However, you'll need to import things from Sage as need be in the Python file, e.g. from sage.all import ZZ.
Extra very useful info from #Dima Pasechnik:
You can do this alternately by doing sage b.sage which generates a Python file b.sage.py which you need to copy to b.py (or even better, create a symlink, i.e. ln -s b.sage.py b.py - you'd only need to do this once). Now import b will work.

Getting data from python to R: rpy2 import pandas.rpy.common error

I am learning python and am just getting into pandas. I am already familiar with R. I would like to be able to create a data frame in python and then somehow get it into R.
Could I do this somehow by writing the file to a CSV and then opening it into R? If so, where should I go to learn how to do this?
I found this website: http://pandas.pydata.org/pandas-docs/dev/r_interface.html that seems to be one solution - it uses rpy2 as a link between python and R. It is unclear to me, though, how the data frame actually gets opened up in R.
But I couldn't get that far because I had an error when trying to execute this code:
import pandas.rpy.common as com
I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/pandas/rpy/__init__.py", line 2, in <module>
from common import importr, r, load_data
File "/Library/Python/2.7/site-packages/pandas/rpy/common.py", line 12, in <module>
from rpy2.robjects.packages import importr
File "rpy2.py", line 1, in <module>
import pandas.rpy.common as com
AttributeError: 'module' object has no attribute 'rpy'
I installed rpy2 using easy_install. It seems to be the latest version.
What should I do?

Resources