Python 导入模块在终端中工作,但在 IDLE 中无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31661694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:22:21  来源:igfitidea点击:

Import module works in terminal but not in IDLE

pythonmoduleterminalpython-importpython-idle

提问by Kuoso

I am trying to import pyodbcmodule on a windows computer. It works in the terminal, but not the IDLE. The error message in IDLE is:

我正在尝试pyodbc在 Windows 计算机上导入模块。它适用于终端,但不适用于 IDLE。IDLE 中的错误信息是:

Traceback (most recent call last):

  File "FilePath/Filename.py", line 3, in <module>
      import pyodbc
  ImportError: No module named pyodbc

回答by Garrigan Stafford

This typically occurs when multiple versions of python are installed with different paths. You can check to see if you have multiple installations by opening up the IDLE terminal and using

这通常发生在使用不同路径安装多个版本的 python 时。您可以通过打开 IDLE 终端并使用

import sys
sys.version
sys.path

These commands will print the system PATH and version of the current instance of python. Use this in both IDLE and the command line terminal to see where each differ. Once you know which version is the one you want then just remove the other. You could also remove all python instances and then reinstall a clean python environment but then you would have to re-install all of your modules using pip or easy_install

这些命令将打印当前 python 实例的系统路径和版本。在 IDLE 和命令行终端中使用它来查看每个不同的地方。一旦你知道哪个版本是你想要的,那么只需删除另一个。您还可以删除所有 python 实例,然后重新安装一个干净的 python 环境,但是您必须使用 pip 或 easy_install 重新安装所有模块

回答by AKD

  1. Open python in cmd (type pythonand press enter)
  2. Import the module in cmd (type import modulename)
  3. Type modulename.__file__
  4. You will get the path where the module is stored
  5. Copy the corresponding folder
  6. In IDLE, import sysand typing sys.executableto get the paths where it looks for modules to import
  7. Paste your module's folder in the path where IDLE looks for modules.
  1. 在cmd中打开python(输入python并回车)
  2. 在cmd中导入模块(输入import modulename
  3. 类型 modulename.__file__
  4. 您将获得存储模块的路径
  5. 复制对应文件夹
  6. 在 IDLE 中,import sys键入sys.executable以获取查找要导入的模块的路径
  7. 将您的模块文件夹粘贴到 IDLE 查找模块的路径中。

This method worked for me.

这种方法对我有用。

回答by dmgl

You can pip showafter install package and know about location where package installed.

您可以pip show在安装包后了解安装包的位置。

After that check in IDLE sys.pathand if directory with package not in sys.pathtry to add it.

之后检查 IDLE sys.path,如果没有包的目录sys.path尝试添加它。

import sys
sys.path.append("/home/dm/.local/lib/python3.6/site-packages")
# or another folder that `pip show` about package.

回答by Nuwan Madushanka

I Found the solution. It works for me

The problem is your installation directory does not match with the python version directory.
solution is >>>

我找到了解决方案。它对我

有用问题是您的安装目录与python版本目录不匹配。
解决办法是>>>

  1. type %localappdata% in your search bar then go to this folder.
  2. here select the program folder. then select Programs , Python , Python version , Scripts
  3. copy the location of the Scripts folder
  4. open command prompt and type cd //yourpath (in my case cd C:\Users\3C HOUSE\AppData\Local\Programs\Python\Python37\Scripts)
  5. if you wanna install numpy, now run pip install numpy
  1. 在搜索栏中键入 %localappdata% 然后转到此文件夹。
  2. 在这里选择程序文件夹。然后选择 Programs , Python , Python version , Scripts
  3. 复制 Scripts 文件夹的位置
  4. 打开命令提示符并输入 cd //yourpath(在我的例子中是cd C:\Users\3C HOUSE\AppData\Local\Programs\Python\Python37\Scripts
  5. 如果你想安装 numpy,现在运行pip install numpy

回答by zerocool

this happen because of multiple python installed (32bit version, 64bit version) or 3v and 2.7v so to solve this problem you have to invoke the idlefor that specific version like this

这是因为安装了多个python(32位版本,64位版本)或3v和2.7v所以要解决这个问题,你必须idle像这样调用特定版本的

cdto the dirof the version that the import work fine in cmdin that folder type this command below

cddir版本将在cmd中输入做工精细该文件夹中键入以下命令

pythonw.exe Lib\idlelib\idle.pyw

this command will invoke idle for that version and the import will work fine

此命令将为该版本调用 idle 并且导入将正常工作