(21)python开发经验 --- python代码将dll添加进临时环境变量

📅 发布时间:2026/7/10 19:06:56 👁️ 浏览次数:
(21)python开发经验 --- python代码将dll添加进临时环境变量
文章目录[toc]1、pycharm插件离线安装2、python实现类似C中将结构体转为二进制的功能3、python代码将dll添加进临时环境变量更多精彩内容内容导航 Qt开发 python开发 1、pycharm插件离线安装PyCharm Plugins and Themes | JetBrains Marketplace下载插件下载后是一个zip包打开【文件】【设置】选中【插件】点击设置图标【从磁盘安装插件】2、python实现类似C中将结构体转为二进制的功能在C/C中传递数据中传递数据常用方式为struc Frame{inta;intb;};Frame frame;char*bufnew[sizeof(Frame)];memcpy(buf,frame,sizeof(Frame));在python中不能直接操作内存也没有memcpy可以使用其他方式实现类似功能importnumpyasnpclassCmdHead:def__init__(self):self.headnp.uint32(0x11223344)self.lengthnp.uint32(0)self.idnp.uint32(0)classCmdBody:def__init__(self,cmd_id0,address0,length0):self.cmd_idnp.uint32(cmd_id)self.addressnp.uint64(address)self.lengthnp.uint64(length)deftoBody(self,frame_buf):self.cmd_idint.from_bytes(frame_buf[0:4],little)self.addressint.from_bytes(frame_buf[4:12],little)self.lengthint.from_bytes(frame_buf[12:20],little)defget_cmd_id():# 初始化静态变量ifnothasattr(get_cmd_id,count):get_cmd_id.count0get_cmd_id.count1returnget_cmd_id.countdefbuild_frame(body):frame_len0forkey,valueinvars(body).items():ifisinstance(value,np.uint32):frame_len4elifisinstance(value,np.uint64):frame_len8else:print(Error1:key)headCmdHead()head.lengthnp.uint32(frame_len8)head.idnp.uint32(get_cmd_id())frame_bufhead.head.tobytes()head.length.tobytes()head.id.tobytes()# 遍历class中所有遍历并转换为字节流for_,valueinvars(body).items():ifisinstance(value,np.uint32)orisinstance(value,np.uint64):frame_bufvalue.tobytes()else:print(Error2)returnframe_buf# 封装命令read_efuseCmdBody(0xaa112233,0x1000,16)erase_rangeCmdBody(0xaa112244,0x2000,100)bufbuild_frame(read_efuse)print(buf.hex( ))bufbuild_frame(erase_range)print(buf.hex( ))defframe_type(frame_buf):cmd_idint.from_bytes(frame_buf[0:4],little)ifcmd_id0xaa112233:cmd_bodyCmdBody()cmd_body.toBody(frame_buf)print(hex(cmd_body.cmd_id),hex(cmd_body.address),cmd_body.length)elifcmd_id0xaa112244:cmd_bodyCmdBody()cmd_body.toBody(frame_buf)print(hex(cmd_body.cmd_id),hex(cmd_body.address),cmd_body.length)else:returnunknownframe_type(buf[12:])3、python代码将dll添加进临时环境变量这段代码主要用于动态修改当前进程的环境变量PATH以便程序能够加载指定目录下的动态链接库或可执行文件修改os.environ仅对当前 Python 进程及其子进程有效不会永久修改系统环境变量。importosdefadd_dll_directory(dll_path): 将DLL目录添加到环境变量中 ifos.path.exists(dll_path):# 添加到PATH环境变量开头print(os.pathsep)os.environ[PATH]dll_pathos.pathsepos.environ.get(PATH,)print(f已添加{dll_path}到环境变量)else:print(f路径{dll_path}不存在)add_dll_directory(./)