xkonti
Posts: 5
Joined: Tue Apr 29, 2014 8:30 pm

Can't use pySerial

Tue Apr 29, 2014 8:38 pm

Hello! For few hours i'm trying to communicate my Raspberry Pi with Arduino Due by serial port. Unfortunately I cant get pySerial module to work.

I installed pySerial:

Code: Select all

pi@xkontipi ~ $ sudo apt-get install python-serial
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
  python-wxgtk2.8 python-wxgtk2.6 python-wxgtk
The following NEW packages will be installed:
  python-serial
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/79.0 kB of archives.
After this operation, 483 kB of additional disk space will be used.
Selecting previously unselected package python-serial.
(Reading database ... 72308 files and directories currently installed.)
Unpacking python-serial (from .../python-serial_2.5-2.1_all.deb) ...
Setting up python-serial (2.5-2.1) ...
And then i wanted to run this Python script:

Code: Select all

import serial

port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=3.0)

while True:
    port.write("\r\nSay something:")
    rcv = port.read(10)
    port.write("\r\nYou sent:" + repr(rcv))
And that is console result:

Code: Select all

pi@xkontipi ~ $ python serial.py
Traceback (most recent call last):
  File "serial.py", line 1, in <module>
    import serial
  File "/home/pi/serial.py", line 3, in <module>
    port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=3.0)
AttributeError: 'module' object has no attribute 'Serial'
I have no idea what am I doing wrong or what is wrong. I'm new to Linux and Python...

Can you help me fix it?

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Can't use pySerial

Wed Apr 30, 2014 6:17 am

Change the name of your own script so that it does not have the same name as the module you are trying to import.

Something like -

Code: Select all

mv serial.py my_serial.py
python my_serial.py
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

xkonti
Posts: 5
Joined: Tue Apr 29, 2014 8:30 pm

Re: Can't use pySerial

Wed Apr 30, 2014 6:44 am

DeeJay wrote:Change the name of your own script so that it does not have the same name as the module you are trying to import.

Something like -

Code: Select all

mv serial.py my_serial.py
python my_serial.py
Unfortunatelly it does not work :/

Code: Select all

pi@xkontipi ~ $ python my_serial.py
Traceback (most recent call last):
  File "my_serial.py", line 1, in <module>
    import serial
  File "/home/pi/serial.py", line 3, in <module>
AttributeError: 'module' object has no attribute 'Serial'

riklaunim
Posts: 265
Joined: Tue Apr 22, 2014 7:34 pm

Re: Can't use pySerial

Wed Apr 30, 2014 6:47 am

Remove serial.pyc file or __pycache__ directory if they exist.

xkonti
Posts: 5
Joined: Tue Apr 29, 2014 8:30 pm

Re: Can't use pySerial

Wed Apr 30, 2014 6:55 am

riklaunim wrote:Remove serial.pyc file or __pycache__ directory if they exist.
Does not work too. Same result as always.

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Can't use pySerial

Wed Apr 30, 2014 8:18 am

Code: Select all

pi@xkontipi ~ $ python my_serial.py
Traceback (most recent call last):
  File "my_serial.py", line 1, in <module>
    import serial
  File "/home/pi/serial.py", line 3, in <module>
AttributeError: 'module' object has no attribute 'Serial'
File "/home/pi/serial.py", line 3, in <module>
There is a file named 'serial.py' in your home directory. Remove it or rename it.
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Can't use pySerial

Wed Apr 30, 2014 8:48 am

Just to prove that there is unlikely to be anything wrong with the python-serial library you have installed...

Code: Select all

pi@raspberrypi ~ $ sudo python
Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=3.0)
>>> print port.name
/dev/ttyAMA0
>>>
pi@raspberrypi ~ $ ls -al /dev/ttyAMA0
crw-rw---- 1 root tty 204, 64 Apr  9 06:17 /dev/ttyAMA0
pi@raspberrypi ~ $
(Python has to be run with privilege because the console port you are using is owned by the admin user 'root'. That has nothing to do with the initial error you have encountered.)
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

xkonti
Posts: 5
Joined: Tue Apr 29, 2014 8:30 pm

Re: Can't use pySerial

Wed Apr 30, 2014 8:59 am

Looks like now everything is working well. All I needed to do:
1. Rename script from serial.py to something else
2. Remove __pycache__ folder
3. After deletion of folder, serial.pyc was silently created so I deleted it too.

Now mu Arduino can talk with Raspberry :)
Thank you for your help.

Return to “Python”