pythonwindowsserial-portpython-3.7pyserial

Python cannot import serial


Having Python 3.7.6 and having already installed pyserial,

pip install pyserial --user
Requirement already satisfied: pyserial in c:\users\myusername\appdata\local\programs\python\python37\lib\site-packages (3.4)

pip install pyserial
Requirement already satisfied: pyserial in c:\users\myusername\appdata\local\programs\python\python37\lib\site-packages (3.4)

I expect that I can import serial via import serial which fails.

No module named 'serial'

This is as follows

python3
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 01:54:44) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'serial'

Am I doing any mistake?


PS. The result of import sys; print(sys.path) is

[
    '',
    'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\\python37.zip',
    'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\\DLLs',
    'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\\lib',
    'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0',
    'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\\lib\\site-packages'
]

PS. The result of where pip is

C:\Users\myusername\AppData\Local\Programs\Python\Python37\Scripts\pip.exe
C:\Users\myusername\AppData\Local\Microsoft\WindowsApps\pip.exe

Solution

  • Firstly, You've installed python from Windows Store. Then You decided to install python using official installer. As a result You've got 2 pythons installed:

    Each installer add path to python into PATH and here is the problem.

    When you have two executables with same name (python.exe, pip.exe, etc.) in different directories which added to PATH You let OS choose which to launch. In Your case OS launches python.exe from ..WindowsApps\ directory and pip.exe from ..Python37\Scripts\.

    How to fix that:

    1. Press āŠž Win+R and type sysdm.cpl:

      Windows "Run" window

    2. Switch to tab "Advanced" and press button "Environment variables":

      Windows "System Properties" window

    3. In opened window you'll see 2 tables representing environment variables for current user (table on the top) and for whole system (table on the bottom).

      Windows "Environment Variables" windows

    4. You need to select valiable "Path" on each of these tables and press "Edit" under the table:

      Windows "Edit environment variable" window

    5. Choose your warrior! You should choose which python you want to use and delete all paths to other copy from list. To do that You should in opened windows select rows which contain path to python You don't want to use and delete them by pressing button "Delete".