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

7

11

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.

Conspire answered 3/2, 2020 at 6:56 Comment(0)
C
1

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 :).

Carburetor answered 3/2, 2020 at 6:59 Comment(0)
J
6

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!

Julide answered 3/2, 2020 at 7:14 Comment(0)
A
4

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.

Attendance answered 8/1, 2021 at 14:52 Comment(3)
@FedericoBaù Which point exactly? The answer gives a specific possible cause for the precise error that has not been covered by the other answers so far (interaction between system package manager and pip). It satisfies the points of all all the paragraphs on the page that you linked to. In particular, "Pay it forward" does not apply, the answer goes beyond the "Have the same problem?" paragraph, satisfies "Answer the question", "Provide context for links" does not apply, "Write to the best of your ability" applies except for one typo, and the other two paragraphs are also satisfied.Attendance
"Write to the best of your ability" I had edited your answer but is still on the review code, so if is accepted you see what you mean. Nothing to be honestly. I highlight what the solution you found is. If you wait my edit to go through you see what I mean. It is written al together and is not understandable at first glance where the solution is.Reborn
Keep in mind that people are in a rush. Image when you have a issue and disparately look for an answer, when you scrub the web you are really fast and only stop when you see something that may help you and attracts . So in this case is only a visual thing, and yes some typo. About the review also, is because you have asked the first time and the first answer is been review so don't take it personal, is for make you better in case you keep progressing.Reborn
C
1

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 :).

Carburetor answered 3/2, 2020 at 6:59 Comment(0)
W
1

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

Worldshaking answered 22/12, 2022 at 3:20 Comment(1)
The question makes no reference to the serial package; only to pyserial.Darnelldarner
M
0

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.

Metts answered 1/6, 2022 at 7:3 Comment(0)
B
0

I somehow got mixed up in using serial and pyserial and also had one of the packages installed for all users on Arch. What I had to do was to delete the virtualenv and re-create it only having pyserial as a package.

Blancheblanchette answered 21/8 at 21:44 Comment(0)
P
-2

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

Parasitology answered 3/2, 2021 at 23:12 Comment(1)
This is highly likely to cause future damage and does not get to the root of the problem anywaysDudleyduds

© 2022 - 2024 — McMap. All rights reserved.