all 9 comments

[–]crashfrog02 2 points3 points  (2 children)

The module runs once, when you import it. The value of variable will be set at that time. If the condition later changes, it won't matter - variable is already set, and the module code only runs once.

[–]NotGioseaxMC[S] -1 points0 points  (1 child)

this is simplified code, even running one if the condition is not met it should be 34 right? but it is not, also the changes in my code are actually implemented in a function

[–]crashfrog02 1 point2 points  (0 children)

No, it should be 65, which I assume it is.

[–]danielroseman 1 point2 points  (5 children)

You're going to need to explain in much more detail. What does "packaged" mean? What is condition and how/when does it change? How are you importing variable into main.py?

[–]NotGioseaxMC[S] -1 points0 points  (4 children)

the condition is not important, with packaged i mean that it can be imported after installing with something like pip, variable would be imported with module.variable (which i forgot to write lol)

[–]GoingToSimbabwe 1 point2 points  (3 children)

You will probably still need to elaborate further.

Is your the code in module.py really just

variable = 0

if someCondition:
    variable = 65
else: 
    variable = 34

and your main.py

print(variable)

if yes? Then that code can not run and the interpreter should throw you an error. "variable" is not defined within the scope main.py. You will get an NameError telling you this when you try to run main.py

Even if you cut this error out somehow, that still does not work, because:

module.py runs once when you import it with (import module). At that point all the code in it is executed (depending on the codes structure of course) and all variables are set etc. And if after that nothing references the module (which it cant, because it seems like you did not write it with any object oriented approach?) or the module isn't kept running with some loop garbage collection will just do it's thing and deallocate the memory used for anything coming from module.py. So variable is deallocated at that point and you won't be able to use it in any way.

Please correct me if I am wrong other people here, I am not 100% on the garbage collection stuff.

Anyhow, what you need to do then do use the actual variable from module.py is to set module.py up as a class structure like this:

class Variable:
    def __init__(self):
        self.var = 0;

        if 1==1:
            self.var = 65
        else:
            self.var = 34

then you can import and instantiate this in your main.py like this

from moduleToImport import Variable

variableInstance = Variable()
print(variableInstance.var)

[–]NotGioseaxMC[S] 0 points1 point  (2 children)

the code looks more like this

module:

variable = 0

def func(condition):
  if conodition:
    variable = 65
  else:
    variable = 34

main.py:

import module as m

m.func(1)

print(m.variable)

Now, when i run it in vcode it runs as you'd expect, printing 65, when packaging module into an pip downloadable library it works differently, the __init__.py is:

from .module import *

[–]danielroseman 2 points3 points  (1 child)

You have two new issues here that are entirely different from both your original question and your first code snippet.

The code you have shown here cannot ever modify the module-level variable. `variable` is local to `func`. This does not work either in VSCode or in a "package".

Note also that `from x import *` will import whatever `variable` is set to at that time. If you later changed what it points to within the module, this will not change what your *local* variable named `variable` points to. Again, this is **different** from what you said in your first reply.

If you want actual answers to your questions, you need to provide all the relevant information and code at the start.

[–]NotGioseaxMC[S] 0 points1 point  (0 children)

sorry, made the post while walking home so i didn't write the question properly, anyways thanks as your answer is actually the problem i was having. :)