Loonhaunt
Posts: 63
Joined: Sat Nov 16, 2013 5:13 am

How to assure that Python Modules are installed in the correct directory?

Mon Nov 16, 2020 9:26 pm

Hello,

I seem to be having trouble with my Python3 via Thonny finding my serial module, or pyserial for that matter. I am getting an ATTRIBUTEError
for 'Serial' has no module?

Code: Select all

Traceback (most recent call last):
  File "/home/pi/Documents/PyDev/Ser_Tesst_shrt.py", line 4, in <module>
    ser = serial.Serial(port="/dev/ttyAMC0"),
AttributeError: module 'serial' has no attribute 'Serial'
I have both serial and pyserial installed I have uninstalled both and re-installed them individually to see if it makes a difference.

Any suggestion at this point would be great. I don't remember having this issue with Python 2.7?

Dave

User avatar
DougieLawson
Posts: 41310
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to assure that Python Modules are installed in the correct directory?

Mon Nov 16, 2020 10:32 pm

Are you using sudo pip3 install ... to install things?
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Loonhaunt
Posts: 63
Joined: Sat Nov 16, 2013 5:13 am

Re: How to assure that Python Modules are installed in the correct directory?

Mon Nov 16, 2020 10:57 pm

Hi Dougie how have you been?

Yes I am using pip3. I got stuck on this issue a couple of years ago but haven't written much python code since then. I do remember that switching over to pyserial made a difference with the particular code that I was working on at the time but it doesn't seem to matter now? I am at a different machine now but when back at the Rpi I will paste in the actual code if that would be of any help. I am pretty sure I will have some other issues pop up with this code relating to attempting to intercede sensor data coming from Arduino serial.print() , but I can't get past this error to continue debugging.

Thanks so much for your help,

Dave

User avatar
B.Goode
Posts: 12089
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to assure that Python Modules are installed in the correct directory?

Tue Nov 17, 2020 12:01 am

I think it is possible that in /home/pi/Documents/PyDev/ you have a file serial.py which is being imported instead of the standard serial library module.


The import and subsequent use works for me with RasPiOS Buster -

Code: Select all

pi@raspberrypi:~ $ sudo python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> ser = serial.Serial(port="/dev/ttyAMA0")
>>>

hippy
Posts: 9971
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: How to assure that Python Modules are installed in the correct directory?

Tue Nov 17, 2020 1:02 pm

B.Goode wrote:
Tue Nov 17, 2020 12:01 am
I think it is possible that in /home/pi/Documents/PyDev/ you have a file serial.py which is being imported instead of the standard serial library module.
That's my feeling too. The error message indicates some 'serial' module has been found, but it doesn't include a 'def Serial():' -

AttributeError: module 'serial' has no attribute 'Serial'

Loonhaunt
Posts: 63
Joined: Sat Nov 16, 2013 5:13 am

Re: How to assure that Python Modules are installed in the correct directory?

Tue Nov 17, 2020 2:23 pm

Your answers got me started poking over Stackoverflow to see is there were similar issues.
What I did on my windows 10 machine was to

Code: Select all

pip3 uninstall serial
pip3 install pyserial
then with powershell I got the message that I should upgrade pip.

Code: Select all

Installing collected packages: pyserial
Successfully installed pyserial-3.4
WARNING: You are using pip version 20.2.3; however, version 20.2.4 is available.
You should consider upgrading via the 'c:\program files\python39\python.exe -m pip install --upgrade pip' command.
PS C:\WINDOWS\system32> python.exe -m pip install --upgrade pip
Collecting pip
  Downloading pip-20.2.4-py2.py3-none-any.whl (1.5 MB)
     |████████████████████████████████| 1.5 MB 3.2 MB/s
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 20.2.3
    Uninstalling pip-20.2.3:
      Successfully uninstalled pip-20.2.3
Successfully installed pip-20.2.4
I have to think about what this means to my raspberry pi 3 with python3?

This is the result on from IDLE on my labtop

Code: Select all

Connected to Arduino port:COM4
Created file
b'BME680 test\r\
b'72.72,836.74,22.11,0.00,4855.95\r\
b'72.84,836.74,22.18,313.47,4855.95\r\
b'73.04,836.76,22.28,322.65,4856.59\r\
b'73.15,836.74,22.29,326.80,4855.95\r\
b'73.20,836.72,22.28,327.45,4856.59\r\
b'73.26,836.72,22.40,326.48,4855.95\r\
b'73.27,836.74,22.72,325.51,4855.95\r\
b'73.29,836.72,23.02,325.83,4855.95\r\
b'73.31,836.74,23.04,328.10,4855.95\r\
b'73.31,836.72,22.91,329.08,4855.95\r\
Data collection complete!
Here is the code that produced the above result

Code: Select all

import serial

# -- update your project info --

#serial port of Arduino
#on windows it will be "COM5" format
#on mac it will be "/dev/cu.usbmodemxxx" format
arduino_port = "COM4"
baud = 9600 #arduino uno runs at 9600 baud
fileName="analog-data.csv" #name of the CSV file generated
samples = 10 #how many samples do you want to collect?
print_labels = False #do you want to print out line ids to serial?

# -- no need to edit beyond this line!--
ser = serial.Serial(arduino_port, baud)
print("Connected to Arduino port:" + arduino_port)
file = open(fileName, "a")
print("Created file")

line = 0

while line <= samples:
    # incoming = ser.read(9999)
    # if len(incoming) > 0:
    if print_labels:
        if line==0:
            print("Printing Column Headers")
        else:
            print("Line " + str(line) + ": writing...")
    getData=str(ser.readline())
    data=getData[0:][:-2]
    print(data)

    file = open(fileName, "a")

    file.write(data + "\n") #write data with a newline
    line = line+1

print("Data collection complete!")
file.close()

Return to “Python”