How to unregister wrong Cython extension? - cython

I created Cython Extension
from distutils.core import setup
from Cython.Build import cythonize
import numpy
print (numpy.__file__)
setup(name='spacy text app',
ext_modules=cythonize("spacy.pyx", language="c++"),
include_dirs=[numpy.get_include()]
)
and register it as same name as "spacy" package name.
python spacyc.py build_ext --inplace
Now when i use spacy
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp(u"The firm earned $1.5 million in 2017.")
I get this error
File "spacy.pyx", line 1, in init spacy
from cymem.cymem cimport Pool
ModuleNotFoundError: No module named 'spacy.strings'; 'spacy' is not a package
How to unregister wrong Cython extension?

Actually after a few hours I found out that you need to manually delete the .pyd file in the same directory that contains the script. I hope to help someone in the future.

Related

ModuleNotFoundError: No module named 'torchvision.models.feature_extraction'

I want to extract features in ResNet101, however, I have trouble importing torchvision.models.feature_extraction.
Here is my code:
from torchvision import models
from torchvision.models.feature_extractor import create_feature_extractor
res101 = models.resnet101(pretrained=True)
extractor = create_feature_extractor(
res101,
return_nodes=[
"conv1",
"maxpool",
"layer1",
"layer2",
"layer3",
"layer4",
]
)
features = extractor(inputs)
And here is the error
from torchvision.models.feature_extractor import create_feature_extractor
Traceback (most recent call last):
Input In [11] in <cell line: 1>
from torchvision.models.feature_extractor import create_feature_extractor
ModuleNotFoundError: No module named 'torchvision.models.feature_extractor'
You might be trying to use something like:
from torchvision.models.feature_extraction import create_feature_extractor
See the extraction vs extractor
Check this module
Same problem. I installed PyTorch using conda and it works fine in Jupyter notebooks. But it does not work in terminal.
Turns out the pip listed torchvision version was 0.82.
Solved by updating torchvision using pip.
Maybe some packages installed the old version for me. Hope my experience helps you.

Get programmatically Python3 default path in Octave base directory

I am using GNU Octave. I have script written in python3 and it is working if I do something like that: (it's .m file)
setenv PYTHON python;
[output,status]=python('python_file.py');
output
But some users do not have installed Python on their own. I found that python3.exe is available in "C:\Program Files\GNU Octave\Octave-6.2.0\usr\bin" for default users.
It is possible to get Octave default path and then setenv python pointing to GNU Octave Python directory programically, everything wrote in octave_file.m?
Python file:
import sys
import subprocess
python = sys.executable
subprocess.check_call([python, '-m', 'pip', 'install', 'numpy'], stdout=subprocess.DEVNULL)
import math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
...
result from octave:
/usr/bin/python.exe: No module named pip
Traceback (most recent call last):
File "C:\Users\X\Desktop\cw1.py", line 9, in <module>
subprocess.check_call([python, '-m', 'pip', 'install', 'numpy'], stdout=subprocess.DEVNULL)
File "/usr/lib/python3.8/subprocess.py", line 364, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/usr/bin/python.exe', '-m', 'pip', 'install', 'numpy']' returned non-zero exit status 1.

how to fix nltk stanford import error in python

I am trying to import stanford ner but getting error:
NLTK was unable to find the
C:/Users/.../stanford-ner-2018-10-16/classifiers/all.7class.distsim.crf.ser
file! Use software specific configuration paramaters or set the
STANFORD_MODELS environment variable.
Below the code:
from nltk.tag import StanfordNERTagger
import os
java_path = "C:/Program Files/Java/jre1.8.0_201/bin/java.exe"
os.environ['JAVAHOME'] = "JAVA_PATH" #this java path you get from command 'echo %PATH%'in terminal
st = StanfordNERTagger('C:/Users/.../stanford-ner-2018-10-16/classifiers/all.7class.distsim.crf.ser',
'C:/Users/.../stanford-ner-2018-10-16/stanford-ner.jar',encoding='utf-8')

Reraising an exception in Cython on Python 2 and Python3

I have some Cython code that currently looks
exc = sys.exc_info()
raise exc[0], exc[1], exc[2]
This doesn't work on Python3, since the "raise from tuple" form is no longer allowed. Were this normal Python code, I would just use six.reraise, but that's not available to me here. What's the Cython friendly way to do the same, which works on both Python2 and Python3?
One great Cython feature is that the generated C code can be compiled for either Python 2 or Python 3. So your example above will work with either version of Python, unmodified.
You can tell Cython to compile code assuming Python 2 syntax and semantics (the -2 argument, which is on by default) or assuming Python 3 (the -3 argument). In either case, the resulting extension module source code can be compiled and used for Python 2 or Python 3, as long as the dynamic components (imports, etc.) are compatible.
For example:
def raises_exception():
raise KeyError("what you doin'?")
def foobar():
try:
raises_exception()
except Exception:
import sys
exc = sys.exc_info()
raise exc[0], exc[1], exc[2]
Here's a setup.py that will work on either Py2 or Py3:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize("reraise.pyx"))
I can run python setup.py build_ext -i on either version of Python (provided I have cython installed for each), and the resulting extension module will work.
$ python setup.py build_ext -i # Py3 python
$ ipython3
Python 3.3.2 (v3.3.2:d047928ae3f6, Oct 4 2013, 15:49:17)
Type "copyright", "credits" or "license" for more information.
IPython 1.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import reraise
In [2]: reraise.foobar()
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-2-9e20eacfd84e> in <module>()
----> 1 reraise.foobar()
/.../reraise.so in reraise.foobar (reraise.c:916)()
/.../reraise.so in reraise.foobar (reraise.c:847)()
/.../reraise.so in reraise.raises_exception (reraise.c:762)()
KeyError: "what you doin'?"
In [3]:

I get an error message when I try FreqDist() in NLTK -- NameError: name 'nltk' is not defined

I'm learning about the NLTK and my mac
is working fine except I have trouble with the FreqDist(). (I saw another question about FreqDist() but he was getting a different error message. TypeError: unhashable type: 'list')
Here's an example:
>>> from nltk.corpus import brown
>>> news_text = brown.words(categories='news')
>>> fdist = nltk.FreqDist([w.lower() for w in news_text])
Traceback (most recent call last):
` File "<stdin>", line 1, in <module>`
`NameError: name 'nltk' is not defined`
This error message is pretty consistent. I get this message every time I try the FreqDist(). Other commands like - >>> brown.fileids() are fine.
Thanks for your help!
Before you can use FreqDist, you need to import it.
Add a line as follows:
import nltk
or if you just want to use FreqDist you should try this:
>>> from nltk.corpus import brown
>>> from nltk import FreqDist
>>> news_text = brown.words(categories='news')
>>> fdist = FreqDist([w.lower() for w in news_text])
which means you haven't installed nltk.
follow these steps to install nltk:
1:go to this link https://pypi.python.org/pypi/setuptools at the end of page you find setuptools-7.0.zip (md5) download it, then unzip it. you can find easy_install.py python script.
2:use the command sudo easy_install pip. By this time pip will be installed ready to use, (make sure you are in the directory where you can find easy_install script file).
3:use this command sudo pip install -U nltk. successful execution ensure that nltk is now installed.
4:open the IDLE then you type the following:
import nltk
if nltk is installed properly then you will be returned with console.
setuptools are required for older versions of Python. There is no need for the same if you are running 3.2+
You can easily download the same from https://pypi.python.org/pypi/nltk
For more information on http://www.nltk.org/install.html
nltk requires data you need to download first.
Then run the following code:
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
stopwords.words("english")