Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my imported script 'scan.py' I want to refer to a value of a variable created and initialized before in the main script. Here's the code from the main script:
Input = EJ.get()
       os.system ('python scan.py')
       FileName = str(r"C:\\Users\\Admin\\Documents\\HPython\\WESMFG\\POnumber\\" + Input + ".pdf")


and the relevant code of the scan.py script:
output_file = Input
print("Output file: %s" % output_file)
.
.
.
print("Writing output file ...")
img = scan_session.images[0]
img.save(output_file, "JPEG")
print("Done")

'Input' stores the Input of the Entry widget EJ from my Tkinter GUI. So I want the user to type in something, so this input gets the name of the new scanned document.
When running the main script, it tells me 'Input' is not defined.
Since I'm kind of new to programming on python, maybe I'm falsely ignoring
if __name__ == "__main__":
    pyinsane2.init()
    try:
        main()
    finally:
        pyinsane2.exit()
at the end of scan.py. Does this matter in any way to my question?

What I have tried:

import __main__

main_global1= __main__.global1
but since 'Input' is in a function it tells me there is no variable in main called 'Input'
Posted
Updated 2-Mar-18 23:02pm
v4

1 solution

You cannot use a local variable in a program that is running in its own environment. You need to pass the value as a parameter on your system call; see 29.1. sys — System-specific parameters and functions — Python 3.6.4 documentation[^]

Your code should be:
Python
Input = EJ.get()
# do not use 'r' prefix if the string uses escape sequences
FileName = str("C:\\Users\\Admin\\Documents\\HPython\\WESMFG\\POnumber\\" + Input + ".pdf")
os.system ('python scan.py ' + FileName)
 
Share this answer
 
v3
Comments
Member 13641865 3-Mar-18 12:46pm    
Thanks running this shows no error but it doesn't run the os.system ('python scan.py' + FileName) line.

if not os.path.exists(FileName):
os.system ('python scan.py ' + FileName)
msg4 = messagebox.showinfo('Scanner', 'A new document was created!')

Any syntax misatakes here? Or is the command just wrong ?
Richard MacCutchan 3-Mar-18 12:49pm    
I can only assume that the file already exists. Should be easy to check.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900