Python Forum
Serial connection connection issue - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Serial connection connection issue (/thread-34735.html)

Pages: 1 2


Serial connection connection issue - Joni_Engr - Aug-26-2021

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'))



RE: Serial connection connection issue - popejose - Aug-26-2021

Can you confirm you have installed the pyserial module?

If not, pip install pyserial should do it.


RE: Serial connection connection issue - Joni_Engr - Aug-30-2021

(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.


RE: Serial connection connection issue - snippsat - Aug-30-2021

(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)



RE: Serial connection connection issue - Joni_Engr - Aug-30-2021

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.


RE: Serial connection connection issue - snippsat - Aug-30-2021

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()



RE: Serial connection connection issue - Joni_Engr - Aug-30-2021

I am running on Windows 10.


RE: Serial connection connection issue - snippsat - Aug-30-2021

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:\>



RE: Serial connection connection issue - Joni_Engr - Aug-30-2021

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



RE: Serial connection connection issue - DeaD_EyE - Aug-30-2021

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.html#python-launcher-for-windows