0

This is not a duplicate question, only marked as so by someone who doesn't even care to read the two carefully.

OK, I give up -- I am learning Python and am still a beginner, but someone just want to show that he is unwilling to help, and doesn't allow other to help either, because the problem get closed and nobody can answer anymore. Once again, They are total different problems (because that one is about using math.pi in module1.cool() which can be easily solved by doing from math import pi in module1), at least to me, and even now, because I still cannot make the connection as I am learning Python and am still a beginner. If you think it is a duplicate, please show me how, not just arbitrarily say so without leaving any clue. That's not helping at all.

I have a dozen of global variables that I want to pass to my functions, previously defined in iPython notebook, and works just fine; now defined inside a separate file and everything broke down. I need a solution, but neither of the suggested "duplicated" can answer that. I give up.

I had been putting my global variables and my Functions definitions in iPython notebook like the following:

global_var = 'foo'
def ex1():
    local_var = 'bar'
    print global_var
    print local_var

ex1()
print global_var

I.e., I define a global variables and can use it from my functions definitions in iPython notebook. This is OK.

However, when I move the functions definitions into a separated file (as it is getting too long and I don't want to copy it around to different iPython notebooks each time), I can't access my global variables from the same function any longer. I got:

NameError: name 'global_var' is not defined

The only difference is that I put the functions definitions into a separated file. Then use

from mylib import *

So, it is still possible for my functions to access my global variables defined in iPython notebook please?

UPDATE 1: I saw that (the dulicate candidate now removed), but that one's first answer is more on functions, SQL cursor, using second modules, yada, yada, which I really can't grasp and relate. Let's give this simple question a simple answer instead. E.g., the second answer is from globals import *, that's what I'm using.

UPDATE 2: About "Python: Namespaces with Module Imports", if you look closely, the problem is about using math.pi in module1.cool(), a total different problem which can be easily solved by doing from math import pi in module1. Well, at least to me they are total different problems, because I still cannot make the connection as I am learning Python and am still a beginner. If you think it is a duplicate, please show me how, not just arbitrarily say so without helping the poor soul who is still lost. Gee, no matter how many time I edit it, that wrongfully-accusing "[duplicate]" sign is still there. Gee. Again, If you think it is a duplicate, please show me how, not just arbitrarily say so without leaving any clue.

xpt
  • 20,363
  • 37
  • 127
  • 216
  • 1
    `global` variables live in the module's namespace. Think of them as "module global". Since `ex1()` is in `mylib`, `global_var` should be moved there also. `from mylib import *` creates new references to the variables and functions from `mylib` but those functions didn't move to the new module... they are still in `mylib` and its still their global namespace. Its more problematic for variables. Supposing `global_var` is in `mylib`. After import, there is now a local `global_var` referencing the same value. But if its reassigned (say, `global_var += 1`) in one module, others won't see it. – tdelaney Dec 20 '15 at 04:53
  • Yeah, after more reading, I know that `global_var` should be moved into `mylib` as well. But, my problem is that I'm doing data manipulation in iPython notebook, so I have a dozen of global variables defined in iPython notebook that will affect the data manipulation and plotting, however, I really don't want to redefine my canned function each time I found I need to use yet another variable instead of hard coding it. I need an easy answer, to make it easy, not complicated. Thanks for your reply, as you can see, this is not an easy problem at all. Just saying "duplicate" won't solve it. – xpt Dec 20 '15 at 14:20
  • 1
    You could move the variables to their own module. Then everyone who wants to use them imports the module (not `from myvars import *` but `import myvars`). As your code base grows, you might find keeping just 1 set of variables limiting and then should move to classes. Me, I do that from the beginning so that I don't have to refactor later. – tdelaney Dec 20 '15 at 19:24
  • Thanks. So for my case, I do `import myvars` from within my `mylib` *and* my iPython notebook, customize them in my iPython notebook, and then they can be used in functions defined in my `mylib`, right? – xpt Dec 20 '15 at 22:42
  • Yes. I don't use ipython notebook so I don't know if there is some other restriction, but that's exactly how to do it in regular python. – tdelaney Dec 20 '15 at 22:48
  • No, unfortunately, it is not working. I've published all my test code at, https://gist.github.com/suntong/fd13ce81bd3c59b67eda. Thanks a lot for your help any way @tdelaney, which symbolizes the helpful attitudes of the Python Community. Though the exceptional few showing hostile attitudes toward newcomers like me, I believe the majority of the Python Community are like you, willing to help. But, that exceptional few really make SO a hostile place to Python newcomers. I've asked the question twice, and twice it was closed brutally without a clue, leaving me still struggling til now. sigh~~~ – xpt Dec 22 '15 at 16:22

0 Answers0