Import module works in terminal but not in IDLE

喜你入骨 提交于 2019-12-17 14:53:50

问题


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

Traceback (most recent call last):

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

回答1:


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

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




回答2:


  1. Open python in cmd (type python and 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 sys and typing sys.executable to get the paths where it looks for modules to import
  7. Paste your module's folder in the path where IDLE looks for modules.

This method worked for me.




回答3:


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

After that check in IDLE sys.path and if directory with package not in sys.path try to add it.

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


来源:https://stackoverflow.com/questions/31661694/import-module-works-in-terminal-but-not-in-idle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!