[英]How can I include a pip installed module with my python script?
I wrote an encryption program in Python 3.6 that uses the module pycryptodome
, specificly these imports: 我在Python 3.6中编写了一个加密程序,该程序使用模块pycryptodome
,特别是这些导入:
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from Crypto.Random import get_random_bytes
The program works, but it cannot be used without the user installing the pycryptodome
module themselves. 该程序可以运行,但是如果没有用户自己安装pycryptodome
模块,则无法使用该程序。
Is there any way to include that package somehow or can I create a "first time setup" to install it for the end user? 是否可以通过某种方式包含该软件包,或者我可以创建“首次安装”以为最终用户安装该软件包?
Short answer - as already mentioned in comments - you need to package your script. 简短答案-正如评论中已经提到的-您需要打包脚本。
Long answer - I have been there before for the first time - it's annoying. 好长的答案-我第一次去过那里-很烦。 They way Python modules and scripts are packaged is constantly evolving and not all available documentation is being kept up to date. 打包Python模块和脚本的方式一直在不断发展,并且并非所有可用的文档都保持最新。 You can easily end up reading recent documentation which, by the time of reading, is already flawed (ie obsolete). 您可以轻松地阅读最近的文档,这些文档在阅读时已经存在缺陷(即已过时)。
The easiest approach, without going down all the way, is a simple file for pip
, which describes your project's dependencies. 最简单的方法(无需全部介绍)是pip
的简单文件,它描述了项目的依赖性。 It's called requirements.txt
. 这就是所谓的requirements.txt
。 Up-to-date information can be found here . 可以在此处找到最新信息。
If this is not enough, you have to package your application. 如果这还不够,则必须打包您的应用程序。 A good but little maintained and somewhat outdated overview / introduction can be found here . 可以在这里找到一个很好但是很少维护并且有些过时的概述/介绍。 It's good for a start, but do NOT try to follow it to the letter! 这是一个很好的开始,但是请不要紧随其后! Another OUTDATED manual for beginners commonly cited can be found here . 在这里可以找到另一本常用的过时的初学者手册。 Read it for basic understanding and do NOT try to follow it to the letter. 阅读该书以了解基本知识,请勿尝试将其跟随于信中。
Once you have come to this point, it's time to read "What the Hell? -- A Journey Through the Nine Circles of Python Packing" . 一旦到了这一点,就该读“地狱是什么?-穿越Python包装九圈的旅程” 。 It gives a lot of practical advise. 它提供了许多实用建议。 The most practical of all: Look at how other projects are doing it and copy-paste it ... ehm ... learn from it. 最实用的方法:查看其他项目的执行情况,然后将其复制粘贴...嗯……从中学习。
If you are not scared off by now, I can actually recommend to look at Python module templates. 如果您现在还不怕,我实际上可以建议您看一下Python模块模板。 One of the best ones I know of is the "Cookiecutter PyPackage" . 我知道的最好的之一是“ Cookiecutter PyPackage” 。 Its well maintained documentation is available here . 其维护良好的文档可在此处获得 。 If you learned the basics of Python packaging, it's a quick and reliable way of creating all the files and infrastructure required for packaging your code. 如果您了解了Python打包的基础知识,那么它是创建打包代码所需的所有文件和基础结构的一种快速可靠的方法。
Honorable mention: There are tools, which try to streamline the entire process. 值得一提的是:有些工具可以简化整个过程。 Number one on my list and also already mentioned in the comments is PyInstaller ( manual ). 在我的清单上并且在评论中已经提到的第一名是PyInstaller ( 手册 )。 Another tool (for Windows), which is usually mentioned, is py2exe (no up-to-date documentation available, AFAIK). 通常提到的另一个工具(适用于Windows)是py2exe (无可用的最新文档,AFAIK)。 Another up-and-coming and promising (yet not production-ready?) tool is Briefcase ( documentation ). 公文包 ( documentation )是另一个新兴且有希望的工具(尚未投入生产吗?)。 There are more of those, but they all have their issues. 这些更多,但是它们都有问题。 There is a good chance you end up reading into the above literature trying to understand those issues ... 您最终有机会阅读上述文献以试图理解这些问题...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.