0

So I have a program with a module and a seperate file which are as follows Module:

 def area_of_Triangle(b, h, units = "square centimeters"):
    aot = (b * h) / 2
    return "{0} {1}".format(aot, units)


 def MyTuple(*args):
    result = 0
    for arg in args:
        result += arg
    return result


 divide = lambda x, y: x / y if y != 0 else "Not Allowed"

The separate file:

import MODULE1

print (area_of_Triangle(4.5, 5.6, units="square inches"))

print (MyTuple(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))

print (divide(2, 3))

every time I try to run it, it keeps saying name 'area_of_Triangle' not defined

3 Answers3

2

You're trying to call MODULE1 functions from a different file. You either need to specify MODULE1 in the calls

import MODULE1

print (MODULE1.area_of_Triangle(4.5, 5.6, units="square inches"))

print (MODULE1.MyTuple(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))

print (MODULE1.divide(2, 3))

Or you can explicitly import them

from MODULE1 import area_of_Triangle, MyTuple, divide

print (area_of_Triangle(4.5, 5.6, units="square inches"))

print (MyTuple(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))

print (divide(2, 3))

Why the extra hassle? Well what if you imported both MODULE1 and some MODULE2, and both define a function called area_of_Triangle? How do you distinguish which is which?

Locke
  • 374
  • 2
  • 9
  • `from MODULE1 import *` you *really shouldn't use starred imports* – juanpa.arrivillaga Oct 11 '21 at 04:25
  • May I ask.. what is the issue with starred imports? Namespace pollution ? – leoOrion Oct 11 '21 at 04:27
  • Oh thank you so much sorry for the simple mistake Im still kinda new to python/ coding in general – Theriddler748 Oct 11 '21 at 04:29
  • 1
    Regarding `import *` - there's a bunch of reasons here https://stackoverflow.com/questions/2386714/why-is-import-bad. For a tiny little standalone program I'd argue it's fine, but meh, each to their own. – Locke Oct 11 '21 at 04:29
  • 1
    the main reason to not use starred imports is to maintain readability. Imagine when you working with large project, where the function distributed to 10†re different files. If you use starred import, then you don't know where this function comes from. Another reason is to reduce unnecessary function import, since it will import everything including all the variables from another file, which can be problematic if you have variable with same name, or in other file the variable is storing huge data. – Vinson Ciawandy Oct 11 '21 at 04:31
0

you only imported the module using

import Module1

if you want to import all methods and variables in a python file use the * sign

for example

from <modulename> import *

so in your case it would be

from Module1 import *

or alternatively, you can import a specific method by using the same way but instead of * use the method's name

for example

from Module1 import area_of_Triangle

if you do not wish to do the above

import Module1

is also fine, just have to call the method as such:

Module1.area_of_Triangle(4.5, 5.6, units="square inches")

notice the Module1.area_of_Triangle()

Jerome Palayoor
  • 31
  • 1
  • 1
  • 9
0

You've to write MODULE1.area_of_triange(<parameters>) and print it. Or else if you don't want to write MODULE1 again and again, you can import specific function from the module, or import all functions using from MODULE1 import * line and then directly use the functions without writing the module name again and again