0

I have a Flask app created, and my folder structure looks like this:

-- Folder
  ---- app.py
  ---- page.py
  ---- __init__.py

I'm trying to reference page.py as a class, using:

from flask import render_template
from flask import Flask, redirect, url_for
import logging
import json
import requests
import os
import page as Page 

""" Initialise the app """
app = Flask(__name__)

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

if __name__ == "__main__":
    app.run(debug=True)

But I get the error:

Cannot import 'page' due to syntax error 'invalid syntax (<unknown>, line 3)'pylint(syntax-error)

Where am I going wrong?

My class file looks like this:

class Page:
    def __init__(self, title, environ)
    self.title = title 
    self.environment = environ

I tried from .page import Page

but still got the error:

Cannot import 'page' due to syntax error 'invalid syntax (<unknown>, line 3)'pylint(syntax-error)

4 Answers 4

1

It is due to syntax error in line 3.

class Page:
    def __init__(self, title, environ) # no colon
    self.title = title # no indentation
    self.environment = environ

If this is how it looks like (as you have attached), there is no colon (:) and indention after def where it should belong. Therefore, your page.py should look something like:

class Page:
    def __init__(self, title, environ):
        self.title = title 
        self.environment = environ
3
  • Thanks, this was it - feel so stupid! I'm coming from C# where indentation etc isn't as critical. Commented Jan 13, 2020 at 0:05
  • 1
    I started with C first, so I feel you :) The indentation can be a real pain, you can always use pylint yourcode.py to check for any syntax-related errors in your code.
    – wookiekim
    Commented Jan 13, 2020 at 0:08
  • Thanks so much. It's these little things that are my biggest learning with Python Commented Jan 13, 2020 at 0:08
0

Have you tried:

from .page import Page
0
0

page.py cannot be referenced as a class as it is just a python file. I presume there is a class within this file called Page.

Therefore, you will need to import the Page class from the page.py file with the following syntax:

from .page import Page

0
0

The reason you are having that error is because you aren't accessing the package first, you are trying to go straight to the module, but it doesn't work that way.
You have to access the package first, then the module, then the class and so on.

What you should have then is

from Folder.page import Page

or to work with relative path use

 from .page import Page

Hope this helps One last piece of advice would be to use correcting naming patterns which means classes start with uppercase letters, that will make imports easier for you in the long run

class Page:
    def __init__(self, title, environ)
        self.title = title 
        self.environment = environ

Note the indentation change

1
  • did you try the initial option of from Folder.page import Page. Also correct the syntax error in your class, i have updated my answer to help Commented Jan 12, 2020 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.