简体   繁体   English

Python 无法导入串口

[英]Python cannot import serial

Having Python 3.7.6 and having already installed pyserial ,拥有Python 3.7.6并且已经安装了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.我希望我可以通过失败的导入serial import serial

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) import sys; print(sys.path)的结果import sys; print(sys.path) import sys; print(sys.path) is import sys; print(sys.path)

[
    '',
    '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 where pipwhere pip的结果

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

Firstly, You've installed python from Windows Store.首先,您已经从 Windows 应用商店安装了 python。 Then You decided to install python using official installer.然后你决定使用官方安装程序安装 python。 As a result You've got 2 pythons installed :结果你已经安装了 2 个 python

  • First (from Windows Store) is located in C:\\Users\\%username%\\AppData\\Local\\Microsoft\\WindowsApps\\python.exe ;首先(来自 Windows 应用商店)位于C:\\Users\\%username%\\AppData\\Local\\Microsoft\\WindowsApps\\python.exe
  • Second (from installer) - in C:\\Users\\%username%\\AppData\\Local\\Programs\\Python\\Python37\\python.exe .第二个(来自安装程序) - 在C:\\Users\\%username%\\AppData\\Local\\Programs\\Python\\Python37\\python.exe

Each installer add path to python into PATH and here is the problem.每个安装程序都将 python 的路径添加到PATH 中,这就是问题所在。

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.当您在添加到 PATH 的不同目录中有两个同名的可执行文件( python.exepip.exe等)时,您可以让操作系统选择要启动的文件。 In Your case OS launches python.exe from ..WindowsApps\\ directory and pip.exe from ..Python37\\Scripts\\ .在你的情况下推出的操作系统从python.exe ..WindowsApps\\从目录和pip.exe ..Python37\\Scripts\\

How to fix that:如何解决这个问题:

  1. Press ⊞ Win + R and type sysdm.cpl :⊞ Win + R并输入sysdm.cpl

    Windows“运行”窗口

  2. Switch to tab "Advanced" and press button "Environment variables" :切换到“高级”选项卡,然后按“环境变量”按钮:

    Windows“系统属性”窗口

  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) .在打开的窗口中,您将看到 2 个表,代表当前用户(顶部的表)和整个系统(底部的表)的环境变量。

    Windows“环境变量”窗口

  4. You need to select valiable "Path" on each of these tables and press "Edit" under the table:您需要在每个表上选择可用的“路径” ,然后按表下的“编辑”

    Windows“编辑环境变量”窗口

  5. Choose your warrior!选择你的勇士! You should choose which python you want to use and delete all paths to other copy from list.您应该选择要使用的 python 并从列表中删除到其他副本的所有路径。 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" .要做到这一点,您应该在打开的窗口中选择包含 python 路径的行,您不想通过按“删除”按钮来使用和删除它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM