0

so I know this has been asked in several forms before, but I cannot relate to any of those, either I have something different or I just don't understand them.

The problem is I have script A and script B, and in script A I calculate and have all the variables I want to use in script B.

Script A has various functions, let's say for now I just want to pass a simple number from a variable in script A to script B , let's call the variable value .

I used from script_A import value .

Now, I have value initialized in script_A with 0 right at the top to say so, but script_A processes value, and gets a result clearly different from 0, but when I debug, I am getting in script_B value == 0, and not value == calculated_value_that_should_be_there.

I did not know what to do so I tough about scope,so I put it in the return of a function, I tried making variable value a Global variable. Nothing seems to work in the way that I am not passing the calculated 'value' but I am passing to script_B that 0 initialization.

P.S last thing I tried and what I saw from this topic is to import script_A as it was said with no namespaces. This has worked. When I write script_A.value it is calculated_value_that_should_be_there. But, I do not know why anything else that I described did not work.

script_A


from definitions import *
variable_1 = 0
variable_2 = 0
variable_3 = 0
variable_4 = 0 

total = 0
respected = 0

time_diff = {}
seconds_all_writes = "write"

class Detect():
    def __init__(self, data_manager, component_name, bus_name_list=None):

 def __Function_A(self):
       """
        global time_diff
        global seconds_all_writes

        process

script_B:
from script_A import respected
from script_A import total


import script_A

        print aln_mon_detector.total
        print aln_mon_detector.respected

I also want a dictionary

table_content.append(script_A.time_diff[file[script_A.seconds_all_writes])

I get

KeyError: 'writes'

4
  • 2
    Instead of describing your code can you please just paste some examples of the code that you are actually using? It would be a lot more helpful to debug it. Commented Oct 4, 2018 at 7:54
  • You'll need to use the script_A_process in your script B and get the returned value there. do, from script_A import value, script_A_processs and then do the process in script B. Also, can you instead write down some code example, i'm not sure I got it right Commented Oct 4, 2018 at 7:55
  • please edit your question to show the code you are using. Commented Oct 4, 2018 at 8:09
  • Mandatory reading: nedbatchelder.com/text/names.html Commented Oct 4, 2018 at 9:00

3 Answers 3

1

this sounds a bit confusing without an example, but, in principle, what you're trying to do should work. Have a look at a minimum example below.

ModuleA - defining the variable

# create the variable
someVariable = 1.

# apply modifications to the variable when called
def someFunc(var):
    return var + 2

# ask for changes
someVariable = someFunc(someVariable)

ModuleB - using the variable

import moduleA

# retrieve variable
var = moduleA.someVariable

print(var) # returns 3
Sign up to request clarification or add additional context in comments.

1 Comment

yes, this works as I said in my P.S, but to the variables I transfer to script_B need to be declared as GLOBAL , I seem not to be able to access them in script_B if they are not global.
0

This probably has to do with immutability. Depends on what value is. If value is a list (that is, a mutable object) and you append to it, the change should be visible. However, if you write

from module import x
x = 5

you are not changing the actual value, so other references to x will still show the original object.

Comments

0

If you have script A like this:

# imports

value = 0
...  # some calculations

Re-organize script A as:

# imports

def main():
    value = 0
    ...  # some calculations
    return value

Now you can import script A in script B and run calculations inside script B:

import script_A

value = script_A.main()

That is how you should organize code pieces in Python.

2 Comments

I see, so initialize the value in main, not after the imports?
@whtr The point here is to create function main() (which returns calculation results) in script_A and then call function scrit_A.main() in script_B after importing script_A.

Your Answer

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.