Python program showing exception - python

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.

Related

Basic python error with importing module - getting modulenotfound error

I'm doing something basic with python, and I'm getting a pretty common error, but not able to find exactly what's wrong. I'm trying to use a custom module (built by someone else). I have the folder structure like this:
There is the test folder, and I have a file testing.py within that:
The contents of testing.py is:
from util import get_data, plot_data
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
When I run this file, using python testing.py, I get this:
I went through the other questions that speak about paths, and this looks fine, so not sure what I am missing here. My environment is setup using conda, and the environment is active.
EDIT
As per #allan-wind, I made the relative edit, which got me past the error, but now getting different errors:
I tried the relative import, and it got past that error, but then it is now throwing this error:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\envs\ml4t\lib\multiprocessing\context.py", line 190, in get_context
ctx = _concrete_contexts[method]
KeyError: 'fork'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "grade_analysis.py", line 21, in <module>
from grading.grading import (
File "E:\_Repo\GT\CS7646\mls4tsp23\grading\grading.py", line 15, in <module>
multiprocessing.set_start_method('fork')
File "C:\ProgramData\Anaconda3\envs\ml4t\lib\multiprocessing\context.py", line 246, in set_start_method
self._actual_context = self.get_context(method)
File "C:\ProgramData\Anaconda3\envs\ml4t\lib\multiprocessing\context.py", line 238, in get_context
return super().get_context(method)
File "C:\ProgramData\Anaconda3\envs\ml4t\lib\multiprocessing\context.py", line 192, in get_context
raise ValueError('cannot find context for %r' % method)
ValueError: cannot find context for 'fork'
`
There are a number of ways to specify how to find modules:
Use a relative import:
from .util import get_data, plot_data
Set the environment variable PYTHONPATH includes the directory where you module resides.
See sys.meta_path
Just place utils in the same folder as your testing.py and the python interpreter you put that directory in your path. Other solutions would be to place utils in a directory that is already in your path, since if thats not the case, you cant import from "above" the current directory

How to enable pytest full trace on import error?

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?

I am having an error on accessing a file from another directory with Python

This is my Python code:
from plugin import Plugin
import logging
import yaml
log = logging.getLogger('discord')
def get_bot_prefix():
with open('HarTex/hartexConfig.yaml', 'r') as prefixReader:
prefixValue = yaml.safe_load(prefixReader)
prefixString = prefixValue['settings']['prefix']
return prefixString
prefix = get_bot_prefix()
However I got an error with the file accessing:
Traceback (most recent call last):
File "C:/Users/85251/Documents/Discord Bots/Python/HarTex/bot.py", line 20, in <module>
from plugins.help import Help
File "C:\Users\85251\Documents\Discord Bots\Python\HarTex\plugins\help.py", line 30, in <module>
prefix = get_bot_prefix()
File "C:\Users\85251\Documents\Discord Bots\Python\HarTex\plugins\help.py", line 22, in get_bot_prefix
with open('HarTex/hartexConfig.yaml', 'r') as prefixReader:
FileNotFoundError: [Errno 2] No such file or directory: 'HarTex/hartexConfig.yaml'
How can I fix it? Or am I completely wrong with the directory?
The script should work if you are calling it from the parent directory of HarTex, maybe you are running it from a different working directory?
You could also try to open the file using the full path, as this is probably easy to check.
Error is very clear.You should use absolute path instead of relative path.
For an example home/Prakash/Desktop/test12/test.yaml
Your code definitely work,Once you will change path like this.

python3 ImportError: cannot import name 'rand_str'

Im getting this error when starting my software i dont know how to repair this
Traceback (most recent call last):
File "/root/findex/lib/python3.5/site-packages/findex_gui/main.py", line 208, in web
run_async()
File "/root/findex/lib/python3.5/site-packages/findex_gui/main.py", line 201, in run_async
from findex_gui.web import app
File "/root/findex/lib/python3.5/site-packages/findex_gui/web.py", line 41, in <module>
from findex_gui.orm.connect import Database
File "/root/findex/lib/python3.5/site-packages/findex_gui/orm/connect.py", line 12, in <module>
from findex_gui.orm.models import BASE
File "/root/findex/lib/python3.5/site-packages/findex_gui/orm/models.py", line 22, in <module>
from findex_common.utils import rand_str
ImportError: cannot import name 'rand_str'
I'm not familiar with the libraries in question, but it seems the maintainer renamed this function in 0.15.14 of findex-common here: development
This has been adjusted for in findex-gui on the master branch though it seems it has not seen a release. Here's the commit which fixes the import: Forgot what I was doing - Ill just push it all ¯_(ツ)_/¯
One short term fix would be to downgrade to findex-common==0.15.13 and perhaps follow up by raising an issue on either project since the change seems non-ideal.

python ctypes.CDLL does not load library although location is know

To run one of my script I have to use the shapely python package, but it crashes because it cannot load the libgeos library. It gives me the following error message:
Traceback (most recent call last):
File "download_nl.py", line 2, in <module>
from download_sentinel import sentinel_available, sentinel_download, download_orbits
File "/nfs/home2/gmulder/radar_database/download_sentinel.py", line 14, in <module>
from fastkml import kml
File "/home/gmulder/python_packages/fastkml/__init__.py", line 30, in <module>
from .kml import KML, Document, Folder, Placemark
File "/home/gmulder/python_packages/fastkml/kml.py", line 37, in <module>
from .geometry import Geometry
File "/home/gmulder/python_packages/fastkml/geometry.py", line 23, in <module>
from shapely.geometry import Point, LineString, Polygon
File "/home/gmulder/python_packages/shapely/geometry/__init__.py", line 4, in <module>
from .base import CAP_STYLE, JOIN_STYLE
File "/home/gmulder/python_packages/shapely/geometry/base.py", line 9, in <module>
from shapely.coords import CoordinateSequence
File "/home/gmulder/python_packages/shapely/coords.py", line 8, in <module>
from shapely.geos import lgeos
File "/home/gmulder/python_packages/shapely/geos.py", line 73, in <module>
_lgeos = load_dll('geos_c', fallbacks=['libgeos_c.so.1', 'libgeos_c.so'])
File "/home/gmulder/python_packages/shapely/geos.py", line 68, in load_dll
libname, fallbacks or []))
OSError: Could not find lib geos_c or load any of its variants ['libgeos_c.so.1', 'libgeos_c.so'].
However, the strange thing is that, when I run python interactively, the import goes fine and I do not get any errors. To check whether it is a problem with setting the right paths, I also checked the source. The original code is the following, where the libname gives the library name.
def load_dll(libname, fallbacks=None, mode=DEFAULT_MODE):
lib = find_library(libname)
dll = None
print(sys.version)
dirname = '/hpc/sw/geos-3.5.0/lib'
os.environ['PATH'] = dirname + os.pathsep + os.environ['PATH']
if os.path.exists(os.path.join(dirname, 'libgeos_c.so.1')):
print('library does exist!')
os.chdir(dirname)
if lib is not None:
try:
LOG.debug("Trying `CDLL(%s)`", lib)
dll = CDLL(lib)
except OSError:
LOG.warn("Failed `CDLL(%s)`", lib)
pass
if not dll and fallbacks is not None:
for name in fallbacks:
try:
LOG.debug("Trying `CDLL(%s)`", name)
dll = CDLL(name)
except OSError:
# move on to the next fallback
LOG.warn("Failed `CDLL(%s)`", name)
pass
print(dll)
if dll:
LOG.debug("Library path: %r", lib or name)
LOG.debug("DLL: %r", dll)
return dll
else:
# No shared library was loaded. Raise OSError.
raise OSError(
"Could not find lib {0} or load any of its variants {1}.".format(
libname, fallbacks or []))
To check whether the needed lib file location can be found by the code I added an extra check at line 7, which confirms the library is there. But then, when I try to load it, nothing is loaded, even if I feed the full path to the CDLL function directly.
I also tried to load the library by running python and opening the library interactively (not from a python script), which worked fine. Therefore, I checked the python versions, because I thought that could maybe cause this problem. But the python versions for my script and the interactive session are the same. Can someone help me with this? Likely I missed something, but I have no idea what is causing this problem.

Categories

Resources