简体   繁体   English

无法导入模块 Python 3 Windows

[英]Can't Import Modules Python 3 Windows

Problem Description: When running scripts from the terminal (example: navigate to C:\\PythonPrograms\\ProjectName> and input file name so it looks like C:\\PythonPrograms\\ProjectName>main.py ), the script fails to run and an error message is received.问题描述:从终端运行脚本时(例如:导航到C:\\PythonPrograms\\ProjectName>并输入文件名,使其看起来像C:\\PythonPrograms\\ProjectName>main.py ),脚本无法运行并显示错误消息收到。 For example, if a script includes import numpy , the error is ImportError: No module named numpy .例如,如果脚本包含import numpy ,则错误为ImportError: No module named numpy When running from an IDE such as spyder, modules import correctly.从 spyder 等 IDE 运行时,模块会正确导入。

Modules were installed with Anaconda, and appear under C:\\Python37\\Lib\\site-packages模块与 Anaconda 一起安装,并出现在C:\\Python37\\Lib\\site-packages

Systems : Windows 10 , Python 3.7.0系统Windows 10Python 3.7.0

Code examples This script successfully runs:代码示例此脚本成功运行:

 x = 1
 for i in range(10):
     print x
     x += 1


C:\Python Programs>test.py
1
2
3
4
5
6
7
8
9
10

This script does not run:此脚本不运行:

import serial
x = 1
for i in range(10):
    print x    
    x += 1


C:\Python Programs>test.py
Traceback (most recent call last):
  File "C:\Python Programs\test.py", line 1, in <module>
    import serial
ImportError: No module named serial

Edit: Python is added to the system path as shown: Screenclip编辑: Python 被添加到系统路径中,如图所示: Screenclip

Solved!解决了!

Checked the path, it was all correct.检查了路径,一切正常。 Went to uninstall python and reinstall, discovered that there was an unknown python 2.7 installed, once it was uninstalled everything worked correctly.去卸载python并重新安装,发现安装了一个未知的python 2.7,卸载后一切正常。

You have to make sure that your Python Path Environment Variable is correctly set on your system.您必须确保在您的系统上正确设置了 Python路径环境变量

This should look like这应该看起来像

C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Python37-32\

If it don't, try to reinstall Python and check Add Python 3.7 to PATH如果没有,请尝试重新安装 Python 并检查Add Python 3.7 to PATH

在此处输入图片说明

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

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