今天是2024年9月18日,我想入门python,开始搜搜,发现网上很多教程很啰嗦而且很多过时了,我就想先打印一个Hello World。我想要简单不想要复杂,于是有了这篇文章。
要想实现这个功能,首先需要下载一个软件能写代码,然后还需要一个编译器把我写的代码编译为电脑能读懂的东西,最后再运行输出结果。
幸运的是VSCode软件都能完成这些工作。VScode是什么,她有什么好处我就不多说了,谁用谁知道。我主要记录下与python有关的插件和怎么编译运行python。
1. 编写代码
首先安装一个插件:
点击安装就行,一键操作非常简单。
然后就可以高亮语法了,那么新建一个代码文件,几乎所有软件的新建文件都如出一辙。
敲入以下示例代码,下面代码打印了100以内的质数,最后输出了Hello World!
sieve= [True]*101
for i in range(2,100):
if sieve[i]:
print(i)
for j in range(i*i,100,i):
sieve[j]=False
print('Hello World!')
input("按任意键结束")
2. 编译代码
重点来了,怎么编译和运行呢?
看下图:
点了以后,会报错说没有python编译器,of course,我又没装过,正在我犯愁怎么安装的时候,我点了确定,出现了这个弹窗:
我想了想我肯定是未安装,所以就点了未安装python,您猜怎么着,瞌睡送枕头来了:
Windows Microsoft Store直接弹出来了,又是一键安装,搞定了。
有点像对着AI喊:给我把python编译器安装好!然后她就安装好了的感觉~~
3. 调试代码
最后就是运行了,按下F5,或者点下图里的圈圈:
接着如下图所示,点击“python文件”
见证奇迹的时候到了:
终端没有自动弹出来的,可以点这个:
4. 生成可执行文件
我们需要将上面的代码打包生成可执行文件,在Windows系统下就是.exe文件。
exe全称“executable”,中文意思为“可执行的”,是一种文件格式。
exe文件是可移植可执行(PE)文件格式的文件,它可以加载到内存中,并由操作系统加载程序执行,是一种可在操作系统存储空间中浮动定位的可执行程序。
首先我们还是需要安装一个软件,在终端使用命令行安装就行,敲入以下指令:
pip.exe install pyinstaller
正常的话会打印如下结果:
Collecting pyinstaller
Downloading pyinstaller-6.10.0-py3-none-win_amd64.whl (1.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.3/1.3 MB 7.1 MB/s eta 0:00:00
Requirement already satisfied: setuptools>=42.0.0 in c:\users\administrator\appdata\local\programs\python\python310\lib\site-packages (from pyinstaller) (58.1.0)
Collecting altgraph (from pyinstaller)
Downloading altgraph-0.17.4-py2.py3-none-any.whl (21 kB)
Collecting pyinstaller-hooks-contrib>=2024.8 (from pyinstaller)
Downloading pyinstaller_hooks_contrib-2024.8-py3-none-any.whl (322 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 322.8/322.8 kB 6.7 MB/s eta 0:00:00
Collecting packaging>=22.0 (from pyinstaller)
Downloading packaging-24.1-py3-none-any.whl (53 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 54.0/54.0 kB 2.7 MB/s eta 0:00:00
Collecting pefile>=2022.5.30 (from pyinstaller)
Downloading pefile-2024.8.26-py3-none-any.whl (74 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 74.8/74.8 kB 4.0 MB/s eta 0:00:00
Collecting pywin32-ctypes>=0.2.1 (from pyinstaller)
Downloading pywin32_ctypes-0.2.3-py3-none-any.whl (30 kB)
Installing collected packages: altgraph, pywin32-ctypes, pefile, packaging, pyinstaller-hooks-contrib, pyinstaller
Successfully installed altgraph-0.17.4 packaging-24.1 pefile-2024.8.26 pyinstaller-6.10.0 pyinstaller-hooks-contrib-2024.8 pywin32-ctypes-0.2.3
安装好以后,就可进行打包生成exe了,先改个名字,把代码文件名从Untitled-1.py改为demo1.py,然后再命令窗中敲入以下指令:
pyinstaller -F demo1.py
正常的话会打印如下结果:
119 INFO: PyInstaller: 6.10.0, contrib hooks: 2024.8
119 INFO: Python: 3.10.5
125 INFO: Platform: Windows-10-10.0.22000-SP0
125 INFO: Python environment: C:\Users\Administrator\AppData\Local\Programs\Python\Python310
125 INFO: wrote C:\xxx\demo1.spec
127 INFO: Module search paths (PYTHONPATH):
['C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\Scripts\\pyinstaller.exe',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\DLLs',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310',
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages',
'C:\\Desktop\\MATH\\others']
348 INFO: checking Analysis
349 INFO: Building Analysis because Analysis-00.toc is non existent
349 INFO: Running Analysis Analysis-00.toc
349 INFO: Target bytecode optimization level: 0
349 INFO: Initializing module dependency graph...
350 INFO: Caching module graph hooks...
360 INFO: Analyzing base_library.zip ...
749 INFO: Processing standard module hook 'hook-heapq.py' from 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'
783 INFO: Processing standard module hook 'hook-encodings.py' from 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'
1383 INFO: Processing standard module hook 'hook-pickle.py' from 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'
1980 INFO: Caching module dependency graph...
2038 INFO: Looking for Python shared library...
2075 INFO: Using Python shared library: C:\Users\Administrator\AppData\Local\Programs\Python\Python310\python310.dll
2075 INFO: Analyzing C:\xxx\demo1.py
2076 INFO: Processing module hooks (post-graph stage)...
2079 INFO: Performing binary vs. data reclassification (2 entries)
2083 INFO: Looking for ctypes DLLs
2084 INFO: Analyzing run-time hooks ...
2085 INFO: Including run-time hook 'pyi_rth_inspect.py' from 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks\\rthooks'
2087 INFO: Looking for dynamic libraries
2148 INFO: Extra DLL search directories (AddDllDirectory): []
2148 INFO: Extra DLL search directories (PATH): []
2516 INFO: Warnings written to C:\xxx\build\demo1\warn-demo1.txt
2527 INFO: Graph cross-reference written to C:\xxx\build\demo1\xref-demo1.html
2541 INFO: checking PYZ
2541 INFO: Building PYZ because PYZ-00.toc is non existent
2541 INFO: Building PYZ (ZlibArchive) C:\xxx\build\demo1\PYZ-00.pyz
2662 INFO: Building PYZ (ZlibArchive) C:\xxx\build\demo1\PYZ-00.pyz completed successfully.
2704 INFO: checking PKG
2705 INFO: Building PKG because PKG-00.toc is non existent
2705 INFO: Building PKG (CArchive) demo1.pkg
3581 INFO: Building PKG (CArchive) demo1.pkg completed successfully.
3582 INFO: Bootloader C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\PyInstaller\bootloader\Windows-64bit-intel\run.exe
3582 INFO: checking EXE
3582 INFO: Building EXE because EXE-00.toc is non existent
3582 INFO: Building EXE from EXE-00.toc
3582 INFO: Copying bootloader EXE to C:\xxx\dist\demo1.exe
3703 INFO: Copying icon to EXE
3821 INFO: Copying 0 resources to EXE
3821 INFO: Embedding manifest in EXE
3943 INFO: Appending PKG archive to EXE
3946 INFO: Fixing EXE headers
4007 INFO: Building EXE from EXE-00.toc completed successfully.
然后就可以生成我们想要的demo1.exe啦,生成的目录在本地的dist文件夹中。
5. 运行程序
到dist文件夹中,双击demo1.exe就可以运行啦,运行结果如下:
以上就是整个Python软件开发的流程啦!