请教如何在没有安装python的环境中执行py脚本??

RabbitLBJ 2012-03-19 07:39:55
是要自己实现一个python虚拟机吗??
还是只需要将pythonXX.dll拷贝到对方机器,用c语言调用里面的api解释执行就好??

我不需要很多库,基本的几个就够了,望高人指教,谢谢
...全文
5701 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
altairheat 2012-04-04
  • 打赏
  • 举报
回复
打包工具
RabbitLBJ 2012-04-03
  • 打赏
  • 举报
回复
顶下,问题还没解决
libralibra 2012-04-03
  • 打赏
  • 举报
回复
有2个办法将py文件编译为exe,
py2exe和pyinstaller
看下说明搞搞就行了,py2exe用的人多一些
http://goo.gl/hdJXS
RabbitLBJ 2012-03-27
  • 打赏
  • 举报
回复
这个自己顶下,貌似启动Python是加参数-S可以不加载site模块,但别的模块呢??
RabbitLBJ 2012-03-21
  • 打赏
  • 举报
回复

#include <python.h>
#include <stdio.h>
int main()
{
Py_Initialize(); // 检查初始化是否成功
printf("OK\n");
PyRun_SimpleString("print('hello!')");

Py_Finalize();
getchar();
return 0;
}


就上面这样一个很简单的代码,我把编译好的exe和python27.dll一起发布给没安装python的客户机,那边报没导入site错误,请问除了这两个东西,我还要把哪些模块一起发布呢?
bullswu 2012-03-19
  • 打赏
  • 举报
回复
第一个方法就是做成exe文件。
第二种方法我经常使用,就是作为别的语言附属的扩展使用,java,c++的编译器,如eclipse可以做到。
socrates 2012-03-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fibbery 的回复:]

一般情况下,你在发布你的程序的同时,发布一个Python解释器。这种方法是许多应用的办法。比如Oracle会同时将一个Perl的版本一起发布一样。
[/Quote]
++
panghuhu250 2012-03-19
  • 打赏
  • 举报
回复
py2exe是把python interpreter和用到的库打包在一起,放在一个exe文件里。

另外,shedskin把python编译为c++,再编译为原生的可执行文件。但它对python(语言特性和标准库)的支持不完全,能否编译你的程序要你自己去看。



fibbery 2012-03-19
  • 打赏
  • 举报
回复
当然,编译成可执行程序是一个办法,但也可能有些细节事情需要考虑,不见得比同时发布一个解释器程序简单。你自己对比一下吧。
fibbery 2012-03-19
  • 打赏
  • 举报
回复
一般情况下,你在发布你的程序的同时,发布一个Python解释器。这种方法是许多应用的办法。比如Oracle会同时将一个Perl的版本一起发布一样。
daols 2012-03-19
  • 打赏
  • 举报
回复

同上...2.x用py2exe...3.x不详...方法详见:
http://cache.baidu.com/c?m=9d78d513d9810aaf4fece4294741c321590a8f397a9192027ea78e55d6211e564711b2e7777c4d5192d27c1050f21641b2a36a33370122b7869bc81b87e0d67f328825367401844a0fd21bb2cc452ec02b914de9d843a1e5ae&p=8167c54ad6c32db513be9b794652&user=baidu&fm=sc&query=py2exe&qid=f03bff2e07b84f02&p1=1

如果是单文件要打包成单独的exe...网上有个pyfly.py
#-*- coding: utf8 -*-
# windows下为Python脚本生成单独可执行程序的脚本(不用为每个脚本写setup.py了)
# 我们经常要用py2exe将python脚本转换为单独的可执行文件,但是有一个麻烦就是要针对脚本写一个setup.py,在setup.py中指定一些信息,然后运行setup.py py2exe来生成执行文件。为避免每次都要写setup.py,本人编写了一个工具脚本pyfly.py,只要运行pyfly.py yourfile.py就能生成yourfile.exe。还可以为其指定其他参数,比如-i myicon.ico为生成的可执行文件添加图标;-p console指定生成控制台程序,-p windows指定生成windows程序;-c yes指定用upx压缩可执行文件以减小大小,-c no执行不压缩可执行文件;-d out_directory 指定生成的可执行文件的输出目录。(-c yes 选项 需要将upx.exe放置在系统的环境变量PATH所包含的目录中,或者将upx.exe的路径加入PATH中)
# 运行"pyfly.py pyfly.py", 就立即在当前目录下生成pyfly.exe,将pyfly.exe置于环境变量PATH中能找到的位置,然后在任意目录中就可为你的python脚本生成单文件的可执行程序了。

import os,sys,re,time,uuid,subprocess,shutil,string,struct
#,WConio
def printgoodnews(news):
# oldcolor=WConio.gettextinfo()[4]
# WConio.textcolor(WConio.GREEN)
print news
# WConio.textcolor(oldcolor)

def printbadnews(news):
# oldcolor=WConio.gettextinfo()[4]
# WConio.textcolor(WConio.RED)
print news
# WConio.textcolor(oldcolor)

def delete_directory(path):
for root, subdirs,files in os.walk(path,topdown=False):
for file in files:
os.unlink(os.path.join(root,file))
for subdir in subdirs:
os.rmdir(os.path.join(root,subdir))
os.rmdir(path)

usage=r'''
---- Python Convert *.py To *.exe single file on windows ----

usage: '''+sys.argv[0]+''' script.py [-d out_directory] [-i iconfile] [-p console|windows] [-c yes|no]
('-c yes': call upx.exe to compress exe)
e.g. : '''+sys.argv[0]+''' script.py -d "c:\\temp" -i myicon.ico -p console
: '''+sys.argv[0]+''' script.py -d "B:\\" -p console -c yes
'''

setuppy_console="""\
from distutils.core import setup
import py2exe
setup(
version = "",
description = "",
name = r"%s",
options = {"py2exe": {"compressed": 1,
"optimize": 2,
"ascii": 0,
"bundle_files": 1}},
zipfile = None,
# targets to build
console = [{"script": r"%s", "icon_resources": [(1, r"%s")]} ],
)
"""

setuppy_noicon_console="""\
from distutils.core import setup
import py2exe
setup(
version = "",
description = "",
name = r"%s",
options = {"py2exe": {"compressed": 1,
"optimize": 2,
"ascii": 0,
"bundle_files": 1}},
zipfile = None,
# targets to build
console = [{"script": r"%s", } ],
)
"""

setuppy_windows="""\
from distutils.core import setup
import py2exe
setup(
version = "",
description = "",
name = r"%s",
options = {"py2exe": {"compressed": 1,
"optimize": 2,
"ascii": 0,
"bundle_files": 1}},
zipfile = None,
# targets to build
windows = [{"script": r"%s", "icon_resources": [(1, r"%s")]} ],
)
"""
setuppy_noicon_windows="""\
from distutils.core import setup
import py2exe
setup(
version = "",
description = "",
name = r"%s",
options = {"py2exe": {"compressed": 1,
"optimize": 2,
"ascii": 0,
"bundle_files": 1}},
zipfile = None,
# targets to build
windows = [{"script": r"%s", } ],
)
"""

if __name__=="__main__":

if len(sys.argv)==1:
printgoodnews(usage)
raw_input("Press [Enter] for Exit")
sys.exit(-1)

script=sys.argv[1]

platform='console'
out_directory=os.getcwd()
iconfile=""
upx="no"
if script[-3:].lower()!=".py" or not os.path.exists(script):
printbadnews("%s is not a valid python source file or not exists!")
printgoodnews(usage)
raw_input("Press [Enter] for Exit")
sys.exit(-1)


try:
if len(sys.argv)>2 and len(sys.argv)<=8 and len(sys.argv) % 2==0:
options=[item.lower() for item in sys.argv[2::2]]
for option in options:
if option not in ['-p','-d','-i','-c']:
raise Exception(r'invalid option: %s!' % option)
if '-p' in options:
platform=sys.argv[sys.argv.index('-p')+1].lower()
if platform not in ['console','windows']:
raise Exception(r"%s is not a valid platform, you should use 'console' or 'windows'!" % platform)
if '-d' in options:
out_directory=sys.argv[sys.argv.index('-d')+1]
if not os.path.exists(os.path.abspath(out_directory)):
raise Exception(r"%s is not a valid directory!" % out_directory)
if '-i' in options:
iconfile=sys.argv[sys.argv.index('-i')+1]
if not os.path.exists(os.path.abspath(iconfile)) or iconfile[-4:].lower()!='.ico':
raise Exception(r"%s is not a valid icon file name or not exists!" % iconfile)
if '-c' in options:
upx=sys.argv[sys.argv.index('-c')+1].lower()
if upx not in ['yes','no']:
raise Exception(r"%s option must be set to 'yes' or 'no'!" % "-c")
elif len(sys.argv)==2:
pass
else:
raise Exception('Invalid counts of arguments!')
except Exception,e:
printbadnews(str(e))
printgoodnews(usage)
raw_input("Press [Enter] for Exit")
sys.exit(-1)


try:
tempdir=os.environ['temp']

#create setup.py in temp directory
setupfile=os.path.join(tempdir,str(uuid.uuid1())+".py")
while os.path.exists(setupfile):
setupfile=os.path.join(tempdir,str(uuid.uuid1())+".py")
g=open(setupfile,'wb')
if iconfile!="":
g.write(eval('setuppy_%s' % platform) % (os.path.basename(script),script,iconfile))
else:
g.write(eval('setuppy_noicon_%s' % platform) % (os.path.basename(script),script))
g.close()
del g

if " " in out_directory:
out_directory='"'+out_directory+'"'

cmd=r'python "%s" py2exe -O2 -d %s' %(setupfile,out_directory)
#print cmd
if subprocess.call(cmd)==0:
#delete temporary file and 'build' directory
os.unlink(setupfile)
cwd=os.getcwd()
build=os.path.join(cwd,'build')
bdist_win32=os.path.join(build,'bdist.win32')
delete_directory(bdist_win32)
#if build is a blank directory, delete it; otherwise keep it.
i=0
for root,subdirs,files in os.walk(build,topdown=True):
if subdirs!=[] or files!=[]:
i+=1
break
if i==0:
delete_directory(build)
# compress exe
if upx=='yes':
subprocess.call('upx -9 -v "%s.exe"' % os.path.join(out_directory,os.path.basename(script)[0:-3]))

except Exception,e:
printbadnews(str(e))


调用方法, 批处理, p2e.bat :
python.exe pyfly.py "%1" -d "%tmp%" -p console -c yes

把.py文件拖到批处理上即可在%tmp%文件夹生成exe文件..
zengna_com 2012-03-19
  • 打赏
  • 举报
回复
py2exe打包

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧