简体   繁体   中英

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.

Specifically, I'm trying to use: 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.

Things I've checked/tried:

  • deleting/reinstalling flask via pip
  • confirming C:\\Python27\\lib\\site-packages is in my system path
  • clearing and re-adding the directories in my PATH environment variable
  • uninstalling/reinstalling Python 2.7.3
  • successfully running the following script to test my 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. It worked fine as soon as I moved it to another directory and also when removing the old flask.pyc.

Thanks for the quick comments timc and BrenBarn!

I made a stupid mistake of naming my script file nltk.py . So when I imported nltk , it didn't show an error. When I tried calling any functions, it displayed an error.

Make sure that this doesn't happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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