【问题标题】:Gooey python behaviour when launched from another file从另一个文件启动时粘糊糊的 python 行为
【发布时间】:2015-07-31 18:26:10
【问题描述】:

所以,我正在尝试整理一个基本的“版本控制”脚本供个人使用。它适用于命令行,但后来我决定加入一个 GUI。一切都开始变得奇怪了。

我正在使用 python 2.7.9 和可在https://github.com/chriskiehl/Gooey 获得的粘性版本

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

但是,当我尝试启动整个程序时,我在窗口中输入信息,点击开始,它只会弹出另一个粘糊糊的窗口,再次询问信息,依此类推,直到我决定关闭现在打开的 400 个窗口。似乎每次,它都会重新启动整个 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 中,每次我输入参数并点击开始时,它就会崩溃并开始吐出一个窗口。

我觉得这真的很令人沮丧和好奇。而且我一生都无法弄清楚它为什么会这样做。

所以,我来找你了:到底为什么会发生这种情况?

如果我的问题似乎缺少任何细节,或者不够明确/研究/详细,请告诉我,我会适当修改。

【问题讨论】:

    标签: python-2.7 python-module


    【解决方案1】:

    所以,原来这是Gooey 模块的限制。

    我将发布此模块创建者的答案:

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

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

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

    你也可以看到我在github上发的issue

    【讨论】:

      猜你喜欢
      • 2016-01-13
      • 2017-07-05
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 2016-08-21
      • 2017-03-17
      相关资源
      最近更新 更多