BostonAreaHuman
BostonAreaHuman

Reputation: 1461

How can I access a global variable in a function I'm importing

here is the top of the code...

import sys
import requests
import datetime
import json
import user_functions
from time import sleep
now = datetime.datetime.now()
import urllib3
urllib3.disable_warnings()
requests.packages.urllib3.disable_warnings()

importgroups = ['staff']
key = ""

In the user_functions file I have a function:

def TDactionuser(bcchangess):
    global key
    print key
    global importgroups
    if bcchangess['primary_affiliation'] not in importgroups:
        return

The rub of the issue is that I can't seem to access the key or import groups variables. I'm not sure how to get around this. I use global and not global and well....it says that they are not defined.

Traceback (most recent call last): File "./userupload.py", line 66, in user_functions.TDactionuser(bcchangess) File "/TDPROXY/USERIMP/user_functions.py", line 12, in TDactionuser print key NameError: global name 'key' is not defined

Any help with this will be appreciated...

Upvotes: 0

Views: 65

Answers (1)

user1785721
user1785721

Reputation:

You can do:

def TDactionuser(bcchangess):
    from __main__ import key
    from __main__ import importgroups
    print key
    if bcchangess['primary_affiliation'] not in importgroups:
        return

But generally that is a bad design

Upvotes: 1

Related Questions