AceScottie
AceScottie

Reputation: 109

import from if__name__ == "__main__"

Hello i am trying to extract some info from a piece of code which runs a potentiometer.

class KY040
    def x():

    def y():

if __name__ == "__main__"
    def rotaryChange(direction):
        turn = "turned"
        return turn

in another code (same dir) I have "from pots import rotaryChange", however it errors saying cant import name rotaryChange.

can i not import from an if statement ?

I didnt write the original code i just edited it, original source: https://github.com/martinohanlon/PiLadyAnneRadio -file is sourcecode/KY040

my edit:

l1= 0
l2 = 0
def rotaryChange(direction):
    global l1
    global l2
    turn = ""
    l1 = l1 +  int(direction)
    #print l1
    #print "turned : " + str(direction)
    if (l1 != l2):
            if (l1 < l2):
                    print ("clockwise Turn")
                    turn = "clockwise"
            else:
                    print ("anit-clockwise Turn")
                    turn = "anit-clockwise"
    l2 = l1
    return turn

Upvotes: 0

Views: 197

Answers (3)

Holt
Holt

Reputation: 37606

The __name__ == "__main__" condition is True only if you execute the python script by doing:

python pots.py

If you import the package using the import directive, everything in the if is not executed, and thus the rotaryChange function is not defined and cannot be imported.


Basically, you use this conditions to test your package or use it as an external program, without having the whole stuff executed if you (or someone else) import it.

class MyClass: pass

if __name__ == "__main__":
   m = MyClass ()
   # Do some test on m

If you run python myclass.py, then all the code is executed and you can test the behavior of your MyClass class. But if someone import your package using import myclass, you don't want the test code to be executed, so you put it under if __name__ == "__main__":.

Another usage would be to allow users to execute your package as "tool", one example of such package is the timeit module.

# import the timeit module and use it
>>> import timeit 
>>> timeit.timeit ('math.exp(1)', setup = 'import math')
# Use the timeit module from the command line
C:\Users\Holt59 python -m timeit 'math.exp(1)' 'import math'

Upvotes: 4

chepner
chepner

Reputation: 530823

You can, if that statement is executed when the module is imported. However, __name__ == '__main__ is specifically used to prevent code from running unless the module is run as the main script, not when it is merely imported by another module. If you intend for rotaryChange to be used by other modules, it should be defined outside that if statement.

Upvotes: 3

Daniel Roseman
Daniel Roseman

Reputation: 599450

Literally the whole point of the if __name__ == '__main__ block is to provide code that only executes when the file is executed directly, rather than when it is imported.

If you need to import this function, move it outside that block.

Upvotes: 3

Related Questions