我试图用ctypes将从C源编译的共享库加载到Python中.共享库(下面名为" libsub.so ")使用了libusb库.这就是"make"所做的:
gcc -c -O2 -Wall -Werror -g -I../src -I../boot/vnd/fw -I. -fPIC -DLIBUSB_1_0 -I/usr/include/libusb-1.0 -o libsub.o libsub.c
gcc -shared -Wl,-soname,libsub.so -o libsub.so libsub.o
Run Code Online (Sandbox Code Playgroud)
之后我尝试了Python:
import ctypes
h = ctypes.cdll.LoadLibrary('./libsub.so')
Run Code Online (Sandbox Code Playgroud)
但是,我收到了这样的错误
OSError: ./libsub.so: undefined symbol: libusb_open
我发现"libusb_open"实际上是"/usr/include/libusb-1.0/libusb.h"中libusb头的一个函数,它已经包含在这个库"libsub.c"的源代码中.
在使用ctypes加载C++共享库时,StackExchange中的一些帖子讨论了这种"未定义的符号"错误,并且通过将编译器从gcc更改为g ++来解决问题.但是,我的源代码是用C语言编写的 - 所以它可能是一个不同的情况(实际上我尝试使用g ++编译这个源代码但得到了一堆错误).任何人都可以指出我在这里失踪了吗?谢谢!
我正在使用ctypes,我正在将一个ndarray传递给一个c函数.它给了我奇怪的输出行为.下面是一些代码:
C-功能:
int foo(int * foo,int N){
for(int i=0;i<N;i++){
cout << "i " << i << " "<< foo[i] << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
蟒蛇:
from ctypes import *
import numpy as np
bar = cdll.LoadLibrary(".../libtest.so")
N = c_int(10)
check = np.ones(10, dtype=int)
print check
bar.foo(c_int(check.ctypes.data),N)
Run Code Online (Sandbox Code Playgroud)
输出:
[1 1 1 1 1 1 1 1 1 1]
i:0 out:1
i:1 out:0
i:2 out:1
i:3 out:0
i:4 out:1
i:5 out:0
i:6 out:1
i:7 out:0
i:8 out:1
i:9 out:0
Run Code Online (Sandbox Code Playgroud)
一切都对吗?:)
我正在编译 …
我正在尝试修改Brandon Rhodes代码例程,它检查CPython字典的内部,以便它适用于CPython 3.3.
我相信我已成功翻译了这个结构.
typedef PyDictKeyEntry *(*dict_lookup_func)
(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject ***value_addr);
struct _dictkeysobject {
Py_ssize_t dk_refcnt;
Py_ssize_t dk_size;
dict_lookup_func dk_lookup;
Py_ssize_t dk_usable;
PyDictKeyEntry dk_entries[1];
};
Run Code Online (Sandbox Code Playgroud)
我认为以下看起来很好:
from ctypes import Structure, c_ulong, POINTER, cast, py_object, CFUNCTYPE
LOOKUPFUNC = CFUNCTYPE(POINTER(PyDictKeyEntry), POINTER(PyDictObject),
py_object, c_ulong, POINTER(POINTER(py_object)))
class PyDictKeysObject(Structure):
"""A key object"""
_fields_ = [
('dk_refcnt', c_ssize_t),
('dk_size', c_ssize_t),
('dk_lookup', LOOKUPFUNC),
('dk_usable', c_ssize_t),
('dk_entries', PyDictKeyEntry * 1),
]
PyDictKeysObject._dk_entries = PyDictKeysObject.dk_entries
PyDictKeysObject.dk_entries = property(lambda s:
cast(s._dk_entries, POINTER(PyDictKeyEntry * s.dk_size))[0]) …Run Code Online (Sandbox Code Playgroud) 如何使用python更改笔记本电脑上的主音量?我知道一种方法,我使用ctypes模拟音量增大/音量减小按键,但不知道当前的音量,每当我启动应用程序进行校准时,我必须让我的代码执行50次连续的音量低调按键它归零.
有没有办法让我获得当前系统音量,或者更好,将主音量设置为指定值?
我正在使用Python 3.4运行Windows 8 64位系统.
我曾经从 Windows 命令行运行 Python 脚本,并且所有打印都在同一个控制台中打印。现在我的机器(Windows 10)上发生了一些事情,当我从命令行启动Python脚本(即打开命令提示符并运行python <my_script.py>)时,Windows会打开一个新窗口(标题为python.exe的绝对路径)。该窗口在执行结束时自动关闭,因此我看不到输出。
如何返回到运行脚本的同一命令提示符窗口中打印输出?
大约半个小时想"我做错了什么!?" 在5行代码..因为Python3以某种方式舍入大整数.任何人都知道为什么会出现这样的问题:
Python2:
int(6366805760909027985741435139224001 # This is 7**40.
/ 7) == 909543680129861140820205019889143 # 7**39
Run Code Online (Sandbox Code Playgroud)
Python3:
int(6366805760909027985741435139224001
/ 7) == 909543680129861204865300750663680 # I have no idea what this is.
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个程序,用于在 Windows 上安装应用程序(如果尚未安装)。我知道可执行文件的文件名,但一无所知。我想查询操作系统以检查该操作系统上是否安装了已知名称或文件名的应用程序。
到目前为止我所拥有的只是以下内容:
def IsProgramInstalled(ProgramName):
"""Check whether ProgramName is installed."""
Run Code Online (Sandbox Code Playgroud)
如果有人知道这个问题的答案,我将不胜感激,因为我在任何地方都找不到它。
有没有人知道我可以查看的方法或资源,以便能够检查我在任务计划程序中拥有的所有 Windows 任务的状态?我想看看任务是失败还是成功。我想在 Python 中做到这一点。
我已经看了一点使用 win32com.client 模块。我可以看到任务是什么,但无法找到已完成工作的状态。
import win32com.client
scheduler = win32com.client.Dispatch("Schedule.Service")
scheduler.Connect()
tasks = scheduler.GetRunningTasks(1)
names = [tasks.Item(i+1).Name for i in range(tasks.Count)]
print names
Run Code Online (Sandbox Code Playgroud) 因此,我不久前正在开发一个批处理文件游戏,由于某种原因,它打印了一个问号符号而不是笑脸符号(当您在命令提示符中按 Ctrl+A 时会得到该符号)。
我对其他符号也有同样的问题:
ctrl+a Smiley face
ctrl+b Dark smiley face
ctrl+d Diamond
ctrl+e Clover
ctrl+f Ace
ctrl+n Music symbol
crtl+o Some random circlish symbol
ctrl+p Left arrow
ctrl+q Right arrow
ctrl+r Up and Down arrows
ctrl+t Line wrap symbol
ctrl+u Double s
ctrl+v Thick underscore
ctrl+w Up and down arrow with a line at the bottom
ctrl+x Up arrow
ctrl+y Down arrow
Run Code Online (Sandbox Code Playgroud)
来源自 Instructables 文章:How do you usespecialchars in cmd
我目前运行的是带有最新更新的 Windows 10 Pro。我试图找到解决这个问题的方法,但没有成功。任何帮助,将不胜感激。
我正在尝试使用 pyinstaller 使我的 python 代码更加用户友好,直到昨天,它还在工作。今天运行时出现以下错误:
C:\Users\user\Desktop\PythonProjects38>pyinstaller --onefile TransmittalEXE.py
83 INFO: PyInstaller: 4.0
83 INFO: Python: 3.8.5
83 INFO: Platform: Windows-10-10.0.18362-SP0
84 INFO: wrote C:\Users\user\Desktop\PythonProjects38\TransmittalEXE.spec
...
31623 INFO: checking PKG
31623 INFO: Building PKG because PKG-00.toc is non existent
31628 INFO: Building PKG (CArchive) PKG-00.pkg
41852 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
41915 INFO: Bootloader c:\users\user\desktop\pythonprojects38\venv\lib\site-packages\PyInstaller\bootloader\Windows-32bit\run.exe
41915 INFO: checking EXE
41917 INFO: Building EXE because EXE-00.toc is non existent
41921 INFO: Building EXE from EXE-00.toc
41927 INFO: Updating manifest in …Run Code Online (Sandbox Code Playgroud) python ×9
windows ×6
ctypes ×3
python-3.x ×3
batch-file ×1
c ×1
dictionary ×1
largenumber ×1
numpy ×1
pyinstaller ×1
python-2.7 ×1
rounding ×1
windows-8 ×1