0

I have a Windows 10 with Python 3.7.4 and I use Jupyter Notebook. I'm trying to use pySerial to connect to my Arduino by Serial attribute. I tried installing pySerial simply by pip install pyserial and eventually I tried conda install -c conda-forge pyserial too, but when I try to run my code:

 import serial
 ser = serial.Serial('COM4', 9600)

I get an error message like:

AttributeError                            Traceback (most recent call last)
<ipython-input-7-413d0d9dabe7> in <module>
      2 import serial
      3 import time
----> 4 ser = serial.Serial('COM4', 9600)
      5 
      6 # Your Account Sid and Auth Token from twilio.com/console

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

Also, I tried

import serial
serial.__file__

And I got 'C:\Users\lippe\Anaconda3\lib\site-packages\serial\__init__.py' as output.

I also tried dir(serial) and I can't see the Serial attribute in the output, so I think it's basically not installed and I don't know why. I tried digging the internet but I still can't find a solution.

  • I've looked through the docs but, as unlikely as it is to give this error; did you call your script `serial.py`? If so, you need to rename it – roganjosh Nov 14 '19 at 18:24
  • https://stackoverflow.com/a/11404052/669576 – 001 Nov 14 '19 at 18:25
  • No I didn't call my script serial.py. I'm not supposed to be using serial instead of pyserial either, but everything I read about this error says I'm using the wrong package. I don't understand why as I uninstalled serial several times. I just tried to uninstall it again and I got "Skipping serial as it is not installed". I also tried to run the script in a friend's pc and I got the same errors... Even if serial wasn't installed either. – Maria Thereza Nov 14 '19 at 18:36
  • As always I think the problem is somehow Windows but I can't use Linux right now. I'll try to run my script in a Linux later and I'll let you know. – Maria Thereza Nov 14 '19 at 18:46
  • I solved it by changing my IDE to pyCharm. I still don't know why it didn't work with Anaconda but at least now I have a working script. Thank you guys :) – Maria Thereza Nov 15 '19 at 10:04

1 Answers1

0

Make sure you're importing the right serial. After installing pyserial with pip, you can use the __file__ attribute to check that it's the one I expect like so:

>>> import serial
>>> serial.__file__
'/usr/local/lib/python3.7/site-packages/serial/__init__.py'

Other answers suggest it might work to import the class directly with

from serial import Serial

But I'm not sure why this would matter if you're importing the latest published on pypi.

Andrey Fedorov
  • 9,148
  • 20
  • 67
  • 99