'List not defined when importing a function
I made two modules in Python. The other one is a sentinel-controlled loop that asks for the data and the other module contains the functions needed for computation. When I import Module 2 (module with functions), I get a Name Error.
NameError: name 'x' is not defined
where x is the argument of my function convert(x)
but x is a defined list in Module 1 (sentinel-controlled loop) where I store data. I can't figure out why the imported module does not consider it.
I imported the other module as
import ModuleName
What could be the problem?
Disclaimer: I'm a beginner in Python so I'm not really very familiar with it.
Solution 1:[1]
It has to do with the way you import. There are a couple of ways to resolve this.
In the example below, you import module_name and then call function x in the proper namespace.
import module_name
module_name.x()
Another way would be to explicitly import function x from module_name.
from module_name import x
x()
I highly advise you to do some reading on how importing works in python, w3schools is always a good starting point.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Edo Akse |
