从另一个文件启动时的 Gooey python 行为

Gooey python behaviour when launched from another file

所以,我正在尝试整理一个基本的 "versionning" 脚本供个人使用。它在命令行上运行良好,但后来我决定加入一个 GUI。一切都开始变得奇怪了。

我正在使用 python 2.7.9 和 https://github.com/chriskiehl/Gooey

上可用的 gooey 版本

问题是解析器本身工作正常,它获取正确的数据,returns它,当我只启动我的 parse.py 文件时它就可用。 Gui window 打开,我输入信息,它启动,我的调试打印显示正确的信息。

然而,当我尝试启动整个程序时,我在 window 中输入了信息,点击开始,它只是让另一个 Gooey window 弹出,询问信息再次,依此类推,直到我决定关闭现在打开的 400 windows。似乎每次都会重新启动整个 Gooey 过程。

用于解析的函数:

import argparse
from gooey import Gooey
from gooey import GooeyParser

@Gooey
def initOptParser():
    defaultFolderPath = "./TEST"
    archivePath = "./Archives"

    parser = argparse.ArgumentParser(description="Copy files to specified location")
    parser.add_argument('files', metavar='file', type=str, nargs='+',
                         help='file(s) or directory that you wish to copy' )
    parser.add_argument('-p', '--path', metavar='path',dest='path',  type=str, 
                        help='Path which will be considered as root of all projects. Default is currently : ' + defaultFolderPath, 
                        default=defaultFolderPath)
    parser.add_argument('projectName', metavar='projectName', type=str, 
                        help='The project you wish to retrieve/add files to/from.')
    parser.add_argument('-v', '--version', metavar='versionName', type=str,
                        help='The name of the directory which will be created during an update. By default, this will be : DATE_TIME.')
    parser.add_argument('-o', '--overwrite',
                        help='Overwrites the files in the current version of specified project. (no new directory creation)', action="store_true")
    parser.add_argument('-m', '--message', metavar='logMessage', type=str, help='Use to specify a log message which will be put in a .txt file. (default : commit.txt)')
    parser.add_argument('-g', '--get', dest='method', action='store_const', help='Retrieve files from project (last version by default)', const=get, default=push)
    parser.add_argument('-l', '--lock', action="store_true",
                        help='Locks the current project. Can be overriden/ignored on demand. (Will ask other users if they want to pull files)'+ 
                            'Unlocked when next push from the locking user is made')
    parser.add_argument('user', metavar='userName', type=str, help='Specify your name.')
    parser.add_argument('-a', '--archive', metavar='archivePath', type=str, help='Use to specify a directory which will be used as an archive. Current default is : ' + archivePath)
    parser.add_argument('-s', '--store', metavar="destPath", help='Use to specify where you want the files retrieved from the project to be copied.')

    args = parser.parse_args()
    printOptions(args) # Just a basic function with a few prints to make sure data is there
    args.method() # Either get() or push(). For now i'm using two factice functions who just print their name.
    return (args)

initOptParser()

如果我们坚持这一点,所有数据都会保持正常,我可以访问它,一切都很好。

但是,当我尝试简单地执行此操作时:

import sys
sys.path.insert(0, 'C:/Users/USER/Projects/pythonFileScript/srcs')
import parse

def start():
    parse.initOptParser()

parse.py 在 srcs 目录中,launch.py 在 pythonFileScript 中,每次我进入参数并点击开始。

我发现这真的很令人沮丧和好奇。而且我一辈子都弄不明白为什么要这样做。

所以,我来找你:到底为什么会这样?

如果我的问题似乎遗漏了任何细节,或者不够explicit/researched/detailed,请告诉我,我会适当修改。

原来这是 Gooey 模块的限制。

我会post这个模块的创建者的答案:

Gooey 获取 sys.argv[0] 指向的任何模块并将其副本存储在临时目录中。但!在将其保存到磁盘之前,它会删除对自身的所有引用(例如 Gooey 装饰器)。这允许 Gooey 然后通过 Popen 之类的东西调用你的文件,而不会触发它自己的另一个实例(你看到的行为)。由于装饰器不在顶层模块中,Gooey 不知道将其剥离,因此每次调用脚本时都会再次触发。

TL;DR --> Gooey 的解析器必须在 __main__ 所在的文件中。您的程序的入口点。

有关更多详细信息,请发表评论,我会尽力将您发送给创作者或尽我所能回答。

您还可以看到我 post 在 github 上编辑的 issue