Python 如何通过传递字符串代码以交互方式为py2exe构建setup.py文件?
我知道这可能涉及到很多不同的领域,我已经设法让它大部分工作,除了一个小问题 假设我有一个名为make_setup的函数,如下所示:Python 如何通过传递字符串代码以交互方式为py2exe构建setup.py文件?,python,string,metaprogramming,Python,String,Metaprogramming,我知道这可能涉及到很多不同的领域,我已经设法让它大部分工作,除了一个小问题 假设我有一个名为make_setup的函数,如下所示: def make_setup(_file_, codestring = None): if codestring == None: #codestring will be a list passed in codestring = ["from distutils.core import 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
导入操作系统
#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文件,如下所示:
谢谢你的帮助
PS:为了便于直截了当,我知道问题在于我的make_设置功能,特别是最后一行:
codestring = ["from distutils.core import setup",
"import py2exe",
"setup(console=["+_file_+"])"]
我尝试了str(_file)
,并使用了双引号“”,等等,但没有成功。也许可以使用inspect模块获取实际的reference.py文件并将其附加到我的代码字符串中?我会继续尝试,找到解决方案后再发布
更新:
以下是我最终得到的结果:
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"
谢谢你,我自己解决后忘了重温。现在我将发布我的工作代码作为示例。
my_string = "a nice string"