HilaD
HilaD

Reputation: 881

python define names as a constant global name is not defined

I am using python 2.7 (pycharm) and I set up global names in "constants.py" and after import constants in the main program, it doesn't recognize it.

project/folder/constants.py:

    DATE_FORMAT = "%Y-%m-%d"
    TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S'

    LOCAL = 'local'
    .......

project/main.py:

from constants import *

if config['masterUrl'] == LOCAL: .....

error:

 if config['masterUrl'] == LOCAL:
NameError: global name 'LOCAL' is not defined

what am I doing wrong?

Upvotes: 1

Views: 1411

Answers (1)

Nikita Kriuchkov
Nikita Kriuchkov

Reputation: 51

You can use the constants in the project like this:

from project.folder.constants import *

And you need __init__.py in the project dir.

The namespace path in python is absolutly. What I mean:

project
  -- app1
    -- constants.py
  -- app2
  -- module2
__init__.py
main.py

main.py

from project.app1.constants import *

It's not scala or cpp

Upvotes: 1

Related Questions