简体   繁体   中英

Importing variables from python script into another script is throwing errors that variables are undefined

I am currently writing automation scripts for a proprietary Windows desktop application at work using WinAppDriver with Python. Our application has the user upload a handful of files, does some behind the scenes calculating based on the files uploaded and then spits out results. I have automation in place that uploads these files using the UI and am not having any issues with this specifically. The process to do this is as follows:

  1. Click the 'Choose File' button. Browse to file location in pop up window

  2. Click in 'File Name' field and input the direct path to the file. Click OK (This is being done with the Python Keyboard library)

  3. Repeat previous steps for all necessary files

  4. Click 'Go'

To tidy up my scripts, I have set the file paths to variables instead of using their direct paths in my code. Then I just call the variable name for the file I need.

Eg file_to_upload_1: str = r”C:\Users\user\...\filename.txt

I have created a separate filePaths.py where all these file paths set to variables are stored so adding/modifying them in the future is easy and all in one place.

The issue that I am running into with all of this is when I import this .py that contains my file paths set to variables. Right now, I am doing from filePaths import * for simplicity sake. This is generally frowned upon and VS Code throws warnings at me advising I have imported unused imports. I went ahead and set my variables to separate classes and then tried to import them in the following way: from filePaths import dataset_1 When I do this I get the follow error: Undefined variable “variable_name” and my tests fail to run. It seems like I can only get this all to work if I import everything and I would like to avoid doing that if possible. All my scripts are in the same directory. What am I missing here?

Sample of code:

from filePaths import * <-- THIS WORKS!
# from filePaths import class_1 <-- THIS DOES NOT

#Open App
desired_caps = {}
desired_caps["app"] = "C:\\Users\\Public\\Desktop\\Application_Being_Tested.lnk"
driver = webdriver.Remote("http://127.0.0.1:4723", desired_caps)

#Login
driver.find_element_by_accessibility_id("Username").send_keys("tester")
driver.find_element_by_accessibility_id("UserPassword").send_keys("password")
driver.find_element_by_accessibility_id("btnLogin").click()

###Upload Files###

#First File To Upload
driver.find_element_by_accessibility_id("ChooseFile").click()
time.sleep(.1)
driver.find_element_by_accessibility_id("FileName").click()
keyboard.write(filePaths_variable)
keyboard.press_and_release('enter')

You have three options:

  1. Import everything using the wildcard (ie from filePaths import * )
  2. Import select objects (ie from filePaths import object1, object2, object3 #... )
  3. Use dot notation (ie import filePaths then filePaths.object1 #etc )

Some options may be considered better programming style than others.

The reason the wildcard works is because it is the same as option 2 from above if you had listed all created objects within filePaths on you import statement. In general, you should either selectively import only the methods and objects you need, or just import the script and use dot notation to selectively use methods and objects as needed.

The following example code shows how to use dot notation.

file 1:

# objects_to_import.py

bob = 127
string = 'my string'

def foo():
    print('bar')

def bar():
    print('foo')

def print_var(var):
    print(var)

file 2:

# main.py in the same directory as objects_to_import.py

import objects_to_import

print(objects_to_import.bob)
objects_to_import.print_var(objects_to_import.bob)
objects_to_import.foo()
objects_to_import.bar()

try:
    print(string)
except NameError:
    print("You didn't import that variable or use correct notation!")

Then, running main.py outputs:

"""
127
127
bar
foo
You didn't import that variable or use correct notation!
"""

The results are identical if main.py instead read:

from objects_to_import import bob, foo, bar, print_var

print(bob)
print_var(bob)
foo()
bar()

try:
    print(string)
except NameError:
    print("You didn't import that variable or use correct notation!")

Note the if we add the following code to both versions of main.py:

if('bob' in globals()):
    print('Bob is in your globals!')
else:
    print("Can't find bob in your globals")

We find that bob is in your globals' space when explicitly imported, but is not present when using dot notation with the general non-explicit import statement. There therefore might be pragmatic reasons to choose one import method over the other (eg if you program is long and complex and you would like to more easily manage potential name collisions, you should use dot notation).

Alright I've come up with a solution!

I have my filePaths.py module with class_1 in there containing a set of certain variables: var_1 , var_2 , etc. respectively...

In my script that wants these variables, I'm bringing the module in like so:

import filePaths

path = filePaths.class_1

When I call one of the variables in class_1 instead of just var_1 I call path.var_1 and it comes in with no issues. Thank you everyone for helping out with this!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM