Comments (8)
You can specify path to libpython using nimpy.py_lib.pyInitLibPath
. Also see #171.
from nimpy.
So here's the working code of mine, Nim 1.6.0, nimpy 0.2.0
import nimpy
let
py = pyBuiltinsModule()
echo py.eval("2+1", py.dict())
Solution found at https://blogs.mathworks.com/loren/2020/03/03/matlab-speaks-python/
from nimpy.
What does echo pyImport("sys").paths
say?
from nimpy.
that one:
/Users/pietroppeter/.nimble/pkgs/nimpy-0.2.0/nimpy.nim(867) getAttr
/Users/pietroppeter/.nimble/pkgs/nimpy-0.2.0/nimpy/py_utils.nim(98) raisePythonError
Error: unhandled exception: <type 'exceptions.AttributeError'>: 'module' object has no attribute 'paths' [Exception]
but I guess it might have been path
and this goes with Python 2.7!
['/usr/lib/python27.zip', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-darwin', '/usr/lib/python2.7/plat-mac', '/usr/lib/python2.7/plat-mac/lib-scriptpackages', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']
I guess this is it, how do I fix it?
the Mac came with python 2.7 for python but I did change the alias to have it use python3
from nimpy.
tried both paths below (the first is the one given me by find_libpython, the second it looked reasonable to me by looking around in the folders):
import nimpy
import nimpy / py_lib
#pyInitLibPath "/usr/local/Cellar/[email protected]/3.9.10/Frameworks/Python.framework/Versions/3.9/Python"
pyInitLibPath "/usr/local/Cellar/[email protected]/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/libpython3.9.dylib"
let py = pyBuiltinsModule()
discard py.exec("sum([1, 2, 3])")
but the result is always an error of loading like this
Error: unhandled exception: Could not load libpython. Tried /usr/local/Cellar/[email protected]/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/libpython3.9.dylib
not sure what I should use...
from nimpy.
Not sure, could it be because of architecture mismatch?
file /usr/local/Cellar/[email protected]/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/libpython3.9.dylib
file $MY_EXECUTABLE
from nimpy.
I went back this (sorry for the delay) and I think back then I had indeed an architecture mismatch (default python on Mac M1 is x86_64). I installed an arm python and now I have both my new python and my nim executable that are both arm
(checked with file
see below).
I do get a different error now. For reference now I compile and run:
import nimpy
import nimpy / py_lib
pyInitLibPath "/Library/Frameworks/Python.framework/Versions/3.11/lib/libpython3.11.dylib"
let py = pyBuiltinsModule()
discard py.exec("sum([1, 2, 3])")
and the error is:
/Users/pietroppeter/play_nimpy/nimpyissue261.nim(10) nimpyissue261
/Users/pietroppeter/.nimble/pkgs/nimpy-0.2.0/nimpy.nim(845) callMethodAux
/Users/pietroppeter/.nimble/pkgs/nimpy-0.2.0/nimpy/py_utils.nim(98) raisePythonError
Error: unhandled exception: <class 'SystemError'>: frame does not exist [Exception]
checking architecture with file:
❯ file /Library/Frameworks/Python.framework/Versions/3.11/lib/libpython3.11.dylib
/Library/Frameworks/Python.framework/Versions/3.11/lib/libpython3.11.dylib: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit dynamically linked shared library x86_64] [arm64]
/Library/Frameworks/Python.framework/Versions/3.11/lib/libpython3.11.dylib (for architecture x86_64): Mach-O 64-bit dynamically linked shared library x86_64
/Library/Frameworks/Python.framework/Versions/3.11/lib/libpython3.11.dylib (for architecture arm64): Mach-O 64-bit dynamically linked shared library arm64
❯ file nimpyissue261
nimpyissue261: Mach-O 64-bit executable arm64
from nimpy.
I'm having the same issue here. I used the find_libpython
and added pyInitLibPath
. My test code:
import nimpy
import nimpy / py_lib
pyInitLibPath "/usr/lib/x86_64-linux-gnu/libpython3.11.so.1.0"
let py = pyBuiltinsModule()
discard py.exec("sum([1, 2, 3])")
Output:
/tmp/test.nim(8) test
/home/dmknght/.nimble/pkgs/nimpy-0.2.0/nimpy.nim(850) callMethodAux
/home/dmknght/.nimble/pkgs/nimpy-0.2.0/nimpy/py_utils.nim(98) raisePythonError
Error: unhandled exception: <class 'SystemError'>: frame does not exist [Exception]
Error: execution of an external program failed: '/tmp/test '
I don't really think this problem comes from different architecture
Update: To make sure pyBuiltinsModule()
worked normally, i tried echo py.bin(3)
to test and ofc it worked. I tried digging a bit and I found that code use pybind
could have this problem too
https://stackoverflow.com/questions/53115558/compile-and-execute-ast-using-pybind11-or-python-c-api
Update 2: According to the stackoverflow link, apparently we have to pass py.globals()
into eval or exec. However, it showed the NULL exception (just like in the comments). I didn't find a way to execute code properly. But here's my code
import nimpy
let
py = pyBuiltinsModule()
modl_builtin = pyImport("builtins")
echo py.eval("x+1", py.globals())
echo modl_builtin.exec("x + 1", py.globals())
from nimpy.
Related Issues (20)
- Could not find platform dependent libraries <exec_prefix> HOT 3
- Codon
- have some trouble printing a module's version HOT 1
- keeping long lived objects alive from python HOT 2
- Import local py file HOT 2
- Nim 2.0 compatibility HOT 2
- [Bug] Using nim procedures as Python decorators is not working HOT 2
- How to unpack PPyObject (dict) to kwargs in Nim? HOT 1
- It is possible to call async function? HOT 1
- How to use python decorators HOT 1
- Error: type mismatch: nimpy.nim(223, 37) HOT 1
- undefined symbol: _PyModule_Add [Exception] HOT 3
- Nim Version 2.0 breaks (Mac) HOT 4
- Issue importing in windows HOT 8
- Examples do not compile - `type mismatch` HOT 1
- Python Command Line Arguments Disappearing HOT 1
- How to pass Windows data types like HWND & HBRUSH
- No way of initializing type with kwargs HOT 1
- Nimpy cannot import modules with default python 3.12 install on Windows 11
- __dict__ or __slots__ implementation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nimpy.