[英]Importing variables from python script into another script is throwing errors that variables are undefined
我目前正在使用带有 Python 的 WinAppDriver 为专有的 Windows 桌面应用程序编写自动化脚本。 我们的应用程序让用户上传少量文件,根据上传的文件进行一些幕后计算,然后输出结果。 我有使用 UI 上传这些文件的自动化功能,并且对此没有任何问题。 执行此操作的过程如下:
单击“选择文件”按钮。 在弹出窗口中浏览到文件位置 window
单击“文件名”字段并输入文件的直接路径。 单击确定(这是通过 Python 键盘库完成的)
对所有必要的文件重复前面的步骤
点击“开始”
为了整理我的脚本,我将文件路径设置为变量,而不是在我的代码中使用它们的直接路径。 然后我只需调用我需要的文件的变量名。
例如file_to_upload_1: str = r”C:\Users\user\...\filename.txt
我创建了一个单独的filePaths.py
,其中存储了所有这些设置为变量的文件路径,因此将来添加/修改它们很容易,而且都在一个地方。
我遇到的所有这些问题是当我import
这个包含设置为变量的文件路径的.py
时。 现在,为了简单起见,我正在from filePaths import *
做。 这通常是不受欢迎的,VS Code 会向我发出警告,建议我导入了未使用的导入。 我继续将变量设置为单独的类,然后尝试通过以下方式导入它们: from filePaths import dataset_1
当我这样做时,我收到以下错误: Undefined variable “variable_name”
并且我的测试无法运行。 似乎只有导入所有内容才能使这一切正常工作,并且如果可能的话,我想避免这样做。 我所有的脚本都在同一个目录中。 我在这里想念什么?
代码示例:
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')
你有三个选择:
from filePaths import *
)from filePaths import object1, object2, object3 #...
)import filePaths
然后filePaths.object1 #etc
)有些选项可能被认为比其他选项更好的编程风格。
通配符起作用的原因是,如果您在 import 语句的filePaths
中列出了所有创建的对象,则它与上面的选项 2 相同。 通常,您应该选择性地仅导入您需要的方法和对象,或者只导入脚本并使用点表示法根据需要选择性地使用方法和对象。
以下示例代码显示了如何使用点表示法。
文件 1:
# objects_to_import.py
bob = 127
string = 'my string'
def foo():
print('bar')
def bar():
print('foo')
def print_var(var):
print(var)
文件 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!")
然后,运行 main.py 输出:
"""
127
127
bar
foo
You didn't import that variable or use correct notation!
"""
如果 main.py 改为读取,结果是相同的:
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!")
请注意,如果我们将以下代码添加到两个版本的 main.py:
if('bob' in globals()):
print('Bob is in your globals!')
else:
print("Can't find bob in your globals")
我们发现bob
在显式导入时位于您的全局空间中,但在将点表示法与一般非显式导入语句一起使用时不存在。 因此,选择一种导入方法而不是另一种可能有实际的原因(例如,如果您的程序又长又复杂,并且您希望更轻松地管理潜在的名称冲突,则应该使用点表示法)。
好吧,我想出了一个解决方案!
我的filePaths.py
模块带有class_1
,其中包含一组特定变量:分别为var_1
、 var_2
等...
在需要这些变量的脚本中,我将模块引入如下:
import filePaths
path = filePaths.class_1
当我调用class_1
中的一个变量而不是var_1
时,我调用path.var_1
并且它没有问题。 谢谢大家帮忙解决这个问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.