简体   繁体   中英

Code working in python IDLE but not in visual studio?

I'm setting up a serial connection between my arduino and python on my laptop. (The code is supposed to send something to the arduino and then the arduino turns on a light) The code works if I test it inside the python IDLE, the light just turns on like it supposed to. But if i copy the exact same code to visual studio and run it, the light doesn't turn on, why? Visual studio also doesn't show any error messages or something.

I tried to run the code inside PyCharm but it also won't work.

Code:

import serial

s = serial.Serial('COM5', 9600)

s.write("s".encode())

UPDATE: This is weird. If i change the code so that the light is constantly on and if it recieves something through the serial it turns off, then it works. I think the problem is if it recieves something it will just always turn off the light. But why does it work only through the python IDLE and not other programs?

Your code is using a different Python interpreter for VS Code and IDLE.

Please check using

import sys
print(sys.executable)

Read here on how to set the Python interpreter in VS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM