簡體   English   中英

Python-意外的導入行為

[英]Python - Unexpected Import Behavior

我有一個名為Utility.py的文件,該文件是必不可少的菜單,它將調用一組工具。

在該文件夾中有一個core_fuctions與嵌套文件夾文件夾,名為powershell_tools內的powershell_tools有一個名為test_tools.py這是PowerShell的工具菜單中。

每個文件夾中都有__init__.py 當我調用實用程序時,它將立即打開test_tools.py文件,而不是實用程序菜單。 我有一個退出選項,它退出到Utility.py菜單。 我不確定為什么要從那里開始。

這是實用程序文件的縮寫版本:

from core_functions.powershell_tools import test_tools
import os

os.system("cls")
while True:
    print "\n"
    print "Select operation."
    print ....
    print "Press 4 to Use Powershell Tools"
    print "Press 5 to Exit"
    print "\n"

choice = raw_input("Enter your choice: ")

if choice == "1":
    verbose = raw_input("\nWould you like to run in verbose mode? (yes) or no: ")
    if verbose == "no":
        print "hi"
    else:
        print "yup"

elif choice == "4":
    test_tools()

elif choice == "5":
    os.system("cls")
    break

else:
    os.system("cls")
    print "\nPlease enter a valid operation."
    continue

因此,這是我希望在啟動Utility.py時會放下的菜單。 但是,當我啟動Utility.py時,我掉在test_tools.py文件中

test_tools.py看起來像這樣:

from tools.ps_cli import *
from tools.remote_ps_session import *
from tools.ps_clear_remote_dns_cache import *
from tools.ps_get_remote_system_local_users import *
from tools.ps_md5_hash import *
from tools.ps_remote_system_uptime import *
from tools.ps_unlock_account import *

while True:
    print "\n"
    print "Select operation."
    print "Press 1 to test ps_cli.py"
    print "Press 2 to test remote_ps_session.py"
    print "Press 3 to test ps_clear_dns_cache.py"
    print "Press 4 to test ps_get_remote_system_local_users"
    print "Press 5 to test ps_md5_hash"
    print "Press 6 to test ps_remote_system_uptime"
    print "Press 7 to test ps_unlock_account"
    print "Press 'a' to exit"


    choice = raw_input("Enter your choice: ")

    if choice == "1":
        ps_cli()
    elif choice == "2":
        remote_ps_session()
    elif choice == "3":
        ps_clear_remote_dns_cache()
    elif choice == "4":
        ps_get_remote_system_local_users()
    elif choice == "5":
        ps_md5_hash()
    elif choice == "6":
        ps_remote_system_uptime()
    elif choice == "7":
        ps_unlock_account()
    elif choice == "a":
        break


    else:
        os.system("cls")
        print "\n\n\nPlease enter a valid operation."
        continue

我不知道為什么導入會導致它啟動test_tools而不是在Utility.py中按4時無法啟動

以下是行為的屏幕截圖:

在Utility.py啟動時

在此處輸入圖片說明

在按“ a”退出后(應該從此處開始):

在此處輸入圖片說明

嘗試按4返回到test_tools.py后:

在此處輸入圖片說明

因此TL; DR就是這個。 為什么我的程序直接在導入的模塊中啟動,為什么離開后不能返回? 我絕對不是Python的新手,但是以前在導入方面沒有遇到過這個問題-盡管它們並沒有位於多重嵌套目錄中。

謝謝你的幫助!

test_tools是一個模塊,無法調用。 它將在您import時運行,而不是在您執行test_tools()

如果要通過調用函數在該模塊中運行代碼,則需要將該代碼移至函數中。 例:

在實用程序中:

elif choice == "4":
    test_tools.run()
. . . 

在test_tools中:

def run():
    while True:
        print "\n"
        print "Select operation."
        print "Press 1 to test ps_cli.py"
        print "Press 2 to test remote_ps_session.py"
        print "Press 3 to test ps_clear_dns_cache.py"
        print "Press 4 to test ps_get_remote_system_local_users"
        print "Press 5 to test ps_md5_hash"
        print "Press 6 to test ps_remote_system_uptime"
        print "Press 7 to test ps_unlock_account"
        print "Press 'a' to exit"
. . .

暫無
暫無

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

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