Search code examples
pythonpylint

Import constant from external file into python


I would like to import a constant from an external file. I have two files in one directory. constants.py file:

SOME_CONSTANT = 'something'

And import this into settings.py

import constants

someVariable = constants.SOME_CONSTANT

But pylint write that Module 'constants' has no 'SOME_CONSTANT' member


Solution

  • Can't really tell how you made your constants, but ideally you'd want to store them in your class.

    #Constants.Py
    class Province:
        CITY = 'Toronto'
    
    #Settings.Py
    from Constants import Province
    someVariable = Province.CITY
    >>> 'Toronto'