1

How can i import functions that make use of a variable defined in the current file?

main.py

from functions import a

x = 1

print(a())

functions.py

def a():
    return x

Error Message

Traceback (most recent call last):
  File "c:\Users\Test\.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "c:\Users\Test\.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\lib\python\ptvsd\__main__.py", line 410, in main
    run()
  File "c:\Users\Test\.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\lib\python\ptvsd\__main__.py", line 291, in run_file
    runpy.run_path(target, run_name='__main__')
  File "C:\Users\Test\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\Test\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\Test\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\Users\Test\Google Drive\Dev\Test\main.py", line 5, in <module>
    print(a())
  File "c:\Users\Test\Google Drive\Dev\Test\functions.py", line 2, in a
    return x
NameError: name 'x' is not defined
4
  • The exception you mention doesn't match the issue you're describing elsewhere. Are you sure the problem is with importing c, rather than looking up x, y or z after you complete the imports and start calling the functions? If you really are getting an ImportError, then something else is going wrong, unrelated to your global variable design issue. Commented Apr 27, 2019 at 23:06
  • better create functions which get it as argument a(x), b(y), c(z) Commented Apr 27, 2019 at 23:08
  • i changed the question by reducing complexity (only one function) and giving the full trace back Commented Apr 27, 2019 at 23:27
  • dont want to give the variables as arguments. in reality i have more than just three., Commented Apr 27, 2019 at 23:27

2 Answers 2

3

As another answer has noted, this will not work because of how Python scopes variables.

Instead, therefore, what I suggest is that you move all these variables into a separate file, e.g. constants.py:

main.py

from functions import a

print(a())

constants.py

X = 1

functions.py

from constants import X

def a():
    return X

Then, running import main prints 1.

Sign up to request clarification or add additional context in comments.

Comments

0

There are no process-wide globals, only module-level globals. a uses functions.x, not the x in whatever global scope it is called from.

import functions
from functions import a, b, c

functions.x = 1
functions.y = 2
functions.z = 3

print(a())
print(b())
print(c())

Because Python is lexically scoped, each function keeps a reference to the global scope in which it was defined. Name lookups use that scope to resolve free variables.

2 Comments

there might be other imports that also use these variables. I think its not useful to write into the context of each import
I'm not stating a best practice: I'm describing how Python works. If you don't like it, don't use global variables.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.