Python Forum
Serial connection connection issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Serial connection connection issue
#1
Hi, I am not able to connect to serial. I get an error "ModuleNotFoundError: No module named 'serial'"

# Serial port config 
import serial
ser = serial.Serial("COM8", 9600)
  
# Send character 'S' to start the program
ser.write(bytearray('S','ascii'))
Reply
#2
Can you confirm you have installed the pyserial module?

If not, pip install pyserial should do it.
Reply
#3
(Aug-26-2021, 03:04 PM)popejose Wrote: Can you confirm you have installed the pyserial module?

If not, pip install pyserial should do it.

Hi, yes it is installed but I am not able to connect to the serial port.
Reply
#4
(Aug-26-2021, 01:28 PM)Joni_Engr Wrote: Hi, I am not able to connect to serial. I get an error "ModuleNotFoundError: No module named 'serial'"

Hi, yes it is installed but I am not able to connect to the serial port.
This message ModuleNotFoundError is not a connect problem,it's a install package\module problem.
A quick test in a virtual environment install is pip install pyserial.
>>> import serial
>>>
>>> serial.Serial
<class 'serial.serialwin32.Serial'>

# Just to make a ModuleNotFoundError 
>>> import serial99
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'serial99'
Connection/port problem give this message,this is a step future and module works.
>>> ser = serial.Serial('/dev/ttyUSB0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\code\cpu_env\lib\site-packages\serial\serialwin32.py", line 33, in __init__
    super(Serial, self).__init__(*args, **kwargs)
  File "c:\code\cpu_env\lib\site-packages\serial\serialutil.py", line 244, in __init__
    self.open()
  File "c:\code\cpu_env\lib\site-packages\serial\serialwin32.py", line 64, in open
    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port '/dev/ttyUSB0': FileNotFoundError(2, 'Systemet finner ikke angitt bane.', None, 3)

could not open port '/dev/ttyUSB0': FileNotFoundError(2, 'Systemet finner ikke angitt bane.', None, 3)
Reply
#5
Hi, if I run just "import serial" than I get the error "ModuleNotFoundError: No module named 'serial'". I am not sure how to fix the problem. The COM port is shown in the device manager.
Reply
#6
Which OS to you use?
It's a install or not using right version of Python problem,do pip -V
C:\code
λ pip -V
pip 21.1.3 from c:\python39\lib\site-packages\pip (python 3.9)
So now if i install pip install pyserial as shown over it will install to Python 3.9
C:\code
λ pip install pyserial
Collecting pyserial
  Using cached pyserial-3.5-py2.py3-none-any.whl (90 kB)
Installing collected packages: pyserial
Successfully installed pyserial-3.5
Then using the same Python version(3.9) as installed to it will work.
# See same version and path,on Linux use which python
C:\code
λ python -c "import sys;print(sys.executable)"
C:\Python39\python.exe

# For me it works as i use cmder
C:\code
λ which python
/c/Python39/python

# Test that it work
C:\code
λ python
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>>
>>> serial.__version__
'3.5'
>>> exit()
Reply
#7
I am running on Windows 10.
Reply
#8
Start cmd and do this what dos it show?
Example.
C:\>pip -V
pip 21.2.4 from c:\python39\lib\site-packages\pip (python 3.9)

C:\>python -V
Python 3.9.5

C:\>python -c "import sys;print(sys.executable)"
C:\python39\python.exe

C:\>
Reply
#9
Hi, yes it shows the following.

O:\>pip -V
pip 21.1.3 from e:\program_files\python-3.9.6\lib\site-packages\pip (python 3.9)

O:\>python -V
Python 3.9.6

O:\>python -c "import sys;print(sys.executable)"
E:\Program_Files\Python-3.9.6\python.exe
Reply
#10
You should not type python or python3 in your terminal.

First you should look, if you've installed more than one Python Interpreter on your system with py.

This shows all available Python Interpreters without the installations from Microsoft App Store.
py -0
Hint: Avoid the Python Installation from MS App Store. There are also some permission limitations

To address the right Python Interpreter on Windows, you should always use py.exe.

If for example 2.7 and 3.9 is installed, you can choose the right interpreter with py.exe:

py -2.7 # runs Python 2.7 if available
py -3.9 # runs Python 3.9 if available
To install a Package for 3.9 and addressing it explicit:
py -3.9 -m pip install pyserial
Then you start later your program with:
py -3.9 your_program.py
Using the py.exe without specifying the version, the latest Python Interpreter will be used if nothing else was set in the configuration file.
https://docs.python.org/3/using/windows....or-windows
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I don't know what is wrong (Python and SQL connection) shereen 3 358 Apr-01-2024, 08:56 AM
Last Post: Pedroski55
  No Internet connection when running a Python script basil_555 8 619 Mar-11-2024, 11:02 AM
Last Post: snippsat
  Connection LTspice-Python with PyLTSpice bartel90 0 347 Feb-05-2024, 11:46 AM
Last Post: bartel90
  Virtual Env changing mysql connection string in python Fredesetes 0 381 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  Database connection problems [login, PyQt5, PySql] gradlon93 5 724 Dec-15-2023, 05:08 PM
Last Post: deanhystad
  connection python and SQL dawid294 4 674 Dec-12-2023, 08:22 AM
Last Post: Pedroski55
  Input network device connection info from data file edroche3rd 6 1,062 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 4,110 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Networking Issues - Python GUI client and server connection always freezes Veritas_Vos_Liberabit24 0 736 Mar-21-2023, 03:18 AM
Last Post: Veritas_Vos_Liberabit24
  Issues with "connection timed out" duckredbeard 0 1,472 Dec-31-2022, 10:46 PM
Last Post: duckredbeard

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020