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
Import module works in terminal but not in IDLE
提问by Kuoso
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:
我正在尝试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
- Open python in cmd (type
python
and press enter) - Import the module in cmd (type
import modulename
) - Type
modulename.__file__
- You will get the path where the module is stored
- Copy the corresponding folder
- In IDLE,
import sys
and typingsys.executable
to get the paths where it looks for modules to import - Paste your module's folder in the path where IDLE looks for modules.
- 在cmd中打开python(输入
python
并回车) - 在cmd中导入模块(输入
import modulename
) - 类型
modulename.__file__
- 您将获得存储模块的路径
- 复制对应文件夹
- 在 IDLE 中,
import sys
键入sys.executable
以获取查找要导入的模块的路径 - 将您的模块文件夹粘贴到 IDLE 查找模块的路径中。
This method worked for me.
这种方法对我有用。
回答by dmgl
You can pip show
after install package and know about location where package installed.
您可以pip show
在安装包后了解安装包的位置。
After that check in IDLE sys.path
and if directory with package not in sys.path
try 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版本目录不匹配。
解决办法是>>>
- type %localappdata% in your search bar then go to this folder.
- here select the program folder. then select Programs , Python , Python version , Scripts
- copy the location of the Scripts folder
- open command prompt and type cd //yourpath (in my case cd C:\Users\3C HOUSE\AppData\Local\Programs\Python\Python37\Scripts)
- if you wanna install numpy, now run pip install numpy
- 在搜索栏中键入 %localappdata% 然后转到此文件夹。
- 在这里选择程序文件夹。然后选择 Programs , Python , Python version , Scripts
- 复制 Scripts 文件夹的位置
- 打开命令提示符并输入 cd //yourpath(在我的例子中是cd C:\Users\3C HOUSE\AppData\Local\Programs\Python\Python37\Scripts)
- 如果你想安装 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 idle
for that specific version like this
这是因为安装了多个python(32位版本,64位版本)或3v和2.7v所以要解决这个问题,你必须idle
像这样调用特定版本的
cd
to the dir
of the version that the import work fine in cmdin that folder type this command below
cd
该dir
版本将在cmd中输入做工精细该文件夹中键入以下命令
pythonw.exe Lib\idlelib\idle.pyw
this command will invoke idle for that version and the import will work fine
此命令将为该版本调用 idle 并且导入将正常工作