簡體   English   中英

將 python 腳本中的變量導入另一個腳本會引發變量未定義的錯誤

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

我目前正在使用帶有 Python 的 WinAppDriver 為專有的 Windows 桌面應用程序編寫自動化腳本。 我們的應用程序讓用戶上傳少量文件,根據上傳的文件進行一些幕后計算,然后輸出結果。 我有使用 UI 上傳這些文件的自動化功能,並且對此沒有任何問題。 執行此操作的過程如下:

  1. 單擊“選擇文件”按鈕。 在彈出窗口中瀏覽到文件位置 window

  2. 單擊“文件名”字段並輸入文件的直接路徑。 單擊確定(這是通過 Python 鍵盤庫完成的)

  3. 對所有必要的文件重復前面的步驟

  4. 點擊“開始”

為了整理我的腳本,我將文件路徑設置為變量,而不是在我的代碼中使用它們的直接路徑。 然后我只需調用我需要的文件的變量名。

例如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')

你有三個選擇:

  1. 使用通配符導入所有內容(即from filePaths import *
  2. 導入 select 對象(即from filePaths import object1, object2, object3 #...
  3. 使用點表示法(即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_1var_2等...

在需要這些變量的腳本中,我將模塊引入如下:

import filePaths

path = filePaths.class_1

當我調用class_1中的一個變量而不是var_1時,我調用path.var_1並且它沒有問題。 謝謝大家幫忙解決這個問題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM