Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import RKNN inside Jupiter fails with the message 'NameError: name 'exit' is not defined' #342

Open
nassimus26 opened this issue Oct 13, 2024 · 6 comments

Comments

@nassimus26
Copy link

nassimus26 commented Oct 13, 2024

I am using the last version with Python 3.12
The import works well in a Python file but fail in the Jupiter Notebook

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[65], line 2
      1 import sys
----> 2 from rknn.api import RKNN

File ~/miniconda3/envs/ai/lib/python3.12/site-packages/rknn/api/__init__.py:1
----> 1 from rknn.api.rknn import RKNN

File ~/miniconda3/envs/ai/lib/python3.12/site-packages/rknn/api/rknn.py:7
      5 from .rknn_log import set_log_level_and_file_path
      6 from .rknn_platform import get_host_os_platform, get_librknn_api_require_dll_dir
----> 7 from .rknn_base import RKNNBase
      8 from argparse import Namespace
     10 already_imported = False

File rknn/api/rknn_base.py:24, in init rknn.api.rknn_base()

File rknn/api/graph_optimizer.py:1, in init rknn.api.graph_optimizer()

Screenshot from 2024-10-13 23-13-07
Screenshot from 2024-10-13 23-20-12

@nassimus26 nassimus26 changed the title import RKNN inside Pycharm Jupiter Notebook fails with the message 'NameError: name 'exit' is not defined' import RKNN inside Jupiter fails with the message 'NameError: name 'exit' is not defined' Oct 13, 2024
@hellobo2802
Copy link

hellobo2802 commented Nov 28, 2024

Has this question been solved? I meet the same situation when I use “from rknn.api import RKNN” in google colab!
Interesting, it is used rightly before a week .

@oliver-space
Copy link

I am also facing this question. Has a solution been determined?

@oliver-space
Copy link

I am also facing this question. Has a solution been determined?

It's tricky. This problem only happens on jupyter notbook. If I run the same code on .py file, everything is good.

@Centauria
Copy link

Centauria commented Mar 6, 2025

What is even trickier, is that I run from rknn.api import RKNN in ipython with python 3.12 in a fresh install, I recieve no error.

BUT if I open ipython the second time, it will raise exactly this error.

--force-reinstall ,
or remove the environment and reinstall everything,
reboot,
nothing can resolve it.

@Centauria
Copy link

Deepseek gave me this solution:

  1. 官方已出现过的类似问题Rockchip 的 rknn-toolkit 系列,因依赖多种底层库(如 OpenCL、RGA、各种驱动),在 Python 3.12 这种相对新的版本上,可能有不少尚未完全测试的场景。有时你会看到类似报错:第一次运行没问题
    第二次及以后出现“no symbol”或“NameError : name 'exit' is not defined”
    这极可能是官方尚未完全处理的 Bug,需要后续版本修复。可以查看或提交 issue 给 Rockchip,说明你在 Python 3.12 环境下遇到这一情况,并附上你的最小复现方式。

  2. 可能的临时规避方法
    (1) 每次使用后重启 Python
    如果确实必须在 Python 3.12 下使用当前版本的 rknn-toolkit2,并且每次首次导入都能成功,那么 在同一进程内 不要去重复地多次导入、反复调用。可以通过以下思路规避:

将处理逻辑封装进一个脚本,脚本启动后只执行一次“导入 + 构建 + 推理”,然后退出 Python 进程。
如果有批量任务需要处理,就编写一个调度脚本,每处理一批就启动一个子进程去跑,这样子进程退出后可以释放全部资源,不会留下“残留状态”。
(2) 回退到 Python 3.10 / 3.11
如前所述:

Python 3.7~3.11 是官方文档更常见的兼容范围;
如果你换成 Python 3.10/3.11 环境,可能就没有第二次报错的问题了。
(3) 检查或去除内部 exit() 调用(若能获取源码)
如果你能解压或查看 rknn_toolkit2-2.3.0-cp312-cp312-manylinux_2_17_x86_64.whl 的内部文件,找到 rknn/api/rknn_base.py 以及类似 graph_optimizer.py(哪怕它们可能打包在 .zip 文件中),搜索是否有 exit( 这样的交互式调用。若确有之,尝试将其替换成 import sys; sys.exit(...) 并重新打包 Wheel——前提是你熟悉这个操作且官方允许这么做。

@phandyKHD
Copy link

I had this problem in jupyter but not when executing a python script. I added this to my notebook and the import worked fine.

import builtins
builtins.exit = quit  # Manually define `exit`
from rknn.api import RKNN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants