0

Now I am trying to change a program from Java into Python, when I finally fix three problems on my code and I run it. Then shows me this Error.

Traceback (most recent call last):
  File "serial.py", line 4, in <module>
    import serial
  File "/home/pi/serial.py", line , in <module>
    port=serial.Serial(
AttributeError:'module' object has no attribute 'Serial'

How can I fix it?

If you need the source code and What were the problems that I had to fix, let me know. Please help me about it.

rpi-uts
  • 51
  • 1
  • 10

3 Answers3

1

I think import serial is wrong.

Did you try:-

from serial import serial

http://pyserial.sourceforge.net/shortintro.html

gazeranco
  • 55
  • 8
1

Your problem is simple - you have given your script file the same name as the library you are importing.

Fix: rename your file to some other name, e.g. serial.py -> serial_test.py and this should fix the problem.

wzona
  • 126
  • 7
1

You've most likely named your file serial.py or maybe Serial.py. Rename your file to something else, "TestSerial.py" if you're desperate to keep that name.

Golden Rule of Python: Never give a file the same name as a module you are importing from