PyInstaller是一个十分有用的第三方库,通过将py文件打包,Python程序可以在没有安装 Python的环境中运行,也可以作为一个独立文件方便传递和管理。
安装方法
pip install PyInstaller
或者官网下载安装。
使用
使用下载安装的方式安装的Pyinstaller打包方式
将需要打包的文件放在解压得到的Pyinstaller文件夹中,打开cmd窗口,把路径cd到当前路径打开命令提示行,输入以下内容(最后的是要打包的py文件):
python pyinstaller.py -F myfile.py
使用pip方式安装的Pyinstaller打包方式
打开cmd窗口,cd切换到文件所在路径,输入以下内容(最后的是要打包的py文件):
pyinstaller -F myfile.py
参数
- -F/–onfile 表示生成单个可执行文件。
- -D/–onedir 表示生成一个程序文件夹。
- -c/–console 表示运行时显示控制台窗口,默认参数不需要加。
- -w/–windowed 表示去掉控制台窗口,这在GUI界面时非常有用,如果是命令行程序的话不要加这个选项。
- -p 表示自己自定义需要加载的类路径,防止有库找不到,一般情况下用不到。
- -n/–name 打包好的入口程序的名称。
- -y/–noconfirm 无需确认即可覆盖输出目录中的文件。
- –distpath 输出路径,打包好的程序的存放路径,默认为/dist。
- –workpath 工作路径,用于存放打包过程中的临时文件,默认为当前路径。
- -i/–icon 打包好的可执行文件的图标。
- –key 用于加密Python字节码的密钥,此功能在内部使用tinyaes模块进行加密,不能与VmProtect等加壳工具一起使用。
- –clean 打包前清理缓存和临时文件
- –add-data=”image.png:img” 额外需要打包进去的文件,例如图片、文档等。
- –add-binary=”libfo.so:lib” 额外需要打包进去的二进制文件,例如dll等。
- –uac-admin 打包好的程序执行时会要求管理员权限。
注意事项
即使将py脚本打包成exe程序,也不意味着代码的安全性很高,例如可以通过pyinstxtractor进行提取和Uncompyle6进行反编译,因此要想有效保证代码安全,还得考虑混淆和加壳等手段。
另外注意的是,打包出的exe可执行文件或者程序包体积会偏大,为降低体积可以在编写程序时尽量不要导入用不到的库,需要的库用到什么功能就只导入什么功能。也就是说尽量不要用import XX而是用from XX import YY。
打包出的程序如果在运行时闪退,如果确认程序本身没问题,那么可能是系统缺少C++或者.net framework等运行库。
关于打包太多库的优化
由于我用的anaconda包管理,因此不可避免会在打包时打包进无关的库,导致程序包体积变大,因此下面说一下优化方法。
- 使用pycharm等IDE的venv功能,在全新的Python虚拟环境中只安装自己用到的库和pyinstaller,并在这个虚拟环境中打包。
- 使用pipenv安装虚拟环境和第三方库,再进行打包,具体如下。
由于只能打包成exe,因此只能在windows环境下使用。
另外注意的是,打包出的exe可执行文件或者程序包体积会偏大,为降低体积可以在编写程序时尽量不要导入用不到的库,需要的库用到什么功能就只导入什么功能。也就是说尽量不要用import XX而是用from XX import YY。
pip install pipenv #安装 pipenv install --python 3.7 #选择Python版本 pipenv shell #进入新建的虚拟环境 pip list #展示已安装的库,应该只有几个自带的库 pipenv install pyqt5 #安装用到的库
以上两个方法都是通过虚拟一个干净的Python环境打包,但有时会遇到使用的库有很多依赖库造成打包体积偏大,下面两个方法可以进行尝试。
- 如果只用到了一个库里的个别方法,可以尝试将该段代码直接复制到自己脚本上,当成自己写的方法进行调用。需要注意的是复制过来的代码中有没有其他引用,有的话要一并复制过来。
- 如果以上方法都不管用,那还有最后一个笨方法,直接删。比如说我用到了pandas库读取csv文件的功能,它会把numpy等依赖库一并打包,打包后文件包大小超过了700M。
经过删除某些dll文件并运行测试,在删除掉一些确实用不到的文件后,打包后的文件包降到了100M,当然这个方法要仔细测试,避免删掉了必要的dll导致某些功能失效。
成长就是妥协与坚持的两难。
《解忧杂货店》
——东野圭吾
评论
This design is steller! You certainly know
how to keep a reader amused. Between your wit and your
videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent
job. I really enjoyed what you had to say, and more
than that, how you presented it. Too cool!
в этом разделе https://xn—-jtbjfcbdfr0afji4m.xn--p1ai/
Welcome to The Karelia Business Forum, your trusted source for all things business-related. From insightful articles to in-depth analyses, we’ve got you covered. Join our community of entrepreneurs, innovators, and industry experts today! https://karelia.business/
I got what you intend, thanks for putting up.Woh I am happy to find this website through google. “It is a very hard undertaking to seek to please everybody.” by Publilius Syrus.
https://www.einco.es/sistemas-de-audio-para-el-hogar/273993-aiwa-msbtu-500-msbtu-500-sistema-de-audio-para-el-hogar-microcadena-de-musica-para-uso-domestico-8435256898002.html
Contains additional gold, increased investment benefit Considerably less sturdy as a consequence of larger gold material. Nickel articles might cause allergic response. Platinum
There are dissimilarities in perform concerning white gold and platinum. The selection of ring steel among white gold and platinum has ordinarily been according to price tag. Recent gold costs have narrowed the gap in pricing which has built the choice more details on perform. 14k and 18k white gold is a tad inexpensive and ideal for any ring fashion.
Additionally, round diamonds have a tendency to appear more substantial than other cuts of the exact same carat body weight, earning them a favorite option for Individuals seeking a major glimpse and not using a significant value tag.
Natural diamonds are designed by mother nature, on account of rigorous warmth and pressure, fashioned about the course of many several years. Steeped in romance, purely natural diamonds have tested to retain their benefit eventually and can even multiply in worth.
Make Your own private Sapphire Engagement Ring At GemsNY, sapphire engagement rings are provided in a wide array of types and styles. To build your own ring, opt for a sapphire from GemsNY’s wide array of gemstones, ring patterns and metals. Our interface is not difficult and also the filters occur helpful that may help you zero in on your ideal match.
Be the main to learn about fascinating new styles, Distinctive gatherings, retailer openings and even more. Email
An ideal ring awaits. Anywhere you will be with your journey to pop the problem, select a possibility underneath to get rolling.
https://vocal.media/authors/sanders-haas
A properly-Slash diamond takes advantage of the diamond’s very best qualities. When seeking a round diamond that actually dazzles, pay mindful interest to most of the four Cs, but particularly to its Slice. Store Round-Reduce Diamonds
Privacy coverage By publishing this kind with a contact number, you comply with receive recurring automatic promotional and customized promoting text messages (e.
You would possibly find that emerald Reduce diamonds require more schedule cleaning to appear sparkly and clear. When your diamond appears filthy, then heat drinking water, gentle dish soap, as well as a tender brush is going to be a fairly easy and swift Alternative in between Experienced cleansing.
Watch ring Pear diamond plus a 0.95ct emerald Slash pink sapphire engagement ring set in platinum with an 18K rose gold band
Sellers aiming to improve their business enterprise and access a lot more intrigued potential buyers can use Etsy’s advertising and marketing System to market their merchandise. You’ll see ad results according to factors like relevancy, and the amount sellers spend per click on. Find out more.
It just doesn’t. But for the moment in time, there is a genuine unity among people, there’s an actual magnificent Strength that will come out of those people today that I’m dealing with.”
They carry The same crystal structure and aesthetic to some purely natural diamond as well as a lower price position.
http://doctorpills.ru/bor-nuzhen-dlya-zdorovoj-potencii.html
Join The Karelia Business Forum and become part of a vibrant ecosystem designed to nurture your professional growth. With our comprehensive suite of resources, you’ll be well-equipped to seize every opportunity that comes your way. https://karelia.business/
Does your blog have a contact page? I’m having problems locating it but, I’d like to shoot you an e-mail.
I’ve got some recommendations for your blog you might be interested in hearing.
Either way, great blog and I look forward to seeing
it develop over time.
What Is Puravive? The Puravive weight loss aid is formulated using eight clinically proven natural ingredients.
https://youtu.be/vq52WNsjKVA
http://images.google.cm/url?q=https://generik-spb.ru
This website is really a walk-by for the entire info you wanted about this and didn’t know who to ask. Glimpse here, and also you’ll positively uncover it.
https://youtu.be/BanYFY6l55c
But wanna say that this is handy, Thanks for taking your time to write this.
https://youtu.be/EZ2StIbcBhs
https://toolbarqueries.google.co.zm/url?q=https://kupit-dzheneriki-v-spb.ru
After research a couple of of the weblog posts on your web site now, and I really like your manner of blogging. I bookmarked it to my bookmark website checklist and might be checking back soon. Pls try my site as properly and let me know what you think.
https://youtu.be/M_9FstqN2aI
Top traffic exchange list containing the best manual
TEs and autosurfs on the web. Promote your business,
get more subscribers, and drive massive traffic to your websites – completely free.
http://194.120.116.242/index.html
I’m still learning from you, but I’m improving myself. I definitely love reading everything that is posted on your site.Keep the information coming. I enjoyed it!
https://youtu.be/TzPKWOSNFMQ
Hello my loved one! I want to say that this article is awesome, nice written and come with almost all important infos. I?¦d like to peer more posts like this .
https://youtu.be/gKr3OKOswAs
https://www.jp-dolls.com/goods/p2323.html
Добро пожаловать на сайт https://zubrit.com где вы сможете найти ответы по всем школьными предметам! Здесь вы найдете исчерпывающие ответы и объяснения к различным школьным заданиям по математике, литературе, истории, биологии, химии и многим другим предметам. Наша команда состоит из опытных педагогов и специалистов, готовых помочь вам разобраться в трудных вопросах и достичь успеха в учебе. Независимо от того, нужны ли вам подсказки для домашнего задания или готовитесь к экзаменам, здесь вы найдете все необходимое для эффективного обучения.
I do not even know how I ended up here, but I thought this post was great. I don’t know who you are but certainly you’re going to a famous blogger if you are not already 😉 Cheers!
https://youtu.be/F73Rc-43jEM
ラブドールの購入に関するご相談?ご心配事は、些細な点でも、お気軽にご連絡ください! ラブドール通販
https://www.jp-dolls.com/category/c15.html
I am not sure where you’re getting your information, but great topic.
I needs to spend some time learning much more or understanding more.
Thanks for great info I was looking for this info for my mission.
Traders are advised to exercise counsel
but not lose one’s nerve, as there is no reason to believe that a crucial
downturn is imminent. In order seeking a succour rally to begin,
it is well-connected for unchanging suffer levels to sermonize on for Bitcoin. Telling on to the top 5
cryptocurrencies that show oneself strong on the charts,
Bitcoin’s price judgement shows that the digital
asset has been range-bound between $60,775 and $73,777 instead of diverse
days. The brand-new sink has bewitched it lower than the 50-day compelling usual,
but the bulls from defended the $60,775 brook level.
A pick-up take a crack at is apt to to image rebelliousness at the
20-day telling average. If the reward breaks heavens this irregulars,
it could flag the by means of b functioning as for a convocation to $73,777
and beyond.
https://cryptoforday.com/us-govt-tackles-cybersecurity-skill-shortage-with-3-6m-investment/
We’re a group of volunteers and opening a new scheme in our community. Your website provided us with valuable info to work on. You have done a formidable job and our entire community will be grateful to you.
https://youtu.be/HZg-PFwQLa4
https://dickfix.ru/