We are testing our c++ library over SWIG in python using pytest.
However, occasionally when having problem in the SWIG interpretation we are not getting the proper traceback information when importing our library in conftest.py
When we import our library directly in python we get the full exception trace
How can we get the full traceback in pytest as could be achieved when importing the lib directly in python?
Below is a snippet from the SWIG auto-generated with additional manual injected exception for import test purposes
from sys import version_info as _swig_python_version_info
if _swig_python_version_info >= (2, 7, 0):
def swig_import_helper():
import importlib
pkg = __name__.rpartition('.')[0]
mname = '.'.join((pkg, '_sdk')).lstrip('.')
try:
# Added - Generate exception use case
raise ImportError("**** Import Error Use Case ****")
return importlib.import_module(mname)
except ImportError:
return importlib.import_module('_sdk')
When we import the library directly in python we get the full trace including the first exception occurrence:
Import in library directly in python
>>> from leaba import sdk
Traceback (most recent call last):
File ".../out/opt3-debug/pylib/leaba/sdk.py", line 17, in swig_import_helper
raise ImportError("**** Import Error Use Case ****")
ImportError: **** Import Error Use Case ****
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../out/opt3-debug/pylib/leaba/sdk.py", line 21, in <module>
_sdk = swig_import_helper()
File ".../out/opt3-debug/pylib/leaba/sdk.py", line 20, in swig_import_helper
return importlib.import_module('_sdk')
File "/common/pkgs/python/3.6.0/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named '_sdk'
Import in pytest conftest.py,
missing the output of first traceback
ImportError while loading conftest '.../fishnet/tests/conftest.py'.
fishnet/tests/conftest.py:60: in <module>
from init import device_init
fishnet/init/device_init.py:28: in <module>
from leaba import sdk
.../out/opt3-debug/pylib/leaba/sdk.py:21: in <module>
_sdk = swig_import_helper()
.../out/opt3-debug/pylib/leaba/sdk.py:20: in swig_import_helper
return importlib.import_module('_sdk')
/common/pkgs/python/3.6.0/lib/python3.6/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
E ModuleNotFoundError: No module named '_sdk'
How can we tell pytest to provide the full traceback and not to truncate and provide only the last one?
Related
I have a directory tree as follows:
main.py
dir1
sub1.py
sub2.py
In main.py:
import dir1.sub1
In dir1/sub1.py:
def f1() -> None:
print("f1")
import dir1.sub2
dir1.sub2.f2()
In dir1/sub2.py:
import dir1.sub1
def f2() -> None:
dir1.sub1.f1()
print("f2")
When I run main.py, I get the following error message:
Traceback (most recent call last):
File "...\main.py", line 1, in <module>
import dir1.sub1
File "...\dir1\sub1.py", line 7, in <module>
dir1.sub2.f2()
File "...\dir1\sub2.py", line 5, in f2
dir1.sub1.f1()
AttributeError: module 'dir1' has no attribute 'sub1'. Did you mean: 'sub2'?
(Where the ... at the beginning of the file path is my working directory.)
If I change main.py to
import dir1.sub2
I get a slightly different error message:
Traceback (most recent call last):
File "...\main.py", line 1, in <module>
import dir1.sub2
File "...\dir1\sub2.py", line 1, in <module>
import dir1.sub1
File "...\dir1\sub1.py", line 7, in <module>
dir1.sub2.f2()
AttributeError: module 'dir1' has no attribute 'sub2'
If I move sub1.py and sub2.py to the same directory as main.py and re‐direct imports as necessary, I get the expected output of
f1
f2
Why does this happen, and how can I make it not happen?
You need to use absolute import because Python 3 only supports that. In Python 2 your method will work. So for example if you have import dir1.sub2 change it to from dir1 import sub2. See here.
Note: I've tested it with your setup and it works.
I am trying to run pox controller using the command
python2 ./pox.py forwarding. l3_detectionEntropyemphasized text
Then I got this error.
` Traceback (most recent call last):
File "./pox.py", line 43, in <module>
from pox.boot import boot
File "/home/name/pox/pox/boot.py", line 38, in <module>
import pox.core
File "/home/name/pox/pox/core.py", line 182, in <module>
import pox.lib.recoco as recoco
File "/home/name/pox/pox/lib/recoco/__init__.py", line 1, in <module>
from .recoco import *
File "/home/name/pox/pox/lib/recoco/recoco.py", line 17, in <module>
from queue import PriorityQueue
ImportError: No module named queue`
Also I have tried to run it in python3 by converting it into python3 Then i got this error.
`Traceback (most recent call last):
File "/home/shivani/pox/pox/boot.py", line 74, in do_import2
__import__(name, level=0)
File "/home/shivani/pox/pox/forwarding/l3_detectionEntropy.py", line 129
print "dpid port and its packet count: ", str(event.connection.dpid),
str(diction[event.connection.dpid]), str(diction[event.connection.dpid][event.port])
^
SyntaxError: invalid syntax
Could not import module: forwarding.l3_detectionEntropy`
How to solve these error??
Adding paranthesis around the print statement should fix the issue.
Python 3 would raise a SyntaxError if we called the print function the Python 2-way without the parentheses.
In Python, when I use this import statement breze.learn.mlp import iter_minibatches, am getting the following errors.
Here iter_minibatches is a function defined in mlp.py.
Traceback (most recent call last):
File "/home/vinod/PycharmProjects/MLPonTheano/MLPbreze.py", line 15, in <module>
from breze.learn.mlp import Mlp, FastDropoutNetwork
File "/home/vinod/breze/breze/learn/mlp.py", line 22, in <module>
from breze.learn.base import SupervisedModel
File "/home/vinod/breze/breze/learn/base.py", line 21, in <module>
from breze.learn.mlp import iter_minibatches
ImportError: cannot import name iter_minibatches
You have a circular import; mlp imports base imports mlp:
# executing mlp.py
File "/home/vinod/breze/breze/learn/mlp.py", line 22, in <module>
from breze.learn.base import SupervisedModel
# executing base.py
File "/home/vinod/breze/breze/learn/base.py", line 21, in <module>
# this tries to import from mlp again, but mlp isn't done yet
from breze.learn.mlp import iter_minibatches
Any line after the from breze.learn.base import SupervisedModel will not yet have been executed so importing any object defined by those lines will fail.
Avoid circular imports, or if you must have them, delay importing in one of the modules to make sure the objects you need in the other are defined.
I want to run this below program using python..
from __future__ import print_function
import sys
from OCC.STEPControl import STEPControl_Reader
from OCC.IFSelect import IFSelect_RetDone, IFSelect_ItemsByEntity
from OCC.Display.SimpleGui import init_display
from OCC.Display.WebGl import threejs_renderer
from OCC.STEPCAFControl import STEPCAFControl_Reader
step_reader = STEPControl_Reader()
status = step_reader.ReadFile('./models/part_ARROWS_ASM.stp')
if status == IFSelect_RetDone: # check status
failsonly = False
step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity)
ok = step_reader.TransferRoot(1)
_nbs = step_reader.NbShapes()
aResShape = step_reader.Shape(1)
else:
print("Error: can't read file.")
sys.exit(0)
#display, start_display, add_menu, add_function_to_menu = init_display()
#display.DisplayShape(aResShape, update=True)
#start_display()
my_renderer = threejs_renderer.ThreejsRenderer(background_color="#123345")
my_renderer.DisplayShape(aResShape)
Its running successfully without this module OCC.STEPCAFControl. After running above program found this below eeror.
Traceback (most recent call last):
File "D:\software\python\pythonocc-core-master\examples\core_load_step_ap203.py", line 26, in <module>
from OCC.STEPCAFControl import STEPCAFControl_Reader
ImportError: No module named 'OCC.STEPCAFControl'
After got this error paste OCC.STEPCAFControl this module within OCC folder and this problem is solved but its showing another exception which is mentioned below.
Traceback (most recent call last):
File "D:\software\python\pythonocc-core-master\examples\core_load_step_ap203.py", line 26, in <module>
from OCC.STEPCAFControl import STEPCAFControl_Reader
File "C:\Python34\lib\site-packages\OCC\STEPCAFControl.py", line 28, in <module>
_STEPCAFControl = swig_import_helper()
File "C:\Python34\lib\site-packages\OCC\STEPCAFControl.py", line 24, in swig_import_helper
_mod = imp.load_module('_STEPCAFControl', fp, pathname, description)
File "C:\Python34\lib\imp.py", line 243, in load_module
return load_dynamic(name, filename, file)
ImportError: DLL load failed: The specified module could not be found.
How to solve this issue? Any suggestion?
The python module is probably trying to load a DLL called _STEPCAFControl.pyd
(I'm guessing at the extension). If you have a STEPCAFControl.dll with this name, you can rename it _STEPCAFControl.pyd, and it may work. If the names is not correct, you need to look into the STEPCAFControl.py file, and find the .DLL file name.
For scientific python on windows, your best bet is to install WinPython, Python(x,y), Enthought Python or Anaconda rather than trying to install everything manually. Any of these will install most packages you are likely to need, and should configure them correctly.
I have some scripts that run every 30 minutes in cron that import paramiko. Seemingly at random, I will get one of these two errors during import:
Traceback (most recent call last):
...
File "build/bdist.linux-x86_64/egg/paramiko/__init__.py", line 65, in <module>
File "build/bdist.linux-x86_64/egg/paramiko/transport.py", line 42, in <module>
File "build/bdist.linux-x86_64/egg/paramiko/packet.py", line 39, in <module>
File "build/bdist.linux-x86_64/egg/Crypto/Hash/HMAC.py", line 66, in <module>
File "build/bdist.linux-x86_64/egg/Crypto/Util/strxor.py", line 7, in <module>
File "build/bdist.linux-x86_64/egg/Crypto/Util/strxor.py", line 6, in __bootstrap__
ImportError: dynamic module does not define init function (initstrxor)
-- OR --
Traceback (most recent call last):
...
File "build/bdist.linux-x86_64/egg/paramiko/__init__.py", line 65, in <module>
File "build/bdist.linux-x86_64/egg/paramiko/transport.py", line 53, in <module>
File "build/bdist.linux-x86_64/egg/Crypto/Cipher/ARC4.py", line 66, in <module>
File "build/bdist.linux-x86_64/egg/Crypto/Cipher/_ARC4.py", line 7, in <module>
File "build/bdist.linux-x86_64/egg/Crypto/Cipher/_ARC4.py", line 6, in __bootstrap__
ImportError: dynamic module does not define init function (init_ARC4)
Every time that I have seen this issue, simply rerunning the script allows paramiko to import correctly and the script to finish.
What can cause this issue? Any help is greatly appreciated.
While I have no idea what may be causing the error, it seems it's failing on the native parts of pycrypto, so you may want to retry a couple of times:
from time import sleep
n_tries= 3
import_success= False
while not import_success:
try:
from Crypto.Cipher import Blowfish, AES, DES3, ARC4
from Crypto.Hash import MD5, SHA, SHA256, HMAC
from Crypto import Random
from Crypto.PublicKey import DSA, RSA
from Crypto.Util import Counter, number
import_success= True
except ImportError:
if not n_tries:
raise #re-raise ImportError
n_tries-=1
sleep(1)