在 Thonny 中运行的 Python3 on RPi4 的 “import picamera2” 命令在 VS Code 中失败。

huangapple go评论147阅读模式
英文:

Python3 on RPi4 "import picamera2" command works in Thonny but fails in VS Code

问题

I'm running a Python3 program to record a small video on a Raspberry Pi 4B:

import time
from picamera2 import Picamera2
from picamera2.encoders import H264Encoder

picam2 = Picamera2()
video_config = picam2.create_video_configuration()
picam2.configure(video_config)

encoder = H264Encoder(10000000)
picam2.start_recording(encoder,'/home/pi/test.h264')
time.sleep(10)
picam2.stop_recording()

When I run this program in VS_Code I receive an error message "no module named picamera2". When I run the same program in Thonny it records a video. Executing import picamera2 in the terminal works also. I like working in VS_Code. Does anyone have an idea why it is reporting an error? Thanks, Jim.

英文:

I'm running a Python3 program to record a small video on a Raspberry Pi 4B:

` import time

from picamera2 import Picamera2
from picamera2.encoders import H264Encoder

picam2 = Picamera2()
video_config = picam2.create_video_configuration()
picam2.configure(video_config)

encoder = H264Encoder(10000000)
picam2.start_recording(encoder,'/home/pi/test.h264')
 time.sleep(10)
picam2.stop_recording()`

When I run this program in VS_Code I receive an error message "no module named picamera2". When I run the same program in Thonny it records a video. Executing import picamera2 in the terminal works also. I like working in VS_Code. Does anyone have an idea why it is reporting an error? Thanks, Jim.

答案1

得分: 1

如果你在vscode上点击运行按钮,应该指定适当的Python解释器。如果你在终端中运行,你知道你安装了所有库的解释器。

要解决这个问题,你只需要在vscode中选择正确的解释器:

如果你创建了虚拟环境并正在使用它,可以按照以下步骤操作:

步骤1: 点击vscode右下角显示的Python版本。

步骤2: 如果你想指定手动选择Python解释器的备用路径(如果你没有使用虚拟环境),选择“输入解释器路径”。

在 Thonny 中运行的 Python3 on RPi4 的 “import picamera2” 命令在 VS Code 中失败。

步骤2: 如果你正在使用虚拟环境,点击如下所示,vscode会自动为你选择正确的版本。

在 Thonny 中运行的 Python3 on RPi4 的 “import picamera2” 命令在 VS Code 中失败。

英文:

If you are pressing the run button on the vscode then the appropriate python interpreter should be pointed. if you are running in terminal u know for which interpreter u have installed all the libraries.

To get this solved u just have to choose the right interpreter in the vs code:

you can follow the below step if u have created a venv and using it:

step 1: click on the python version displayed on the right botton corner of your vscode.

step2: select the enter interpreter path if you want to specify manual alternate path of the python interpreter if you are not using venv.

在 Thonny 中运行的 Python3 on RPi4 的 “import picamera2” 命令在 VS Code 中失败。

step2 : alternative if you are using a venv click as shown below , vs code automatically picks the right version for you.

在 Thonny 中运行的 Python3 on RPi4 的 “import picamera2” 命令在 VS Code 中失败。

huangapple
  • 本文由 发表于 2023年3月9日 13:52:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定