Hi all..
I need to do a condition check in a script. The same condition check needs to be repeated in another script. So, I am looking for a way, where i can set, say a flag in the first script ,upon meeting the condition, and be able to access that flag in another script, instead of repeating the check all over again.

Hope I am clear in explaining what I want to do. Can anyone pls give me suggestions?

:( Else, can anybody tell me how to import a function from one python script into another?

to import a single function from another script, simply put "from <script> import <function>". You can set vars and consts in another script. Example:

#example.py
a = 10
b = 20
def function( n ):
    n + 20
    return n

#script.py
import example
example.a = 14
example.function(6)

You could save the flag to file and read it in the second script.

Hi all..
U ppl have been of great help to me, guiding me throughout my project :)

well, importing a function from another module is not working for me :(
Pls let me know where am I going wrong. :(

scenario is as such..
My script1.py goes like this


def main():
            ###
            ###
            ....
            ###
            ###



 def mode_check(op_mode):
                #-- 
                ###
                return flag

if __name__== "__main__":
        main()

and when i try importing the functiom mode_check in script2.py

from script1 import *
##########
#########
##########
status=mode_check(mode)
########
############

gives me the following error:

NameError: global name 'mode_check' is not defined

Dont use bold character for everthing.
Here is and example you can look at and try out.

#average.py
#saved in python dir
from __future__ import division

def average(seq_in):
    if isinstance(seq_in, (list, tuple)):
        return sum(seq_in) / len(seq_in)
    else:
        return 'Input can only be list or tuple'

So we save this as average.py in python folder(or a python path folder so python can find it)
Import and test it out before we use it in new script.

>>> from average import average
>>> average
<function average at 0x04D2C430>
>>> average([1,2,3,4])
2.5
>>> average(88)
'Input can only be list or tuple'
>>> average('hi')
'Input can only be list or tuple'
>>>

It work as it should.
Then we can use it in a new code like this.

from average import average

def calc_average(l):
       return average(l) #Here we use the imported code

def radius_2():
    '''Not finish yet'''
    pass

def main():
    '''Main menu'''
    while True:
        print '(1) Calculate average'
        print '(2) Calculate radius'
        print '(Q) Quit'
        choice = raw_input('Enter your choice: ')
        if choice == '1':
            user_in = raw_input('Enter numbers for average calculation: ')
            a = [float(i) for i in user_in]
            print calc_average(a)
        elif choice == '2':
            radius_2()
        elif choice in ['Q','q']:
            return False
        else:
            print 'Not a correct choice:', choice

if __name__ == '__main__':
    main()

Thanks...

will abide by your suggestion of not using bold everywhere :)

I am not clear with this:
So we save this as average.py in python folder(or a python path folder so python can find it)
can u pls tell me more about this?

And...
import in script2 worked fine when mode_check() was defined before main.

Please let me know, why is it wrong to define a function before main, if it is so? and why did it work now?

And...
import in script2 worked fine when mode_check() was defined before main.

Please let me know, why is it wrong to define a function before main, if it is so? and why did it work now?

Your problem is a circular import like this

# in module A
import B

# in module B
import A # or from A import foo

You should not use circular imports, although it works partially. For example if the function foo() is defined in A before the 'import A' is executed in B, the A.foo may work. It's not difficult to create your programs to avoid circular imports. To start with, you should not import a function from the main module in another module.

Another unrelated tip: don't indent python code with tabs. Configure your editor to indent code with 4 spaces (the recommended spacing).

Thanks a lot. Every input is helping me :) But, I am not clear why you mentioned about the circular import here. Does it mean that importing a function from the main module in another module leads to circular import?

Thanks a lot. Every input is helping me :) But, I am not clear why you mentioned about the circular import here. Does it mean that importing a function from the main module in another module leads to circular import?

It necessarily leads to circular import: if script2.py was executed while running script1.py, you must have written 'import script2' somewhere in script1.

A better solution is to create a separate module for the functions which must be used in both scripts

# miscelaneous.py

def mode_check(...):
   ...

# script1.py
from miscelaneous import mode_check

def main():
    ...
...

# script2.py
from miscelaneous import mode_check
...

status = mode_check(mode)

Thanks a lot Gribouillis. I got it now :) :)

Hi guys :

Any help with this please!

script1.py in 192.168.1.10

x = 10

script2.py in 192.168.1.20

if x == 10:

print x

else:

print "sorry"

how can apply this between two scripts in same network.

Thanks for all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.