2013-08-21 152 views
2

我有一個PyInstaller的跨平臺GUI界面。我想檢查已安裝的PyInstaller的版本。用戶提供PyInstaller的安裝文件夾。我可以通過命令行python pyinstaller --version執行此操作,但是這會在Windows上打開我想要避免的命令提示符。有沒有一種方法可以訪問Python模塊中的get_version()函數,而不是正在運行的應用程序所在的路徑?或者,如何防止在Windows上啓動命令提示符,同時保持我的應用程序的跨平臺兼容性。從用戶指定的目錄中導入一個python模塊?

這裏是我到目前爲止的代碼:

def pyversion(installdir): 
    flags=[] 
    flags.append(sys.executable) 
    pylocation = os.path.join(installdir, 'pyinstaller.py') 
    flags.append(pylocation) 
    flags.append('--version') 
    p = subprocess.Popen(flags, 
         stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 
    out,err = p.communicate() 
    return float(out.strip()[:3]) 
+1

如果GUI什麼正在安裝'pyinstaller'的同一個python運行,然後你可以'從Pyinstaller import get_version; version = get_version()'。或修改'sys.path'從目錄中導入假設版本兼容 – jfs

回答

2
my_path = "some/path/blah" 
os.path.insert(0,my_path) 
import my_custom_module 

你只需要導入前將其添加到路徑,或者......我不明白你問

+2

您可能意味着'sys.path.insert(0,my_path的)'(事後宣稱,該模塊是從路徑進口(檢查'.__ file__'或類似)) – jfs

+0

你是對的。 :)我的部分忽略 –

+0

os.path.append不存在。 sys.path.insert工作!謝謝。 – pedram

相關問題