1

Trying to send a message from my Pi to the Arduino via serial communication and I'm receiving this error despite having installed pyserial ImportError: cannot import name 'Serial' from 'serial' (/home/pi/serial.py)

When I change my code to import serial instead of from serial import Serial, the same error presents.

Wondering if anyone else has had this issue and you resolved it.

from serial import Serial
import time

if __name__ == '__main__':
    ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
    ser.flush()

while True:
    ser.write(b"Hello from Raspberry Pi!\n")
    line = ser.readline().decode('utf-8').rstrip()
    print(line)
    time.sleep(1)
4
  • 2
    Not Pi specific. No details of the name of your Python module, nor of its contents. If you edit these details into your question it may reveal an obvious error. – joan Sep 20 '20 at 16:30
  • Other thing to check is if you used pip or pip3 to install it. PIP is for Python 2.x and PIP3 is for Python 3.x. Remember on a Pi the command python fires up v2 still :-( – user115418 Sep 20 '20 at 16:51
  • Ah thanks will upload details – patriciajlim Sep 20 '20 at 20:22
  • Thing is I installed pyserial on all version of python (tried both commands pip and pip3. But I will double check – patriciajlim Sep 20 '20 at 20:24
1

It looks like your code file is named serial.py. Change it to something else!

If you name a file in the current working directory after a module, then Python will use that file in your current working directory than the modules installed.

3
  • Thanks. Changed the name but receiving the same error – patriciajlim Sep 20 '20 at 20:22
  • After changing the name it returns ImportError: cannot import name Serial – patriciajlim Sep 20 '20 at 20:25
  • 1
    @patriciajlim Remove any file with the name serial.pyc in your working directory. – joan Sep 20 '20 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.