passing-arguments-from-one-script-to-another - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: passing-arguments-from-one-script-to-another (/thread-26088.html) |
passing-arguments-from-one-script-to-another - jacklee26 - Apr-21-2020 I have some question related to pass value from file A to File B, and from File B to File A. Do anyone know how it work. In main.py input name, and in second.py enter your age. I main.py i wish to use second.py value, and in second,py wish to use main.py value. Do anyone have any idea? main.py import second string_name = input("please enter your Name : ") print(second.age)second.py import main age = input("please enter your Age : ") print(main.string_name) RE: passing-arguments-from-one-script-to-another - deanhystad - Apr-21-2020 Why not just try it? RE: passing-arguments-from-one-script-to-another - jacklee26 - Apr-21-2020 hi deanhystad, i try it but not working File "main.py", line 1, in <module> import second File "C:\second.py", line 1, in <module> import main File "C:\main.py", line 3, in <module> print(second.age) AttributeError: module 'second' has no attribute 'age' RE: passing-arguments-from-one-script-to-another - deanhystad - Apr-21-2020 I just did this: page1.py value = 'This is a test' def echo(): print(value)page2.py import page1 print(page1.value) page1.value = 'Are you listening?' page1.echo()When I run the page2.py script it prints
RE: passing-arguments-from-one-script-to-another - jacklee26 - Apr-21-2020 Hi deanhystad, i know this will work if me import page1 in and used the value. But i wish page1 can use or get page2's value, and page2 can used or get page1's value. Do you know any way can do that it. RE: passing-arguments-from-one-script-to-another - ndc85430 - Apr-21-2020 It looks like you have a circular dependency between your modules. Why do you need to write them that way? Also, it would be best to use functions for code you want to reuse, since you can control when that code is executed, rather than it being done at module loading time. RE: passing-arguments-from-one-script-to-another - jacklee26 - Apr-21-2020 HI , this is my code. The reason is i want user to input the mac address, and then it will send the mac address value to another file to do telnet to get ip address from the server. This is the reason wy i need two file. main.py [user will enter the mac address] import os MACADD = input("please enter your MAC ")telnet.py [it will telnet to server and find the mac address user enter, to find the IP address] import main ip ="192.168.1.252" username = "guest" password = "guest" ########################## mac=main.MACADD ########################## new_IPv4 = telnet_To_CMTS(ip, username, password, mac) print (new_IPv4)Step: 1. Go to main.py =>user enter mac address of a device and it will go to telnet.py to send the mac address 2. telnet.py to search mac address to find the ip address. After finding IP address wish to return back to main.py 3. main.py will print IP address of the MAC Devices So this is why i want to do like this. Due to i have two script which does different function. Is there any suggestion i can do. RE: passing-arguments-from-one-script-to-another - deanhystad - Apr-21-2020 Without seeing all your code I cannot think of a reason why you are using main.py. Why doesn't telnet.py ask for the mac address? However, from the info provided in your last post I see no reason why your code does not work. Essentially it is the same as this. page1.py value = input('Enter a value:')page2.py import page1 print('The value is', page1.value) I re-read your posts more carefully this time. Upon further reflection it sounds like there is no need for telnet.py. You should write a function that does what telnet.py does. Actually you should write a function that inputs the mac address, computes the IP address, and returns the computed IP address. Your main program would call this function just like any of the standard functions. Now you only have 1 module and don't have to worry about global variables.
|