-1

I'm working on a project that takes data from excel and enters it into SAP. Everything works quite well until I try to import some variables between files.

output.py:

import openpyxl
import time
from data import *
import subprocess

wb = openpyxl.load_workbook("OutputProject.xlsm")
ws = wb.active
List = wb["List"]
Data = wb["Data"]

count = 0
for row in ws:
    if not all([cell.value == None for cell in row]):
        count += 1
count = count - 1

print(count)
i = 2
while i < count + 2:
    vendornumber = List.cell(i,1).value  
    countrycode = List.cell(i,2).value
    companycode = eval(countrycode + "_cc")
    market = eval(countrycode + "_market")

    asd = ['python', 'test2.py']
    subprocess.Popen(asd).wait()
    i = i + 1
    time.sleep(1)

print("Done")   

test2.py:

import sys
import win32com.client
import time
import subprocess
from output import *

def sap():
    try:
        path = r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe"
        subprocess.Popen(path)
        time.sleep(10)

        SapGuiAuto = win32com.client.GetObject('SAPGUI')
        if not type(SapGuiAuto) == win32com.client.CDispatch:
            return

        application = SapGuiAuto.GetScriptingEngine
        if not type(application) == win32com.client.CDispatch:
            SapGuiAuto = None
            return
        connection = application.OpenConnection("ECC Production", True)

        if not type(connection) == win32com.client.CDispatch:
            application = None
            SapGuiAuto = None
            return

        session = connection.Children(0)
        if not type(session) == win32com.client.CDispatch:
            connection = None
            application = None
            SapGuiAuto = None
            return
    except:
        print(sys.exc_info()[0])
        print(sys.exc_info()[1])
        print(sys.exc_info()[2])

    finally:
        session = None
        connection = None
        application = None
        SapGuiAuto = None
  sap()

Code works perfectly without below line:

from output import *

However I need to import variables defined in output file and this line makes the code loop and doing print(count) over and over again. Can someone explain why is it happening?

Thanks!

  • Hi, welcome to Stack Overflow. Please read https://stackoverflow.com/help/minimal-reproducible-example and https://stackoverflow.com/help/how-to-ask. In this example, we need to see your current code, and a better description of what you mean. You need to important `arguments`? Or `functions`? Define an `argument` here. There is no way we can see why that is happening without your code. – artemis Jan 23 '20 at 14:20
  • Thanks for your comments! I uploaded full code to give you better insight on what's happening. Apologies for not asking the question properly, I am still very new to the python and many things confuse me. I meant to import variables, not arguments. – Lord And Savior Jan 23 '20 at 14:37

1 Answers1

0

The reason it runs your code is because when you import a python file, it will run the moudule. In order to stop this, you want to put your code in a main() function. Then you want to do this:

def main(): 
  pass
if __name__ == '__main__':
  main()

What happens is if the name of the file being ran is main, then it will run the main() function, however, when it is being imported, it wont be ran.