Error message python-pylint C0103:Invalid constant name

2024/11/19 17:29:00

I'm confused about the error(s) in this photo:

Enter image description here

I don't know how to fix them. My program is a Python-Flask web frame. When I use Visual Studio Code to debug my program, Pylint shows these errors. I know this problem doesn't matter, but it makes me annoyed. How can I fix it?

# -*- coding: utf-8 -*-
import sys
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_moment import Moment
#from flask_wtf import Form
#from wtforms import StringField, SubmitField
#from wtforms.validators import Required
from flask_sqlalchemy import SQLAlchemyreload(sys)
sys.setdefaultencoding('utf-8')app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost:3306/test?'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = Truebootstrap = Bootstrap(app)
moment = Moment(app)
db = SQLAlchemy(app)if __name__ == '__main__':db.create_all()app.run()
Answer

As explained by Kundor, PEP 8 states that:

Constants are usually defined on a module level and written in all capital letters with underscores separating words.

The point is that "constants" in Python don't really exist. Pylint, as per PEP 8, expects module level variables to be "constants."

That being said you've several options:

  • you don't want this "constant" thing, then change Pylint's const-rgx regular expression to be the same as e.g. variable-rgx,

  • you may deactivate those warnings for this file, or even locally in the file, using # pylint: disable=invalid-name,

  • avoid module level variables, by wrapping them into a function.

In your case, I would go with the third option, by creating a build_app function or something similar. That would return the application (and maybe the 'db' object as well, but you have several choices there). Then you could add a salt of the second option to get something like:

app = build_app() # pylint: disable=invalid-name

https://en.xdnf.cn/q/26412.html

Related Q&A

How to use virtualenv with python3.6 on ubuntu 16.04?

Im using Ubuntu 16.04, which comes with Python 2.7 and Python 3.5. Ive installed Python 3.6 on it and symlink python3 to python3.6 through alias python3=python3.6.Then, Ive installed virtualenv using s…

Is a day always 86,400 epoch seconds long?

While reviewing my past answers, I noticed Id proposed code such as this:import timedef dates_between(start, end):# muck around between the 9k+ time representation systems in Python# now start and end …

Find the first instance of a nonzero number in a list in Python [duplicate]

This question already has answers here:Return the index of the first element of a list which makes a passed function true(7 answers)Closed last year.I have a list like this: myList = [0.0, 0.0, 0.0, 2.…

How to debug a Python module in Visual Studio Codes launch.json

My question may seem simple but, I have a module that I launch in a terminal like this: python -m my_module.my_fileHow do I debug this in Visual Studio Code? I have this in my launch.json (documentati…

Cleanest Fastest server setup for Django [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

Quicker to os.walk or glob?

Im messing around with file lookups in python on a large hard disk. Ive been looking at os.walk and glob. I usually use os.walk as I find it much neater and seems to be quicker (for usual size direct…

Getting PyCharm to recognize python on the windows linux subsystem (bash on windows)

While running Linux versions of python, pip etc. "natively" on windows is amazing, Id like to do so using a proper IDE. Since SSHD compatibility has not been implemented yet, Im trying get Py…

Whats the difference between nan, NaN and NAN

In numpy there are nan, NaN and NAN. Whats the sense of having all three, do they differ or any of these can be used interchangeably?

Python requests: URL base in Session

When using a Session, it seems you need to provide the full URL each time, e.g.session = requests.Session() session.get(http://myserver/getstuff) session.get(http://myserver/getstuff2)This gets a littl…

size of NumPy array

Is there an equivalent to the MATLAB size() command in Numpy? In MATLAB, >>> a = zeros(2,5)0 0 0 0 00 0 0 0 0 >>> size(a)2 5In Python, >>> a = zeros((2,5)) >>> a ar…