trollchen
trollchen

Reputation: 78

Flask PyCharm Syntax error

I've just started developing a new application using Flask and PyCharm as my IDE. I've encountered a problem which is driving me crazy.

Here is the code. I'll spare use all the import part and so on, I'll show only the routes that cause problems.

@app.route("/")
def home():
    return render_template("home.html")

@app.route("/login/") 
def login():
    return "foo"

The first route works without a problem. However on the first line of the second route, PyCharm gives me an error message '"@" or "def" expected'. This breaks the autoindent and the auto completion features.

But the code runs without a problem.

Can anyone tell me how to fix this?

Thank you

Upvotes: 3

Views: 893

Answers (2)

JayRizzo
JayRizzo

Reputation: 3616

What worked for me was (I was missing pycharm's python environment dependencies for python)

If you don't already have a requirements file for your project. Navigate to your project & this on your command line (as I used a venv virtual environment):

(venv)$ pip freeze > requirements.txt

Then Open your pycharm and open the requirements file, wait a second or two and a pop up notification will show up at the top showing you are missing dependencies and will provide you a click here to install.

After that I had no issues.

Upvotes: 0

Pralhad Narsinh Sonar
Pralhad Narsinh Sonar

Reputation: 1454

This is because PyCharm is not able to recognize the flask package or version.

Once you add new project to PyCharm -

  1. Go to File menu item
  2. Click on Settings option
  3. This will open-up the pop-up window.
  4. In the left-hand-side link list - click on the project name
  5. Click on the Project Interpreter link
  6. In the right-hand-side pane select Flask and related packages that your project needs and add/install the same.

enter image description here

Upvotes: 1

Related Questions