Python Forum
passing-arguments-from-one-script-to-another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
passing-arguments-from-one-script-to-another
#1
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)
Reply
#2
Why not just try it?
Reply
#3
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'
Reply
#4
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
Output:
This is a test Are you listening?
Reply
#5
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.
Reply
#6
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.
Reply
#7
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.
Reply
#8
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)
Output:
Enter value: 5 The value is 5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Passing writable arguments to functions. Assembler 11 824 Jan-15-2024, 11:32 PM
Last Post: sgrey
  Passing string functions as arguments Clunk_Head 3 1,209 Jun-15-2022, 06:00 AM
Last Post: Gribouillis
  Passing flags to python script, through a function xbit 4 3,876 Apr-20-2021, 06:32 AM
Last Post: ndc85430
  redirect url_for passing arguments with the url Leon79 1 1,612 Jul-09-2020, 05:20 PM
Last Post: Leon79
  How to write a script to execute a program need passing additional input? larkypython 2 2,471 Nov-23-2019, 04:38 AM
Last Post: larkypython
  Still confused about how passing arguments works RedSkeleton007 3 2,916 Apr-25-2018, 11:01 AM
Last Post: snippsat
  Trouble passing arguments underoathed 2 2,884 Nov-13-2017, 04:20 PM
Last Post: underoathed
  Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) Adelton 2 3,818 Mar-02-2017, 10:23 PM
Last Post: zivoni
  multiprocess passing multiple arguments double asterisk pic8690 1 5,225 Oct-23-2016, 08:51 AM
Last Post: Skaperen
  Effeciency of passing XML tree object as function arguments sandeepvl 3 5,689 Oct-13-2016, 07:58 AM
Last Post: sandeepvl

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020