ImportError: cannot import name 'Serial' from 'serial' (unknown location)

Question:

Whenever i execute the code below it gives me following Error:

ImportError: cannot import name 'Serial' from 'serial' (unknown location)

Code:

  from serial import Serial

  arduinodata = Serial('com4',9600)

  print("Enter n to ON LED and f to OFF LED")

  while 1:
    input_data = raw_input()
    print ("You Entered"+input_data)
    
    if (input_data == 'n'): 
        arduinodata.write(b'1')
        print("LED ON")

    if (input_data == 'f'):
        arduinodata.write(b'0')
        print("LED OFF")

I have installed all the required python modules. Like pyserial, pyfirmata etc but it is continuously giving me this error.

Asked By: Kashif Iftikhar

||

Answers:

Most likely missing an

 __init__.py 

file or the module, or the file sub-directory for the module (Serial) is on a different layer than the file executable. Hope that helps :).

Answered By: FishingCode

I encountered the same problem. I first uninstalled pyserial from all pip, pip3, and conda (I had it on all 3), and then re-installed it. It then worked fine for me. Hope that helps!

Answered By: Swati Srivastava

I got exactly this problem, as well. It was caused by the "pyserial" and "serial" libraries installed on per-user basis with pip while pyserial was also installed system-wide (possibly in a different version, using the Linux package manager).

Solution

Removing the per-user version fixed the problem in my case.

Answered By: Ruediger

I had to rename serial.py to something else (serial0.py) in my C:/python39 folder and it fixed the problem.

Answered By: Sam_Carmichael

I got same problem while try to install serrial on rpi4
In this tutorial
http://wiki.ros.org/rosserial_arduino/Tutorials/Arduino%20IDE%20Setup
if you install by:

  • 2.1.1(RECOMMENDED) Installing Binaries on the ROS workstation

It work well
But if you install follow:

  • 2.1.2 Installing from Source onto the ROS workstation

It said : ImportError: cannot import name ‘Serial’ from ‘serial’
I dont know why, but when install by 2.1.1 it worked well.

Answered By: Bui Dinh Ba

Uninstalling serial, pyserial

pip uninstall serial

And

pip uninstall pyserial

Then reinstalling pySerial

pip install pySerial

Worked for me.
Note however that I am using a virtual environment

Answered By: Mafeng Pam