英文:
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解释器的备用路径(如果你没有使用虚拟环境),选择“输入解释器路径”。
步骤2: 如果你正在使用虚拟环境,点击如下所示,vscode会自动为你选择正确的版本。
英文:
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.
step2 : alternative if you are using a venv click as shown below , vs code automatically picks the right version for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论