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)
3
  • 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
    Commented Sep 20, 2020 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
    Commented Sep 20, 2020 at 16:51
  • Thing is I installed pyserial on all version of python (tried both commands pip and pip3. But I will double check Commented Sep 20, 2020 at 20:24

1 Answer 1

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 Commented Sep 20, 2020 at 20:22
  • After changing the name it returns ImportError: cannot import name Serial Commented Sep 20, 2020 at 20:25
  • 1
    @patriciajlim Remove any file with the name serial.pyc in your working directory.
    – joan
    Commented Sep 20, 2020 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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