0

In my code I try to interact with some equipment using the serial module:

from serial import serial

ser = serial.Serial('/dev/ttyUSB0')  # open serial port

print(ser.name)         # check which port was really used

ser.write(b'hello')     # write a string

ser.close()

But I get this error:

cannot import name 'serial' from 'serial' (unknown location)

I searched on Stack Overflow for this error message but I could not find an answer which resolves my problem.

Based on what I found, I tried:

  • pip install serial,
  • pip install pyserial,
  • and also unistall and reinstall.

How can I fix this error?

Ivo Mori
  • 2,177
  • 5
  • 24
  • 35
gijae LEE
  • 3
  • 2
  • 1
    I would like to know what you uninstall and reinstalled. Serial and Pyserial from my experience , these both cant get along that installed together. either one have to go. – 钟智强 Jul 24 '20 at 02:58
  • It may be just using correct case: Try `from serial import Serial` and when you use it, use: `ser = Serial('/dev/ttyUSB0') # open serial port` – RufusVS Jul 24 '20 at 03:18
  • 1
    Does this answer your question? [Python serial without pyserial](https://stackoverflow.com/questions/20542858/python-serial-without-pyserial) – 钟智强 Jul 27 '20 at 01:33
  • @RufusVS Thanks for your command But I'm can't understand what you said. That mean is right? or something need to fix? ++ Aha! I try to this now – gijae LEE Jul 27 '20 at 02:26
  • @JohnMelodyMelissa Thanks to your answer. But I've already installed and uninstalled it both together...And I saw the linked qeustion but It;s little bit different with my problem.. I reallty appreciate. – gijae LEE Jul 27 '20 at 02:28
  • Actually like I mentioned previously, You cant use serial and pyserial together(this confuses the compiler). Either you use one or the other. usually I use ```pip3 install serial``` and COMPLETELY (I mean COMPLETELY) uninstall pyserial. ```sudo pip3 uninstall pyserial``` alternatively, You can download from the source and install it at the {directory} of your pip will be. This from my experience of the same problem. – 钟智强 Jul 27 '20 at 09:00
  • else you can compile it from the [Source] (https://github.com/pyserial/pyserial) and copy it to your pip {directory} – 钟智强 Jul 27 '20 at 09:08

1 Answers1

0

Try it this way:

from serial import Serial  # note the capital S change

ser = Serial('/dev/ttyUSB0')  # open serial port

print(ser.name)         # check which port was really used

ser.write(b'hello')     # write a string

ser.close()
RufusVS
  • 4,008
  • 3
  • 29
  • 40
  • OMG Master Rufus!! I got solution from you :). How can I express this pleasure I'm really Thanks for your help. Could I question one more? What is different between Serial and serial.Sreial? – gijae LEE Jul 30 '20 at 02:58
  • if you import using `from serial import Serial`, it puts the Serial class into your name space so you can use it as I show. if you imported the module using: `import serial`, to use the class you would need `ser = serial.Serial(...` – RufusVS Jul 30 '20 at 04:25
  • AHA!! I really appreciate sir. I have to study more step by step @@!! – gijae LEE Aug 07 '20 at 06:03