You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派中Rscript调用Python脚本出现ModuleNotFoundError问题求助

解决Rscript调用Python脚本找不到tweepy模块的问题

问题根源

终端和R交互环境会加载用户的shell配置(比如~/.bashrc),里面可能包含了Python包的路径配置,但Rscript运行时是非交互式shell,不会加载这些配置,导致找不到安装在用户目录下的tweepy模块——哪怕python3路径一致,环境变量还是不一样。

具体解决方案

方案1:给system命令指定PYTHONPATH

先在终端里查tweepy的安装目录:

python3 -c "import tweepy; print(tweepy.__file__)"

输出类似/home/pi/.local/lib/python3.9/site-packages/tweepy/__init__.py,取前面的/home/pi/.local/lib/python3.9/site-packages部分。

然后在R脚本里把这个路径加到PYTHONPATH里再调用:

# 替换成你查到的site-packages路径
py_package_path <- "/home/pi/.local/lib/python3.9/site-packages"
system(sprintf("PYTHONPATH=%s python3 tweetImage.py %s %s", py_package_path, var1, var2))

方案2:把tweepy装到系统级

如果不想折腾路径,直接用sudo把tweepy装到系统Python环境里,这样所有运行环境都能访问:

sudo python3 -m pip install tweepy

方案3:在Python脚本里手动加路径

在tweetImage.py开头加上这段代码,把tweepy所在的site-packages路径添进去:

import sys
# 替换成你查到的路径
sys.path.append("/home/pi/.local/lib/python3.9/site-packages")
import tweepy
# 后续脚本代码

内容的提问来源于stack exchange,提问作者Lucius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 08:52:06