简体   繁体   中英

Python cannot import name serial

I'm trying to communicate with my Arduino over serial using Python. I've installed pyserial, and this is my code.

#!/usr/bin/env python
from serial import serial
print("helloworld")
ser=serial.Serial('/dev/ttyACM0',9600)

a=raw_input("enter value")
ser.write(a) 

When I try to run the code this is What I get.

Traceback (most recent call last):
  File "/home/vm/Desktop/serial.py", line 2, in <module>
    from serial import serial
  File "/home/vm/Desktop/serial.py", line 2, in <module>
    from serial import serial
ImportError: cannot import name serial

You've named your script serial . It's trying to import serial from itself. Rename your script.

or:

import serial
ser = serial.Serial('/dev/ttyACM0',9600)

when doing

from a import b

you are trying to import member b from module a.

when doing

import a

you are importing the whole module a.

good luck

嗨,您有太多使用下面的代码

from serial import Serial

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