0

I have stored some data in another module that I want to call, but can't retrieve the variable values from that module. How would I do this?

module_with_data.py:

def values():
 global x
 x = 1
values()

main.py:

import module_with_data
x = 1 + x 

What it should output is 2 but I can't get this work.

martineau
124k29 gold badges181 silver badges319 bronze badges
asked Jan 29, 2019 at 23:33
5
  • 3
    x is not in the module scope of module_with_data because you've wrapped it in the scope of a pointless function. Commented Jan 29, 2019 at 23:35
  • 1
    use classes and stuff Commented Jan 29, 2019 at 23:38
  • 2
    import module_with_data introduces exactly one name to your current namespace: module_with_data. You have to be explicit about referring to names in the imported module: module_with_data.x, perhaps, although that won't work either as there is no such thing as x in the module (it's a local variable instead). Commented Jan 29, 2019 at 23:38
  • 1
    Following your update where you declared global x, in module_with_data.py you would need to do a from module_with_data import x to bring the variable into the scope of code in main.py. Alternatively you could reference it this way: module_with_data.x = module_with_data.x + 1. Commented Jan 29, 2019 at 23:51
  • To be clear, x is not defined in the module, it's defined inside the (oddly-named) function values() in the module. That's why importing the module doesn't make it visible. Commented Jan 30, 2019 at 0:41

2 Answers 2

1

module_with_data.py

x = 1

main.py

from module_with_data import x
x = x + 1
print(x)

Looks like you wanted a clean syntax like this, however, do be careful with using naked variables from modules as if they were global, especially when you are changing their value. For simple code, it is fine, but we would forget things once the code base grows large.

answered Jan 29, 2019 at 23:40
Sign up to request clarification or add additional context in comments.

1 Comment

With your edit you could use import module_with_data as mwd and use mwd.x to avoid that issue
1

The x you are changing doesn't exist until it is assigned a value, but since it is assigned a value inside a function, it is created in scope of that function and won't be available outside it.

You can assign a global x a value inside a function, if that makes more sense in your case, but you have to declare x as global:

def values()
 global x
 x = 1
values()

Alternatively, don't declare it in a function, but just put the assignments in the main body of the module:

x = 1

Secondly, you're importing the entire module, but that means you only have access to its contents if you namespace them:

import module_with_data
module_with_data.x = 1 + module_with_data.x

Or, you can add it to the namespace of the script importing it:

from module_with_data import x
x = 1 + x
answered Jan 29, 2019 at 23:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.