如何通过传入一串代码为py2exe交互式构建一个setup.py文件?

How to interactively build a setup.py file for py2exe by passing in a string of code?

我知道这可能涵盖了很多不同的领域,除了一个小问题外,我已经设法让 most 正常工作..

假设我有一个名为 make_setup 的函数,如下所示:

def make_setup(_file_, codestring = None):
    if codestring == None:
        #codestring will be a list passed in
        codestring = ["from distutils.core import setup",
                      "import py2exe",
                      "setup(console=["+_file_+"])"]
    else:
        codestring = codestring

    with open('setup.py', 'w+') as f:

        for line in codestring:
            f.write(line+'\n')

    f.close()

然后我有另一个名为 build_exe 的函数,我将 运行 实际上使我的可执行文件。它看起来像这样: 从子进程导入 Popen 导入 os

#build exe
def build_exe(codestring = None):
    if codestring == None:
        codestring = ["python setup.py py2exe"]
    else:
        codetring = codestring

    #write a batch file?
    with open('exe_bat.bat', 'w+') as f:

        for line in codestring:
            f.write(line+'\n')

    f.close()
    #run the batch file
    p = Popen("exe_bat.bat", cwd=os.getcwd())
    stdout, stderr = p.communicate()

基本上,我的想法是我可以将一个 .py 文件传递​​到我的 make_setup 函数中,以便它在写入的 setup.py 文件中正确引用该文件。假设我有一个名为 my_python_code.py 的文件,其中包含创建 .exe 所需的一切。这个文件的内容对于这个例子并不重要。

当我 运行 我的 make_setup 函数通过传入我的文件时:

_file_ = r'my_python_code.py'
make_setup(_file_)

它使用以下代码行创建一个 setup.py 文件:

from distutils.core import setup
import py2exe
setup(console=[my_python_code.py])

请注意,my_python_code.py 位不是字符串。这是我可以寻求帮助的主要问题:

如何将我的文件名传递到我的 make_setup 函数中,以便它正确地创建我的 setup.py 文件:

from distutils.core import setup
import py2exe
setup(console=['my_python_code.py'])

感谢您的帮助!

PS:为了更容易直截了当,我知道问题出在我的 make_setup 函数上,特别是最后一行:

codestring = ["from distutils.core import setup",
                      "import py2exe",
                      "setup(console=["+_file_+"])"]

我试过str(_file_),用双引号""等都没有用。也许使用 inspect 模块获取实际参考 .py 文件并附加到我的代码字符串是一个选项?当我找到解决方案时,我会继续尝试并 post。

更新: 这是我最终得到的结果:

try:
    from cStringIO import StringIO 
except:
    from StringIO import StringIO  

from subprocess import Popen
import os

#make setup.py
def make_setup(_file_, codestring = None):

    def f(_file_): return '\'{0}\''.format(_file_)

    if codestring == None:
        codestring = ["from distutils.core import setup",
             "import py2exe",
             "setup(console=["+f(_file_)+"])"]
    else:
        codestring = codestring

    #this displayes the code 
    output = StringIO()
    for line in codestring:
        #no returns in lines
        output.write(line+'\n')
    print output.getvalue()
    output.close()

    user_response = raw_input("is this correct? y/n")

    if user_response != "y":
        print "try again"
    else:
        with open("setup.py", "w+") as f:
            for line in codestring:
                f.write(line+'\n')
        f.close()

#build exe
def build_exe(codestring = None):
    if codestring == None:
        codestring = ["python setup.py py2exe"]
    else:
        codetring = codestring

    #this displayes the code 
    output = StringIO()
    for line in codestring:
        #no returns in lines
        output.write(line+'\n')
    print output.getvalue()
    output.close()

    user_response = raw_input("is this correct? y/n")

    if user_response != "y":
        print "try again"
    else:        
        #write a batch file?
        with open('exe_bat.bat', 'w+') as f:

            for line in codestring:
                f.write(line+'\n')

        f.close()

    user_response = raw_input("run the batch file? y/n")
    if user_response == 'y':
        #run the batch file
        p = Popen("exe_bat.bat", cwd=os.getcwd())
        stdout, stderr = p.communicate() 
    else:
        pass 

如果您想在字符串中使用引号,则必须将它们放在那里。 Python猜不到你想要什么

value = "a nice string"
statement = "my_string = \"{}\"".format(value)
print(statement)

输出:

my_string = "a nice string"