pythonnet use System.Text.Json - python

I am trying to utilise a .NET6 library that depends on System.Text.Json which is failing to import using pythonnet:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ModuleNotFoundError: No module named 'System'
In python, inspecting the assemblies pythonnet has added by default, it can be seen that it does not ship with the System.Text.Json namespace loaded:
import clr
print("[", ", ".join(clr.ListAssemblies(False)), "]")
Outputs:
[ mscorlib, clrmodule, Python.Runtime, System.Core, System.Configuration, System.Xml, System, __CodeGenerator_Assembly, e__NativeCall_Assembly ]
I then try to add System.Text.Json, which appears to succeed:
import clr
import sys
DOTNET_PATH: str = {YOUR PATH TO .NET6 DLLs}
sys.path.append(DOTNET_PATH)
clr.AddReference("System.Text.Json")
print("[", ", ".join(clr.ListAssemblies(False)), "]")
Outputs:
[ ..., System, System.Text.Json, System.Runtime, ... ]
However, trying to import a class from the namespace:
from System.Text.Json import JsonDocument
Continues to raise:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ModuleNotFoundError: No module named 'System'
(I have also tried adding every .dll that ships with .NET 6.0.1 with no success)
Is there some other mechanism I need to utilise to successfully import from this namespace? (And associated)

To use .NET Core/.NET 5+ assemblies you need pythonnet 3.0.0 or later (currently in preview).
You also need to explicitly load coreclr:
from clr_loader import get_coreclr
from pythonnet import set_runtime
coreclr = get_coreclr("/full/path/to/app.runtimeconfig.json")
set_runtime(coreclr)
// here goes the rest of your code
runtimeconfig.json file is created by dotnet publish

Related

No file found when converting a python project with NUITKA with library OWLREADY2

After I converted my python project with NUITKA and I started running the following message appeared:
Owlready2 * Warning: optimized Cython parser module 'owlready2_optimized' is not available, defaulting to slower Python implementation
Traceback (most recent call last):
File "/user/try.dist/try.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "/user/try.dist/owlready2/__init__.py", line 35, in <module owlready2>
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "/user/try.dist/owlready2/reasoning.py", line 41, in <module owlready2.reasoning>
FileNotFoundError: [Errno 2] No such file or directory: '/user/try.dist/owlready2/pellet'
Indicating that after I compiled the program some files were not found.
The code that I run was python3 -m nuitka --follow-imports --enable-plugin=numpy --onefile try.py
and the code that I compiled was just simple this:
from owlready2 import *
onto = get_ontology("file:///user/ontologies/try.owl").load()
with onto:
types.new_class("Drug", (Thing,))
onto.save()
So simple there was something wrong with the library.
If anyone knows what to do it will be helpful.
Thanks in advance.
I have changed the location of the file for privacy

IDAPython with PyCharm throws: "ModuleNotFoundError: No module named '_ida_hexrays'"

I want to use PyCharm with IDAPython. I setup the Python Interpreter by adding the following path to the "Interpreter Paths":
C:\Program Files\IDA Pro 7.7\python\3
After that, it recognize the modules idaapi and idautils which is a good sign.
I wrote a simple scripts to test it:
import idaapi
import idautils
filename = idaapi.get_input_file_path()
I set a breakpoint on the filename... row and it failed with:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Program Files\IDA Pro 7.7\python\3\ida_hexrays.py", line 12, in <module>
import _ida_hexrays
ModuleNotFoundError: No module named '_ida_hexrays'
It doesn't recognize the modules _ida_hexrays. How can I make it work?
I see that there is a file: C:\Program Files\IDA Pro 7.7\python\3\ida_64\_ida_hexrays.pyd.
In the ida_hexrays.py it throws the error when it try to import it like that:
# Import the low-level C/C++ module
if __package__ or "." in __name__:
from . import _ida_hexrays
else:
import _ida_hexrays

ModuleNotFoundError: No module named 'PySide2' while trying to use poetry env

i'm trying to use slicereg (https://github.com/brainglobe/slicereg) for data registration.
after import code from github page and install poetry to run slicereg i keep getting the following error :
The virtual environment found in ~\.conda\envs\sliceregenv seems to be broken.
Recreating virtualenv slicereg-6kSWvOuc-py3.8 in ~\AppData\Local\pypoetry\Cache\virtualenvs\slicereg-6kSWvOuc-py3.8
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "~\.conda\envs\sliceregenv\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 843, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "~\slicereg\slicereg\main.py", line 3, in <module>
from PySide2.QtWidgets import QApplication
ModuleNotFoundError: No module named 'PySide2'
Pyside2 is already installed in my env using pip.
Any advice on how to resolve this issue ?
Thx
Seems to be an issue, poetry fails to recognize conda environments as valid
see more at https://github.com/python-poetry/poetry/issues/4566
Remain unresolved for the current release (1.1.12)
Update : https://github.com/python-poetry/poetry/issues/5907

AttributeError: module 'numpy.core' has no attribute 'numerictypes'

Strange behavior in PyCharm (2021.2.1 Professional Edition) with Python 3.9 and numpy 1.21.2.
When I run my program with "Run" then it works as expected. But when I run it with "Debug" then the following error messages appears and the program stops:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Python\JobAnalyzer\venv\lib\site-packages\numpy\__init__.py", line 200, in <module>
core.numerictypes.typeDict,
AttributeError: module 'numpy.core' has no attribute 'numerictypes'
python-BaseException
Any hints what could be the cause?
It crashes immediately at the following line:
import matplotlib.pyplot as plt
And in numpy at this line:
# Numpy 1.20.0, 2020-10-19
__deprecated_attrs__["typeDict"] = (
core.numerictypes.typeDict, <--- CRASH HERE
"`np.typeDict` is a deprecated alias for `np.sctypeDict`."
)
The bug is documented here, it is related to PySide2, which is used by default by PyCharm for PyQt compatibility. The workaround (besides downgrading numpy) is to go to PyCharm's settings / "Build, Execution, Deployment" / "Python debugger", and there uncheck or change the value of the PyQt compatibility drop-down to something else than Auto or PySide2:

Logging module not working with Python3

I am having issues with the standard logging module. If I open a python2.7 shell and import logging everything works fine:
$ python
>>> import logging
>>>
But if I open a python3.4 shell and import logging I get the following error:
$ python3.4
>>> import logging
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2222, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 2164, in _find_spec
File "<frozen importlib._bootstrap>", line 1940, in find_spec
File "<frozen importlib._bootstrap>", line 1916, in _get_spec
File "<frozen importlib._bootstrap>", line 1897, in _legacy_get_spec
File "<frozen importlib._bootstrap>", line 863, in spec_from_loader
File "<frozen importlib._bootstrap>", line 904, in spec_from_file_location
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/logging-0.4.9.6-py3.4.egg/logging/__init__.py", line 618
raise NotImplementedError, 'emit must be implemented '\
^
SyntaxError: invalid syntax
I have no idea what the problem is and can't seem to find anyone else who has had the same issue.
logging module is by default there in Python 3 environment .No need to import it.
You seem to have installed a third party library called logging in your Python 3 environment, which is hiding the standard library version, and one of its files has a typo.
I stupidly created a file called logging.py to try out some log features. Then when trying the code below, it is effectively referring to itself and can't find the debug method.
import logging
logging.debug("Debug message")
Changing my file name to logtest.py fixed the problem.
for me it was the previous install of a logging library under an older Python version.
pip3 uninstall logging
fixed it for me.

Categories

Resources