17

I have added Flask and Flask-Script to PyCharm virtual environment. The app runs but PyCharm gives me various errors:

Unresolved reference "Manager" (from  flask.ext.script import Manager)
Cannot find reference "script" in "__init__.py" (from  flask.ext.script import Manager)
No module named "script" (command = flask.ext.script.Command(app)

As a result code completion doesn't work.

So if I directly import the modules like import flask_script code completion works but I am not sure if that's an intended use!

I can fix that if I import flask_script directly instead of flask.ext. Is that a valid workaround?

Is there any other proper way to solve that? I read a few answers but none of them seems to address this issue.

Zingam
  • 4,498
  • 6
  • 28
  • 48
  • 4
    If you can import directly from `flask_script` then you should do that. The `flask.ext` namespace exists due to some import remapping code anyway and so certain things can break, such as code completion. – metatoaster Aug 11 '14 at 11:05
  • I don't understand the need for 'flask.ext' then. – Zingam Aug 11 '14 at 11:17
  • 2
    Just pretend it doesn't exist, it will make your life much easier. However, [Flask Extension Development](http://flask.pocoo.org/docs/extensiondev/) has some sort of weak justification for the `flask.ext` import location, but really it goes against some of the basic tenets of Python (namely: "There should be one-- and preferably only one --obvious way to do it"). – metatoaster Aug 11 '14 at 11:30
  • Similar Question here: http://stackoverflow.com/questions/11725519/pycharm-shows-unresolved-references-error-for-valid-code hope that helps. – Lone Ronin May 05 '17 at 14:47

5 Answers5

16

In your venv > lib > python3.x > site-packages mark site-packages as source root.

Ali Gajani
  • 14,762
  • 12
  • 59
  • 100
9

Go to File -> Default Settings -> Project Interpreter Add your respective interpreter. Then, add the 'Flask' package by clicking that plus sign. It will cover up all the module.

It works for me!

vegetarianCoder
  • 2,762
  • 2
  • 16
  • 27
3

I had a similar error stating "Cannot find reference 'Flask' in 'flask.py".

If you're on windows (using Pycharm) then go to Files -> project interpreter There will be a list of packages, locate Flask and open it by double clicking it. Then click install Package, it will install it for your project.

I don't know about it much in detail but this is how I solved my problem.

Zed Xin
  • 31
  • 2
2

You need to tell PyCharm which interpreter you are using. Let's say you use some virtualenv, on a Mac (is similar on Linux) go to File > Default Settings > Project Interpreter

Then click on the little mechanic wheel next to the project interpreter dropdown menu and choose 'add local' and pick your interpreter (e.i. the virtual environment you use in which you installed flask).

Hope this helps.

Robbotnik
  • 557
  • 8
  • 8
1

Try giving the file a name other than flask.py. Because there will be a naming conflict during import.