保存了python文件怎样在python中拿出来

保存了python文件怎样在python中拿出来,第1张

1.python文件保存之后默认的文件类型都是py,而py文件直接双击去点它的话是不能够将其打开的,而是会出现提示找不到可以打开这个文件的应用程序。

这是因为python在安装之后虽然会带一个默认的编辑器IDLE以及解释器python.exe,但是这两个程序都不是python文件的默认打开方式和执行方式。

2.所以要打开保存后的python文件的话有两种方式,第一种就是右键它然后选择打开方式,在打开方式里面选择记事本这个应用程序将其打开就行,因为python文件实际上也是一个文本类型文件。如果不用记事本的话就需要点击其他程序,然后找到pycharm或者是IDLE之类的python编辑器来将其打开了。

第二种方式的话就是将py后缀改成pyw后缀,这样它就会被视为经过编译后的python文件,双击的话会直接调用默认文本编辑器打开它

3.如果是要打开python文件并且运行的话也有几种方式,打开python编辑器然后将python文件拖进去就能自动将其打开,然后按下shift+F10快捷键就能运行python文件了。

一.pyinstaller简介

pyinstaller将Python脚本打包成可执行程序,使在没有Python环境的机器上运行

最新版是pyinstaller 3.1.1。支持python2.7和python3.3+。

可运行在Windows,Mac和Linux *** 作系统下。

但它不是跨编译的,也就是说在Windows下用PyInstaller生成的exe只能运行在Windows下,在Linux下生成的只能运行在Linux下。

二.pyinstaller在windows下的安装

使用命令pip install pyinstaller即可

在windows下,pyinstaller需要PyWin32的支持。当用pip安装pyinstaller时未找到PyWin32,会自动安装pypiwin32

出现Successfully installed pyinstaller-3.1.1 pypiwin32-219即表示安装成功

三.打包

打包的app里并不包含任何源码,但将脚本的.pyc文件打包了。

基本语法:

pyinstaller options myscript.py

常用的可选参数如下:

--onefile 将结果打包成一个可执行文件

--onedir 将所有结果打包到一个文件夹中,该文件夹包括一个可执行文件和可执行文件执行时需要的依赖文件(默认)

--paths=DIR 设置导入路径

--distpath=DIR 设置将打包的结果文件放置的路径

--specpath=DIR 设置将spec文件放置的路径

--windowed 使用windows子系统执行,不会打开命令行(只对windows有效)

--nowindowed 使用控制台子系统执行(默认)(只对windows有效)

--icon=<FILE.ICO>将file.ico添加为可执行文件的资源(只对windows有效)

如pyinstaller --paths="D:\Queena" guess_exe.py

四.小实例(windows下)

写好游戏文件guess_exe.py,代码如下:

__author__ = 'qa-2'

# -*- coding:utf-8 -*-

# 摇3次骰子,当总数total,3<=total<=10时为小,11<=total<=18为大

import random

import time

def enter_stake(current_money):

'''输入小于结余的赌资及翻倍率,未考虑输入type错误的情况'''

stake = int(input('How much you wanna bet?(such as 1000):'))

rate = int(input("What multiplier do you want?你想翻几倍?(such as 2):"))

small_compare = current_money <stake * rate

while small_compare == True:

stake = int(input('You has not so much money ${}!How much you wanna bet?(such as 1000):'.format(stake * rate)))

rate = int(input("What multiplier do you want?你想翻几倍?(such as 2):"))

small_compare = current_money <stake * rate

return stake,rate

def roll_dice(times = 3):

'''摇骰子'''

print('<<<<<<<<<<Roll The Dice! >>>>>>>>>>')

points_list = []

while times >0:

number = random.randrange(1,7)

points_list.append(number)

times -= 1

return points_list

def roll_result(total):

'''判断是大是小'''

is_big = 11 <= total <= 18

is_small = 3 <= total <= 10

if is_small:

return 'Small'

elif is_big:

return 'Big'

def settlement(boo,points_list,current_money,stake = 1000,rate = 1):

'''结余'''

increase = stake * rate

if boo:

current_money += increase

print('The points are ' + str(points_list) + ' .You win!')

print('You gained $' + str(increase) + '.You have $' + str(current_money) + ' now.' )

else:

current_money -= increase

print('The points are ' + str(points_list) + ' .You lose!')

print('You lost $' + str(increase) + '.You have $' + str(current_money) + ' now.' )

return current_money

def sleep_second(seconds=1):

'''休眠'''

time.sleep(seconds)

# 开始游戏

def start_game():

'''开始猜大小的游戏'''

current_money = 1000

print('You have ${} now.'.format(current_money))

sleep_second()

while current_money >0:

print('<<<<<<<<<<<<<<<<<<<<Game Starts! >>>>>>>>>>>>>>>>>>>>')

your_choice = input('Big or Small: ')

choices = ['Big','Small']

if your_choice in choices:

stake,rate = enter_stake(current_money)

points_list = roll_dice()

total = sum(points_list)

actual_result = roll_result(total)

boo = your_choice == actual_result

current_money = settlement(boo,points_list,current_money,stake,rate)

else:

print('Invalid input!')

else:

sleep_second()

print('Game Over!')

sleep_second(2)

if __name__ == '__main__':

start_game()

之后命令行,切换到guess_exe.py的目录下,

输入:

pyinstaller --onefile --nowindowed --icon=" D:\Queena\PyCharmProjects\dist1\computer_three.ico" guess_exe.py

就会在当前文件下形成build文件夹、dist文件夹和.spec文件。

dist里就是guess_exe.exe可执行文件。

如果有打包错误,具体看build里的warn*.txt文档,里面详细记载了错误的原因。一般都是库丢失。

spec文件告诉PyInstaller如何去处理脚本。它对脚本名以及大多数pyinstaller的可选参数进行加密。PyInstaller就是通过执行spec文件的内容来build the app。

打开python根据提示步骤 *** 作就行。

1、打开python根目录(右击程序,不是开始菜单程序组,打开在右击程序也行)的Scripts文件夹(复制路径)在cmd中输入cd+一个空格+路径(不带+号),输入pythonpywin3postinstallpyinstall回车,安装pyinstaller;2、win+r打开运行输入cmd,回车;3、输入pip,install,pyinstaller,回车。复制pyinstaller压缩包内文件到某个文件夹,cmd,输入cd+一个空格+文件夹路径(不带+号)回车,输入pythonsetup,py,install回车。

Python提供了高效的高级数据结构,还能简单有效地面向对象编程。Python语法和动态类型,以及解释型语言的本质,使它成为多数平台上写脚本和快速开发应用的编程语言,随着版本的不断更新和语言新功能的添加,逐渐被用于独立的、大型项目的开发。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/11281901.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存