搜索
bottom↓
回复: 14

求助帖 关于python ctypes调用

[复制链接]

出0入4汤圆

发表于 2020-4-7 12:27:18 | 显示全部楼层 |阅读模式
/ # ./_install/bin/python
Python 2.7.3 (default, Apr  7 2020, 12:06:28)
[GCC 4.9.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/_install/lib/python2.7/ctypes/__init__.py", line 10, in <module>
    from _ctypes import Union, Structure, Array
ImportError: No module named _ctypes
好像是没有编译这个ctypes 模块 但是找了下资料都说这个模块是python 自带的不需要安装?  
请问下这是怎么回事?


阿莫论坛20周年了!感谢大家的支持与爱护!!

月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!

出0入0汤圆

发表于 2020-4-7 14:45:05 | 显示全部楼层
python 3.7.0测试没有问题
可能从python3开始自带了吧

出0入0汤圆

发表于 2020-4-7 21:15:29 | 显示全部楼层
python 3.7没问题,python2的问题不要去纠结了,没啥意义,赶紧换py3

出0入0汤圆

发表于 2020-4-7 23:23:40 | 显示全部楼层
有的,搜了下官方Wiki:https://docs.python.org/2.7/library/ctypes.html

出0入4汤圆

 楼主| 发表于 2020-4-8 10:37:44 | 显示全部楼层
w282529350 发表于 2020-4-7 14:45
python 3.7.0测试没有问题
可能从python3开始自带了吧

可是我编译python 3.7 也是出现这个问题 编译不报错 但是ctypes 没有添加进入

出0入4汤圆

 楼主| 发表于 2020-4-8 10:38:43 | 显示全部楼层
dreambox 发表于 2020-4-7 21:15
python 3.7没问题,python2的问题不要去纠结了,没啥意义,赶紧换py3

换了依然还是这个问题。。。

出0入4汤圆

 楼主| 发表于 2020-4-8 10:38:59 | 显示全部楼层
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2                  _curses               _curses_panel      
_dbm                  _gdbm                 _hashlib           
_lzma                 _sqlite3              _ssl               
_tkinter              _uuid                 readline           
zlib                                                           
To find the necessary bits, look in setup.py in detect_modules() for the module's name.


The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
_abc                  atexit                pwd               
time                                                           


Failed to build these modules:
_ctypes                                                        


Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381

running build_scripts
creating build/scripts-3.7
copying and adjusting /home/kyq/python/Python-3.7.5/Tools/scripts/pydoc3 -> build/scripts-3.7
copying and adjusting /home/kyq/python/Python-3.7.5/Tools/scripts/idle3 -> build/scripts-3.7
copying and adjusting /home/kyq/python/Python-3.7.5/Tools/scripts/2to3 -> build/scripts-3.7
copying and adjusting /home/kyq/python/Python-3.7.5/Tools/scripts/pyvenv -> build/scripts-3.7
changing mode of build/scripts-3.7/pydoc3 from 664 to 775
changing mode of build/scripts-3.7/idle3 from 664 to 775
changing mode of build/scripts-3.7/2to3 from 664 to 775
changing mode of build/scripts-3.7/pyvenv from 664 to 775
renaming build/scripts-3.7/pydoc3 to build/scripts-3.7/pydoc3.7
renaming build/scripts-3.7/idle3 to build/scripts-3.7/idle3.7
renaming build/scripts-3.7/2to3 to build/scripts-3.7/2to3-3.7
renaming build/scripts-3.7/pyvenv to build/scripts-3.7/pyvenv-3.7
/usr/bin/install -c -m 644 ./Tools/gdb/libpython.py python-gdb.py
arm-linux-gnueabihf-gcc -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall    -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration   -I. -I./Include    -DPy_BUILD_CORE -o Programs/_testembed.o ./Programs/_testembed.c
arm-linux-gnueabihf-gcc     -Xlinker -export-dynamic -o Programs/_testembed Programs/_testembed.o libpython3.7m.a -lcrypt -lpthread -ldl  -lpthread -lutil   -lm  
sed -e "s,@EXENAME@,/home/kyq/python/Python-3.7.5/pyinstall/bin/python3.7m," < ./Misc/python-config.in >python-config.py
LC_ALL=C sed -e 's,\$(\([A-Za-z0-9_]*\)),\$\{\1\},g' < Misc/python-config.sh >python-config

出0入4汤圆

 楼主| 发表于 2020-4-8 10:40:52 | 显示全部楼层
我编译了3.7.5  2.7.3 都出现了这样的问题 编译通过 但是ctypes 模块没有添加进入。。。 看了很多帖子 都是说要安装什么libffi-dev   安装过后 没有 任何反应。。不知道还要做些什么。。。

出0入0汤圆

发表于 2020-4-8 10:53:11 | 显示全部楼层
貌似libffi-dev装完后,要ldconfig重新加载,然后再重编译安装python
参考这个: https://stackoverflow.com/questi ... lue-from-module-mul

出0入119汤圆

发表于 2020-4-8 11:06:44 | 显示全部楼层
python 3.7.0没有问题

出0入0汤圆

发表于 2020-4-8 19:39:11 | 显示全部楼层
刚试了下,执行正常:

  1. from ctypes import *

  2. value = c_bool(1)
  3. print(value)
复制代码

出0入4汤圆

 楼主| 发表于 2020-4-9 14:55:41 | 显示全部楼层
root@ATK-IMX6U:~# python2.7
Python 2.7.11 (default, Aug 14 2019, 18:06:34)
[GCC 5.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> mylib = CDLL('/home/root/test/libbeep.so')
>>> mylib.beep_on()
Segmentation fault

出0入4汤圆

 楼主| 发表于 2020-4-9 14:56:31 | 显示全部楼层
本帖最后由 kyq_linux 于 2020-4-9 15:06 编辑

$CC ./test/beepApp.c  -fPIC -shared -o ./test/libbeep.so


beepApp.c
内容如下


int beep_on(void);
int beep_off(void);
int beep_on()
{
...
}
int beep_off()
{
...
}

出0入4汤圆

 楼主| 发表于 2020-4-9 14:57:33 | 显示全部楼层
本帖最后由 kyq_linux 于 2020-4-9 15:07 编辑

>>> mylib.beep_on()
Segmentation fault

调用so库中的beep_on 出现 Segmentation fault
是我用的有问题吗。。。。

出0入4汤圆

 楼主| 发表于 2020-4-9 15:11:13 | 显示全部楼层
liansh2002 发表于 2020-4-8 10:53
貌似libffi-dev装完后,要ldconfig重新加载,然后再重编译安装python
参考这个: https://stackoverflow.com ...

好的蟹蟹 ! 已经能正常用了
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-24 10:49

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表