[英]Script can't import modules, import in shell works fine
I recently ran into an issue on my development box. 我最近在我的开发盒上遇到了一个问题。 I can run my script line by line fine within the interactive shell, but it fails as soon as it tries to import a module when running the script from the command line.
我可以在交互式shell中逐行运行我的脚本,但是当它从命令行运行脚本时尝试导入模块时它会立即失败。
Specifically, I'm trying to use: from flask import Flask
具体来说,我正在尝试使用:
from flask import Flask
and the error I get is: 我得到的错误是:
from flask import Flask
ImportError: cannot import name Flask
It was working perfectly fine a couple days ago and I didn't make any changes to the system (that I can recall) so I can't figure out if this is an issue with my python shell or flask. 它在几天前工作得非常好,我没有对系统进行任何更改(我记得),所以我无法弄清楚这是不是我的python shell或烧瓶问题。
Things I've checked/tried: 我检查/尝试过的事情:
successfully running the following script to test my python shell: 成功运行以下脚本来测试我的python shell:
from time import gmtime, strftime print strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
I'm open to any new troubleshooting suggestions. 我愿意接受任何新的故障排除建议。
I had originally called the file flask.py and there was an old compiled flask.pyc in the same directory. 我最初调用文件flask.py并且在同一目录中有一个旧编译的flask.pyc。 It worked fine as soon as I moved it to another directory and also when removing the old flask.pyc.
一旦我将它移动到另一个目录,并且在移除旧的flask.pyc时,它工作正常。
Thanks for the quick comments timc and BrenBarn! 感谢timc和BrenBarn的快速评论!
I made a stupid mistake of naming my script file nltk.py
. 我犯了一个愚蠢的错误,就是命名我的脚本文件
nltk.py
So when I imported nltk
, it didn't show an error. 因此,当我导入
nltk
,它没有显示错误。 When I tried calling any functions, it displayed an error. 当我尝试调用任何函数时,它显示错误。
Make sure that this doesn't happen. 确保不会发生这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.