Reraising an exception in Cython on Python 2 and Python3 - exception

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]:

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.

Can't compile Cython function with multiple input arguments

I am trying to get Cython running and I think I'm almost there but have hit a snag that I do not understand. I can compile functions with a single argument, but not with multiple. I have this issue with python 3.3, but not 2.7.
I successfully compiled and ran "hello world" and fibonacci examples but when I try to compile anything with two or more input arguments I get an error when compiling.
To boil it down to a very simple case. This compiles, imports to python, and runs.
def cycheckadd(c):
b = 1
d = b + c
return d
This does not.
def cycheckadd(b,c):
d = b + c
return d
And returns the following error when building.
C:\Users\Chris\PycharmProjects\CythonTest>python setup.py build_ext --inplace
Compiling cycheck.pyx because it changed.
Cythonizing cycheck.pyx
running build_ext
building 'cycheck' extension
creating build
creating build\temp.win32-3.3
creating build\temp.win32-3.3\Release
c:\mingw\bin\gcc.exe -mdll -O -Wall -IC:\WinPython-32bit-3.3.5.9\python-3.3.5\include -IC:\WinPython-32bit-3.3.5.9\python-3.3.5\include -c cycheck.c -o build\temp.win32-3.3\Release\
cycheck.o
writing build\temp.win32-3.3\Release\cycheck.def
creating build\lib.win32-3.3
c:\mingw\bin\gcc.exe -shared -s build\temp.win32-3.3\Release\cycheck.o build\temp.win32-3.3\Release\cycheck.def -LC:\WinPython-32bit-3.3.5.9\python-3.3.5\libs -LC:\WinPython-32bit-3
.3.5.9\python-3.3.5\PCbuild -lpython33 -lmsvcr100 -o build\lib.win32-3.3\cycheck.pyd
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libmsvcrt.a(dupvs00137.o):(.idata$5+0x0): multiple definition of `_imp___assert'
C:\WinPython-32bit-3.3.5.9\python-3.3.5\libs/libmsvcr100.a(dqcgs00457.o):(.idata$5+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
Compiled using the setup.py file below.
from setuptools import setup
from Cython.Build import cythonize
setup(
name = 'cython math check',
ext_modules = cythonize("cycheck.pyx"),
)
This result was generated using python 3.3 and cython 0.22. I do not have this problem when I use 2.7.
The compiler is MinGW. I have been using 32bit versions of Python on Windows 10-64bit. I have added the following config file to the python directory.
C:\WinPython-32bit-3.3.5.9\python-3.3.5\Lib\distutils\distutils.cfg
[build]
compiler = mingw32
[build_ext]
compiler = mingw32
Any help would be greatly appreciated!
Thanks,
Chris

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")

calling legacy c functions from python

I want to call legacy c third party functions from python.
I created a C api to make the function calls simpler.
In my python file
I tried to import the *.so for the api which links with the legacy code library.
But I kept seeing a libxxx ( the legacy c library) not found error.
I tried to import both ans still saw the same error.
I then tried using ctypes, but get the error ctypes not found.
Any suggestions ?
# python
Python 2.4.2 (#1, Apr 20 2012, 19:31:49)
[GCC 4.1.2 20070115 (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named ctypes
>>> quit
ctypes only made it into the standard library in version 2.5, so 2.4 does not have it. I've also heard that some people exclude it when they build Python for a distribution or for embedding in a specific application, as you can easily crash your process using it, or even corrupt the host application's heap. But I have never encountered this myself, and I doubt a full installation does this.
As for not finding your C library, we'll need more details on that.