GNURadio自定义Python OOT块导入失败问题求助
问题描述
我是GNURadio自定义OOT块新手,正在制作查找表(lookup table)块,按官方教程操作。模块编译安装成功,文件安装在/usr/local/lib/目录下,但在GNU Radio Companion中刷新块并运行后,出现ImportError:无法从gnuradio导入DABPhaseRef。尝试设置PYTHONPATH后问题仍未解决,使用Manjaro Linux系统。
错误信息
Generating: '/home/matterhorn/gnu_radio/lookup_test.py' Executing: /usr/bin/python3 -u /home/matterhorn/gnu_radio/lookup_test.py Traceback (most recent call last): File "/home/matterhorn/gnu_radio/lookup_test.py", line 27, in <module> from gnuradio import DABPhaseRef ImportError: cannot import name 'DABPhaseRef' from 'gnuradio' (/usr/lib/python3.10/site-packages/gnuradio/__init__.py) >>> Done (return code 1)
自定义块Python代码(lookupTable.py)
#!/usr/bin/env python # -*- coding: utf-8 -*- # # Copyright 2023 Staš Bucik. # # SPDX-License-Identifier: GPL-3.0-or-later # import numpy as np from gnuradio import gr class lookupTable(gr.sync_block): """ docstring for block lookupTable """ def __init__(self): gr.sync_block.__init__(self, name="lookupTable", in_sig=None, out_sig=[(np.complex64, 2048)]) self.kinTable = # long lookup table self.timePhaseTable = # long lookup table self.table = [] for k in range(-1024, 1024): '''if k < -768 or k > 768 or k == 0: table[k + 1024] = 0. + 0.j else: kin = self.getKINParams(k) h = self.timePhaseTable[kin[1], k - kin[0]] phase = np.pi * 0.5 * (h + kin[2]) table[k + 1024] = np.exp(1.j * phase)''' table[k + 1024] = k + 0.j def getKINParams(self, k): for row in self.kinTable: if k >= row[0] and k <= row[1]: return row[2:] raise Exception("No KIN parameters found!") def work(self, input_items, output_items): output_items[0] = self.table return len(output_items[0])
GRC块配置YAML文件(DABPhaseRef_lookupTable.block.yml)
id: DABPhaseRef_lookupTable label: lookupTable category: '[DABPhaseRef]' templates: imports: from gnuradio import DABPhaseRef make: DABPhaseRef.lookupTable() # Make one 'inputs' list entry per input and one 'outputs' list entry per output. # Keys include: # * label (an identifier for the GUI) # * domain (optional - stream or message. Default is stream) # * dtype (e.g. int, float, complex, byte, short, xxx_vector, ...) # * vlen (optional - data stream vector length. Default is 1) # * optional (optional - set to 1 for optional inputs. Default is 0) outputs: - label: out0 domain: stream dtype: complex vlen: 2048 # 'file_format' specifies the version of the GRC yml format used in the file # and should usually not be changed. file_format: 1
安装日志
Install the project... -- Install configuration: "Release" -- Up-to-date: /usr/local/lib/cmake/gnuradio-DABPhaseRef/gnuradio-DABPhaseRefConfig.cmake -- Up-to-date: /usr/local/include/gnuradio/DABPhaseRef/api.h -- Up-to-date: /usr/local/lib/python3.10/site-packages/gnuradio/DABPhaseRef/__init__.py -- Up-to-date: /usr/local/lib/python3.10/site-packages/gnuradio/DABPhaseRef/lookupTable.py -- Installing: /usr/local/lib/python3.10/site-packages/gnuradio/DABPhaseRef/__init__.pyc -- Installing: /usr/local/lib/python3.10/site-packages/gnuradio/DABPhaseRef/lookupTable.pyc -- Installing: /usr/local/lib/python3.10/site-packages/gnuradio/DABPhaseRef/__init__.pyo -- Installing: /usr/local/lib/python3.10/site-packages/gnuradio/DABPhaseRef/lookupTable.pyo -- Up-to-date: /usr/local/share/gnuradio/grc/blocks/DABPhaseRef_lookupTable.block.yml
解决方案
1. 确保Python路径包含/usr/local的site-packages
Manjaro中Python默认优先加载/usr/lib下的gnuradio,而自定义模块安装在/usr/local/lib/python3.10/site-packages下。临时修复可在终端运行:
export PYTHONPATH="/usr/local/lib/python3.10/site-packages:$PYTHONPATH"
之后再启动GNU Radio Companion运行流图。
若要永久生效,将上述命令添加到shell配置文件(/.bashrc、/.zshrc等),再重新加载配置:
source ~/.bashrc # bash用户 # 或 source ~/.zshrc # zsh用户
2. 检查DABPhaseRef模块的__init__.py
打开/usr/local/lib/python3.10/site-packages/gnuradio/DABPhaseRef/__init__.py,确保正确导出lookupTable类,添加以下内容:
from .lookupTable import lookupTable __all__ = ['lookupTable']
这样才能通过from gnuradio import DABPhaseRef后调用DABPhaseRef.lookupTable()。
3. 验证模块导入是否正常
在终端启动Python3,执行以下命令测试:
import sys sys.path.insert(0, "/usr/local/lib/python3.10/site-packages") from gnuradio import DABPhaseRef # 测试实例化块 block = DABPhaseRef.lookupTable()
若无报错,说明模块导入正常,此时再运行GRC流图即可。
4. 修复Python代码中的索引错误
lookupTable类中self.table初始化为空列表,直接用table[k + 1024]赋值会触发索引错误,需先初始化列表长度:
self.table = [0+0j] * 2048 # k从-1024到1023,共2048个元素
内容的提问来源于stack exchange,提问作者Staš Bucik

