0

I'm trying to create a function within a library, to be called from the main script, where the variables are established

Main script (\project1\script.py):

from mylib.dosum import *
myvar = 10
print(dosum(1000))

\project1\mylib\dosum.py:

def dosum(x):
    global myvar
    valuetoreturn = x+myvar
    return valuetoreturn

However, I get the following error message

NameError                                 Traceback (most recent call last)
<ipython-input-2-5c2b1d2cc456> in <module>
      1 from mylib.dosum import *
      2 myvar = 10
----> 3 print(dosum(1000))

~\Python-scripts\project1\mylib\dosum.py in dosum(x)
      1 def dosum(x):
      2     global myvar
----> 3     valuetoreturn = x+myvar
      4     return valuetoreturn

NameError: name 'myvar' is not defined
Hookstark
  • 1,097
  • 3
  • 11
  • 23
  • 1
    The existence of myvar is going to be checked when the module is imported (the imported script actually executes from top to bottom when imported), so it detecting the undeclared variable at that time. You would have to declare myvar outside of the function within the module and also import the variable (don't use wildcard imports). A better solution is to simply pass the variable as a parameter. https://stackoverflow.com/questions/37373115/use-of-global-in-python-across-multiple-scripts-in-python-3-5 – nigh_anxiety Oct 14 '22 at 12:36
  • @nigh_anxiety No, it's not. `myvar` doesn't have to exist until the function is *called*. The problem is that the OP is not defining `myvar` in the correct global namespace. – chepner Oct 14 '22 at 12:37
  • 1
    Does this answer your question? [Using global variables between files?](https://stackoverflow.com/questions/13034496/using-global-variables-between-files) – Abdul Aziz Barkat Oct 14 '22 at 12:45

2 Answers2

2

Every module has its own global namespace. The myvar used by dosum is mylib.dosum.dusum; you are defining a new global in your script named myvar, not assigning to mylib.dosum.myvar.

Do this instead:

# Avoid "from ... import *"
import mylib.dosum

mylib.dosum.myvar = 10
print(mylib.dosum.dosum(1000))
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    `mylib.dosum.myvar = 10` seems incorrect, maybe you meant `mylib.myvar = 10`? I'd prefer a singleton class based approach though. – Abdul Aziz Barkat Oct 14 '22 at 12:47
  • 1
    No, the module `mylib.dosum` contains a function named `dosum` as well. The `...` in `from ... import *` has to be a module name. – chepner Oct 14 '22 at 13:03
0

There are two problems here:

First, that's not how to do the import. Try like this instead!

from mylib import dosum

Second, the error on myvar isn't due to your library missing! You are trying to sum a value (in this case 1000) to nothing, since myvar isn't initialized. I think your goal is to sum 10 and 1000, right? Then either you need to pass two parameters to your dosum() or you need to instantiate a variable somewhere and import it too, like this:

main.py:

from mylib import dosum

MY_VAR = 10
print(dosum(1000))

mylib.py:

from main import MY_VAR


def dosum(x):
    valuetoreturn = x + MY_VAR
    return valuetoreturn

..or, you set the global variable in your main, like so:

mylib.dosum.myvar = 10