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.

1/3/2013 3:23:22 AM

Accepted Answer

I had originally called the file 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!

1/3/2013 3:36:24 AM

I made a stupid mistake of naming my script file 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow