I am trying to run code that is using wrap_function from torch.utils.ffi (which has deprecated). I am struggling to figure out how to use cpp extensions instead as suggested by the error message, can any anyone help please?
The code I need to replace:
from torch.utils.ffi import _wrap_function
from ._nms import lib as _lib, ffi as _ffi
__all__ = []
def _import_symbols(locals):
for symbol in dir(_lib):
fn = getattr(_lib, symbol)
if callable(fn):
locals[symbol] = _wrap_function(fn, _ffi)
else:
locals[symbol] = fn
__all__.append(symbol)
_import_symbols(locals())
I have tried running the code in both python 3.6 and python 2.7, see error message below,
File "build.py", line 3, in <module>
from torch.utils.ffi import create_extension
File "/usr/local/lib/python2.7/dist-packages/torch/utils/ffi/__init__.py", line 1, in <module>
raise ImportError("torch.utils.ffi is deprecated. Please use cpp extensions instead.")
ImportError: torch.utils.ffi is deprecated. Please use cpp extensions instead.
Check
from torch.utils.cpp import ....
#or
from torch.utils.cpp_extension import ....
#or
from torch.utils.cpp_extension_versioner import...
it looks good at my PC
Related
I'm trying to write a python extension that enables me to use my already working c library in python (working on Raspbian and compiling with arm-linux-gnueabihf-gcc) .
The setup.py compiles (with some warnings) but when I import the extension (bme) in my python3 interpreter i get the following error
>>> import bme
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/python3.7/dist-packages/bme.cpython-37m-arm-linux-gnueabihf.so: undefined symbol: bsec_sensor_control
The bsec_sensor_control function is defined in the bsec_interface.h header.
I tried to include the header directly into the program via relative path.
Here is my setup.py
from distutils.core import setup, Extension
bme_module = Extension('bme',
include_dirs = ['/usr/local/include',
'../BSEC_1.4.8.0_Generic_Release/algo/normal_version/bin/RaspberryPi/PiThree_ArmV6',
'../BSEC_1.4.8.0_Generic_Release/config/generic_33v_3s_28d',
'../BSEC_1.4.8.0_Generic_Release/examples/bsec_iot_example'],
library_dirs = ['../BSEC_1.4.8.0_Generic_Release/algo/normal_version/bin/RaspberryPi/PiThree_ArmV6'],
libraries = ['pthread', 'm', 'rt', 'wiringPi'],
# extra_objects = ['../BSEC_1.4.8.0_Generic_Release/algo/normal_version/bin/RaspberryPi/PiThree_ArmV6/libalgobsec.a'],
extra_compile_args = ['-fPIC'],
depends = ['bsec_integration.h', 'bsec_interface.h', 'bsec_datatypes.h', 'bsec_integration.c', 'bme680.c', 'libalgobsec.a'],
sources =['Pybme.c', '../BSEC_1.4.8.0_Generic_Release/examples/bsec_iot_example/bsec_integration.c', '../BSEC_1.4.8.0_Generic_Release/examples/bsec_iot_example/bme680.c'])
setup (name = 'bme',
version = '1.0',
description = 'Provide BME68X and BSEC outputs for python',
author = 'Nathan',
# url='https://url/of/website',
ext_modules = [bme_module],
headers = ['../BSEC_1.4.8.0_Generic_Release/examples/bsec_iot_example/bme680.h',
'../BSEC_1.4.8.0_Generic_Release/examples/bsec_iot_example/bme680_defs.h',
'../BSEC_1.4.8.0_Generic_Release/examples/bsec_iot_example/bsec_integration.h',
'../BSEC_1.4.8.0_Generic_Release/examples/bsec_iot_example/bsec_interface.h',
'../BSEC_1.4.8.0_Generic_Release/examples/bsec_iot_example/bsec_datatypes.h'])
Since I already tried a lot of things now I would be very happy about some advise on how to fix this or how to figure out the problem.
Thanks
Nathan
You need to link the object, archive or library that defines the symbol bsec_sensor_control. Bosch requires you to accept a license to download the library so that is how I am willing to push it. They do provide build information on github for BSEC-Arduino-library.
I am trying to use glog in my python code and when I am trying to import, it throws the following error:
/usr/local/lib/python2.7/dist-packages/glog.py:171: RuntimeWarning:
Trying to access flag verbosity before flags were parsed. This will raise
an exception in the future.
setLevel(FLAGS.verbosity)
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/glog.py", line 171, in
<module>
setLevel(FLAGS.verbosity)
File "/usr/local/lib/python2.7/dist-packages/gflags/flagvalues.py", line
390, in __getattr__
traceback.print_stack()
E0602 09:45:07.674463 4695 flagvalues.py:399] Trying to access flag
verbosity before flags were parsed.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/gflags/flagvalues.py", line
391, in __getattr__
raise exceptions.UnparsedFlagAccessError(error_message)
UnparsedFlagAccessError: Trying to access flag verbosity before
flags were parsed.
My code is as follows.
import gflags
import glog as log
I have searched online but haven't got any information about the python version of glog. I think this error has to do something with gflags and glog together. Can anyone please explain what is going wrong ?
I have the same issue. After checking the code, it seems some kind of bug of glog.
When import glog is intercepted, it assumes gflags.GFLAGS has been initiated, and tries to read gflags.GFLAGS.verbosity, which actually is not guaranteed to be initialized in most cases.
For example, the following code will throw exception:
import gflags
import glog
import sys
if __name__ == "__main__":
gflags.FLAGS(sys.argv)
# do something.
Since when import glog is intercepted, it reads FLAGS.vebosity, but since glfags.FLAGS(sys.argv) is not executed yet, reading from FLAGS will fail.
So we have to make sure import gflags intercepted after GFLAGS being initialized, the only solution I could think of is putting import glog in another file instead of the main py script.
For example, in main script:
import gflags
import sys
# DON'T IMPORT ANYTHING that evolves of 'glog'
FLAGS = gflags.FLAGS
if __name__ == "__main__":
argv = FLAGS(sys.argv)
# import any modle that uses 'glog'
import your_other_module
your_other_module.do_something()
I am using nltk but the problem I am facing does not seem to be related to nltk specifically.
I have a module named util.tokenize inside which there are some classes and I have the following first line:
util/tokenizer.py
from nltk.tokenize.regexp import RegexpTokenizer
...
class SentTokenizer(object):
def __init__(self, stem=False, pattern='[^\w\-\']+'):
self.alg = RegexpTokenizer(pattern, gaps=True)
def __call__(self, text):
return self.alg.tokenize(text)
....
if __name__ == '__main__':
s_t = SentTokenizer()
s_t('blah blah')
When I call those classes from another module, say test.py everything seems to work, but running the tokenize.py module directly causes ImportError.
File "tokenize.py", line 1, in <module>
...
File "Python27\lib\site-packages\nltk\corpus\reader\util.py", line 28, in <module>
from nltk.util import AbstractLazySequence, LazySubsequence, LazyConcatenation, py25
ImportError: cannot import name AbstractLazySequence
What could be the problem? Why it works when called from other modules?
test.py
from util.tokenize import SentTokenizer
s_t = SentTokenizer()
print s_t('blah blah')
Platform is Windows.
We determined that this was being caused by a namespace conflict with nltk.tokenize and the user's tokenize.py. After renaming tokenize.py, everything worked properly.
I am making an app using python 2.7 on windows and keyring-3.2.1 . In my python code on eclipse, I used
import keyring
keyring.set_password("service","jsonkey",json_res)
json_res= keyring.get_password("service","jsonkey")
is working fine as I am storing json response in keyring. But, when I converted python code into exe by using py2exe, it shows import error keyring while making dist. Please suggest how to include keyring in py2exe.
Traceback (most recent call last):
File "APP.py", line 8, in <module>
File "keyring\__init__.pyc", line 12, in <module>
File "keyring\core.pyc", line 15, in <module>
File "keyring\util\platform_.pyc", line 4, in <module>
File "keyring\util\platform.pyc", line 29, in <module>
AttributeError: 'module' object has no attribute 'system'
platform_.py code is :
from __future__ import absolute_import
import os
import platform
def _data_root_Windows():
try:
root = os.environ['LOCALAPPDATA']
except KeyError:
# Windows XP
root = os.path.join(os.environ['USERPROFILE'], 'Local Settings')
return os.path.join(root, 'Python Keyring')
def _data_root_Linux():
"""
Use freedesktop.org Base Dir Specfication to determine storage
location.
"""
fallback = os.path.expanduser('~/.local/share')
root = os.environ.get('XDG_DATA_HOME', None) or fallback
return os.path.join(root, 'python_keyring')
# by default, use Unix convention
data_root = globals().get('_data_root_' + platform.system(), _data_root_Linux)
platform.py code is:
import os
import sys
# While we support Python 2.4, use a convoluted technique to import
# platform from the stdlib.
# With Python 2.5 or later, just do "from __future__ import absolute_import"
# and "import platform"
exec('__import__("platform", globals=dict())')
platform = sys.modules['platform']
def _data_root_Windows():
try:
root = os.environ['LOCALAPPDATA']
except KeyError:
# Windows XP
root = os.path.join(os.environ['USERPROFILE'], 'Local Settings')
return os.path.join(root, 'Python Keyring')
def _data_root_Linux():
"""
Use freedesktop.org Base Dir Specfication to determine storage
location.
"""
fallback = os.path.expanduser('~/.local/share')
root = os.environ.get('XDG_DATA_HOME', None) or fallback
return os.path.join(root, 'python_keyring')
# by default, use Unix convention
data_root = globals().get('_data_root_' + platform.system(), _data_root_Linux)
The issue you're reporting is due to an environment that contains invalid modules, perhaps from an improper installation of one version of keyring over another. You will want to ensure that you've removed remnants of the older version of keyring. In particular, make sure there's no file called keyring\util\platform.* in your site-packages.
After doing that, however, you'll encounter another problem. Keyring loads its backend modules programmatically, so py2exe won't detect them.
To work around that, you'll want to add a 'packages' declaration to your py2exe options to specifically include the keyring.backends package. I invoked the following setup.py script with Python 2.7 to convert 'app.py' (which imports keyring) to an exe:
from distutils.core import setup
import py2exe
setup(
console=['app.py'],
options=dict(py2exe=dict(
packages='keyring.backends',
)),
)
The resulting app.exe will import and invoke keyring.
I have written the following codes in the setup file and include both sdl_ttf.dll", "SDL.dll in the default folder.
But, it shows an error message:
NotImplementedError:font module not available
<Import error: DLL load failed:can't find assigned module>
The code
from distutils.core import setup
import py2exe,sys,os
import pygame
setup(console=['blackjack.py'])
origIsSystemDLL = py2exe.build_exe.isSystemDLL
def isSystemDLL(pathname):
if os.path.basename(pathname).lower() in ["sdl_ttf.dll", "SDL.dll"]:
return 0
return origIsSystemDLL(pathname)
py2exe.build_exe.isSystemDLL = isSystemDLL
pygamedir = os.path.split(pygame.base.__file__)[0]
os.path.join(pygamedir, pygame.font.get_default_font()),
os.path.join(pygamedir, 'SDL.dll'),
os.path.join(pygamedir, 'SDL_ttf.dll')
Is there something wrong?
Your check
if os.path.basename(pathname).lower() in ["sdl_ttf.dll", "SDL.dll"]:
will not work, since you're calling lower() on the filename, but use SDL.dll instead of sdl.dll, so py2exe will not include the sdl library.
You could also try to use this script from the pygame wiki.