简体   繁体   English

确保其他人可以运行我的 python 程序的可靠方法

[英]Robust way to ensure other people can run my python program

I wish to place a python program on GitHub and have other people download and run it on their computers with assorted operating systems.我希望在 GitHub 上放置一个 python 程序,并让其他人在他们的计算机上使用各种操作系统下载并运行它。 I am relatively new to python but have used it enough to have noticed that getting the assorted versions of all the included modules to work together can be problematic.我对 python 比较陌生,但使用它已经足够了,注意到让所有包含的模块的各种版本一起工作可能会有问题。 I just discovered the use of requirements.txt (generated with pipreqs and deployed with the command pip install -r /path/to/requirements.txt ) but was very surprised to notice that requirements.txt does not actually state what version of python is being used so obviously it is not the complete solution on its own.我刚刚发现了requirements.txt的使用(使用pipreqs生成并使用命令pip install -r /path/to/requirements.txt部署)但非常惊讶地注意到requirements.txt实际上并没有说明 python 的版本如此明显地使用它本身并不是完整的解决方案。 So my question is: what set of specifications/files/something-else is needed to ensure that someone downloading my project will actually be able to run it with the fewest possible problems.所以我的问题是:需要什么样的规范/文件/其他东西来确保下载我的项目的人实际上能够以最少的问题运行它。

EDIT: My plan was to be guided by whichever answer got the most upvotes.编辑:我的计划是以得到最多票数的答案为指导。 But so far, after 4 answers and 127 views, not a single answer has even one upvote.但到目前为止,在 4 个回答和 127 次观看之后,没有一个回答获得了一个赞。 If some of the answers are no good, it would be useful to see some comments as to why they are no good.如果某些答案不好,那么查看一些关于为什么它们不好的评论会很有用。

Have you considered setting up a setup.py file?您是否考虑过设置setup.py文件? It's a handy way of bundling all of your... well setup into a single location.这是一种将您所有的...很好的设置捆绑到一个位置的便捷方式。 So all your user has to do is A) clone your repo and B) run pip install .所以你的用户所要做的就是 A) 克隆你的 repo 和 B) 运行pip install . to run the setup.py运行setup.py

There's a great stack discussion about this.关于这个有一个很棒的堆栈讨论。

As well as a handle example written by the requests guy. 以及请求人编写的句柄示例。

This should cover most use cases.这应该涵盖大多数用例。 Now if you want to make it truly distributable then you'll want to look into setting it up in PyPi , the official distribution hub.现在,如果您想让它真正可分发,那么您需要考虑在官方分发中心PyPi 中进行设置

Beyond that if you're asking how to make a program "OS independent" there isn't a one size fits all.除此之外,如果您问如何使程序“独立于操作系统”,则没有一种适合所有人的方法。 It depends on what you are doing with your code.这取决于您对代码的处理方式。 Requires researching how your particular code interacts with those OS's etc.需要研究您的特定代码如何与这些操作系统等交互。

There are many, many, many, many, many, many, many ways to do this.有很多很多很多很多很多很多很多很多方法可以做到这一点。 I'll skate over the principles behind each, and it's use case.我将滑过每个背后的原则,这是用例。

1. A python environment 1.一个python环境

There are many ways to do this.有很多方法可以做到这一点。 pipenv , conda, requirments.txt , etc etc. pipenv ,康达, requirments.txt ,等等等等

With some of these, you can specify python versions.使用其中的一些,您可以指定 python 版本。 With others, just specify a range of python versions you know it works with - for example, if you're using python 3.7, it's unlikely not to support 3.6;对于其他人,只需指定您知道它适用的一系列 Python 版本 - 例如,如果您使用的是 Python 3.7,则不太可能不支持 3.6; there's only one or two minor changes.只有一两个小的变化。 3.8 should work as well. 3.8 也应该有效。

Another similar method is setup.py .另一setup.py似的方法是setup.py These are generally used to distribute libraries - like PyInstaller (another solution I'll mention below), or numpy, or wxPython, or PyQt5 etc - for import/command line use.这些通常用于分发库——比如 PyInstaller(我将在下面提到的另一个解决方案)、或 numpy、或 wxPython、或 PyQt5 等——用于导入/命令行使用。 The python packaging guide is quite useful, and there are loads of tutorials out there. python 打包指南非常有用,并且有很多教程。 (google python setup.py tutorial ) You can also specify requirements in these files. (google python setup.py tutorial )您还可以在这些文件中指定要求。

2. A container 2.一个容器

Docker is the big one. Docker 是最大的。 If you haven't heard of it, I'll be surprised.如果你没听说过,我会感到惊讶。 A quick google of a summary comes up with this , which I'll quote part of:一个摘要的快速谷歌提出了这个,我将引用其中的一部分:

So why does everyone love containers and Docker?那么为什么每个人都喜欢容器和 Docker? James Bottomley, formerly Parallels' CTO of server virtualization and a leading Linux kernel developer, explained VM hypervisors, such as Hyper-V, KVM, and Xen, all are "based on emulating virtual hardware. That means they're fat in terms of system requirements."前 Parallels 服务器虚拟化 CTO 和领先的 Linux 内核开发人员 James Bottomley 解释说,VM 管理程序,例如 Hyper-V、KVM 和 Xen,都是“基于模拟虚拟硬件。这意味着它们在系统要求。”

Containers, however, use shared operating systems.然而,容器使用共享操作系统。 This means they are much more efficient than hypervisors in system resource terms.这意味着它们在系统资源方面比管理程序更有效。 Instead of virtualizing hardware, containers rest on top of a single Linux instance.容器不是虚拟化硬件,而是位于单个 Linux 实例之上。 This means you can "leave behind the useless 99.9 percent VM junk, leaving you with a small, neat capsule containing your application,"这意味着您可以“留下 99.9% 无用的 VM 垃圾,留下一个包含应用程序的小而整洁的胶囊,”

That should summarise it for you.那应该为你总结一下。 (Note you don't need a specific OS for containers.) (请注意,容器不需要特定的操作系统。)

3. An executable file 3. 一个可执行文件

There are 2 main tools that do this at the time of writing.在撰写本文时,有 2 个主要工具可以执行此操作。 PyInstaller, and cx_Freeze. PyInstaller 和 cx_Freeze。 Both are actively developed.两者都在积极开发。 Both are open source.两者都是开源的。

You take your script, and the tool compiles it to bytecode, finds the imports, copies those, and creates a portable python environment that runs your script on the target system without the end user needing python.您获取脚本,该工具将其编译为字节码,查找导入内容,复制这些内容,并创建一个可移植的 Python 环境,该环境在目标系统上运行您的脚本,而最终用户不需要 Python。

Personally, I prefer PyInstaller - I'm one of the developers.就个人而言,我更喜欢 PyInstaller - 我是开发人员之一。 PyInstaller provides all of its functionality through a command line script, and supports most libraries that you can think of - and is extendable to support more. PyInstaller 通过命令行脚本提供其所有功能,并支持您能想到的大多数库 - 并且可以扩展以支持更多。 cx_Freeze requires a setup script. cx_Freeze 需要安装脚本。

Both tools support windows, Linux, macOS, and more.这两种工具都支持 Windows、Linux、macOS 等。 PyInstaller can create single file exes, or a one folder bundle, whereas cx_Freeze only supports one folder bundles. PyInstaller 可以创建单个文件 exe 或一个文件夹包,而 cx_Freeze 仅支持一个文件夹包。 PyInstaller 3.6 supports python 2.7, and 3.5-3.7 - but 4.0 won't support python 2. cx_Freeze has dropped python 2 support as of the last major release (6.0 I think). PyInstaller 3.6 支持 python 2.7 和 3.5-3.7 - 但 4.0支持 python 2。从上一个主要版本(我认为是 6.0)开始,cx_Freeze 已经放弃了对 python 2 的支持。

Anyway, enough about the tools features;无论如何,关于工具功能已经足够了; you can look into those yourself.你可以自己看看这些。 (See https://pyinstaller.org and https://cx-freeze.readthedocs.io for more info) (有关更多信息,请参阅https://pyinstaller.orghttps://cx-freeze.readthedocs.io

When using this distribution method, you usually provide source code on the GitHub repo, a couple of exes (one for each platform) ready for download, and instructions on how to build the code into an executable file.使用这种分发方法时,您通常会在 GitHub 存储库上提供源代码、几个可供下载的 exe(每个平台一个),以及有关如何将代码构建为可执行文件的说明。

The best tool I have used so far for this is Pipenv .到目前为止,我为此使用的最好的工具是Pipenv Not only it unifies and simplifies the whole pip+virtualenv workflow for you, developer, but it also guarantees that the exact versions of all dependencies (including Python itself) are met when other people run your project with it.它不仅为您、开发人员统一和简化了整个 pip+virtualenv 工作流程,而且还保证当其他人使用它运行您的项目时,所有依赖项(包括 Python 本身)的确切版本都得到满足。

The project website does a pretty good job at explaining how to use the tool, but, for completeness sake, I'll give a short explanation here.项目网站在解释如何使用该工具方面做得非常好,但是,为了完整起见,我将在这里做一个简短的解释。

Once you have Pipenv installed (for instance, by running pip install --user pipenv ), you can go to the directory of your project and run pipenv --python 3.7 , so Pipenv will create a new virtualenv for your project, create a Pipfile and a Pipfile.lock (more on them later).一旦您安装了 Pipenv(例如,通过运行pip install --user pipenv ),您可以转到您的项目目录并运行pipenv --python 3.7 ,因此 Pipenv 将为您的项目创建一个新的 virtualenv,创建一个 Pipfile和一个 Pipfile.lock (稍后会详细介绍)。 If you go ahead and run pipenv install -r requirements.txt it will install all your packages.如果你继续运行pipenv install -r requirements.txt它将安装你所有的包。 Now you can do a pipenv shell to activate your new virtualenv, or a pipenv run your_main_file.py to simply run your project.现在,您可以执行pipenv shell来激活新的 virtualenv,或者执行pipenv run your_main_file.py来简单地运行您的项目。

Now let's take a look at the contents of your Pipfile.现在让我们看看 Pipfile 的内容。 It should be something resembling this:它应该是类似这样的:

[packages]
Django = "*"
djangorestframework = "*"
iso8601 = "*"
graypy = "*"
whitenoise = "*"

[requires]
python_version = "3.7"

This file has the human-readable specifications for the dependencies of your project (note that it specifies the Python version too).此文件具有项目依赖项的人类可读规范(请注意,它也指定了 Python 版本)。 If your requirements.txt had pinned versions, your Pipfile could have them too, but you can safely wildcard them, because the exact versions are stored in the Pipfile.lock.如果您的 requirements.txt 有固定版本,您的 Pipfile 也可以有它们,但您可以安全地通配它们,因为确切的版本存储在 Pipfile.lock 中。 Now you can run things like pipenv update to update your dependencies and don't forget to commit Pipfile and Pipfile.lock to your VCS.现在您可以运行pipenv update类的东西来更新您的依赖项,并且不要忘记将 Pipfile 和 Pipfile.lock 提交到您的 VCS。

Once people clone your project, all they have to do is run pipenv install and Pipenv will take care of the rest (it may even install the correct version of Python for them).一旦人们克隆了你的项目,他们所要做的就是运行pipenv install并且 Pipenv 会处理剩下的事情(它甚至可能为他们安装正确版本的 Python)。

I hope this was useful.我希望这是有用的。 I'm not affiliated in any way with Pipenv, just wanted to share this awesome tool.我与 Pipenv 没有任何关系,只是想分享这个很棒的工具。

If your program is less about GUI, or has a web GUI, then you can share the code using Google Colaboratory.如果您的程序较少涉及 GUI,或者具有 Web GUI,那么您可以使用 Google Colaboratory 共享代码。

https://colab.research.google.com/ https://colab.research.google.com/

Everyone can run it with the same environment.每个人都可以在相同的环境下运行它。 No need for installation.无需安装。

In case converting all your python scripts into one executable can help you, then my answer below would help ...如果将所有 python 脚本转换为一个可执行文件可以帮助您,那么我下面的回答将有所帮助...

I have been developing a large desktop application purely in python since 3 years. 3 年来,我一直在纯粹用 python 开发一个大型桌面应用程序。 It is a GUI-based tool built on top of pyqt library (python-bindings of QT C++ framework).它是一个建立在 pyqt 库(QT C++ 框架的 python 绑定)之上的基于 GUI 的工具。

I am currently using " py2exe " packaging library : is a distutils extension which allows to build standalone Windows executable programs (32-bit and 64-bit) from Python scripts;我目前正在使用“ py2exe ”打包库:是一个 distutils 扩展,它允许从 Python 脚本构建独立的 Windows 可执行程序(32 位和 64 位); all you have to do is to:您所要做的就是:

  1. install py2exe: 'pip install py2exe'安装 py2exe: 'pip install py2exe'

  2. Create a setup.py script: It is used to specify the content of the final EXE (name, icon, author, data files, shared libraries, etc ..)创建setup.py脚本:用于指定最终EXE的内容(名称、图标、作者、数据文件、共享库等..)

  3. Execute: python setup.py py2exe执行:python setup.py py2exe

I am also using "Inno Setup" software to create installer: Creating shortcuts, setting environment variables, icons, etc ...我还使用“Inno Setup”软件来创建安装程序:创建快捷方式、设置环境变量、图标等......

I think you can use docker with your python https://github.com/celery/celery/tree/master/docker我认为你可以在你的 python https://github.com/celery/celery/tree/master/docker 中使用 docker

kindly follow the files and I think you can figure out the way to make your docker file for your python scripts!请按照文件进行操作,我想您可以找出为您的 python 脚本制作 docker 文件的方法!

Because it is missing from the other answers, I would like to add one completely different aspect:因为其他答案中缺少它,所以我想添加一个完全不同的方面:

Unit testing.单元测试。 Or testing in general.或一般测试。

Usually, it is good to have one known good configuration.通常,最好有一种已知的良好配置。 Depending on what the dependencies of the program are, you might have to test different combinations of packages.根据程序的依赖项,您可能需要测试不同的包组合。 You can do that in an automated fashion with eg tox or as part of a CI/CD pipeline.您可以使用例如tox或作为 CI/CD 管道的一部分以自动化方式执行此操作。

There is no general rule of what combination of packages should be tested, but usually python2/3 compatability is a major issue.没有关于应该测试什么包组合的一般规则,但通常 python2/3 兼容性是一个主要问题。 If you have strong dependencies on packages with major version differences, you might want to consider testing against these different versions.如果您对具有主要版本差异的软件包有很强的依赖性,您可能需要考虑针对这些不同版本进行测试。

I'll give you a very brief summary of some of the existing available solutions when it comes to python packaging you may choose from (knowledge is power):当涉及到您可以选择的 Python 打包时,我将向您简要介绍一些现有的可用解决方案(知识就是力量):

  1. Follow the guidelines provided at Structuring Your Project , these conventions are widely accepted by python community and it's usually a good starting point when newcomers start coding in python.遵循Structuring Your Project 中提供的指导方针,这些约定已被 Python 社区广泛接受,这通常是新手开始使用 Python 编码时的一个很好的起点。 By following these guidelines pythonists watching your project/source at github or other similar places will know straightaway how to install it.通过遵循这些指南,在 github 或其他类似地方观看您的项目/源代码的 Python 专家将立即知道如何安装它。 Also, uploading your project to pypi as well as adding CI by following those rules will be painless.此外,通过遵循这些规则将您的项目上传到 pypi 以及添加 CI 将是轻松的。

  2. Once your project is structured properly according to standard conventions, the next step might be using some of the available freezers , in case you'd like to ship to your end-users a package they can install without forcing them to have python installed on their machines.根据标准约定正确构建您的项目后,下一步可能是使用一些可用的冷冻机,以防您想向最终用户发送他们可以安装的软件包,而无需强迫他们在其上安装 python机器。 Be aware though these tools won't provide you any code protection... said otherwise, extracting the original python code from the final artifacts would be trivial in all cases请注意,尽管这些工具不会为您提供任何代码保护......否则,从最终工件中提取原始 python 代码在所有情况下都是微不足道的

  3. If you still want to ship your project to your users without forcing them to install any dev dependency and you do also care about code protection so you don't want to consider any of the existing freezers you might use tools such as nuitka , shedskin , cython or similar ones.如果您仍然希望将您的项目交付给您的用户而不强迫他们安装任何开发依赖项,并且您也确实关心代码保护,因此您不想考虑任何现有的冰柜,您可能会使用诸如nuitkashedskin 之类的工具, cython或类似的。 Usually reversing code from the artifacts produced by these tools isn't trivial at all... Cracking protection on the other hand is a different matter and unless you don't provide a physical binary to your end-user you can't do much about it other than slowing them down :)通常,从这些工具生成的工件中反转代码根本不是微不足道的......另一方面,破解保护是另一回事,除非您不向最终用户提供物理二进制文件,否则您将无能为力除了减慢他们的速度之外,还有其他问题:)

  4. Also, in case you'd need to use external languages in your python project another classic link that comes to mind would be https://wiki.python.org/moin/IntegratingPythonWithOtherLanguages , adding the build systems of such tools to CI by following rules of 1 would be pretty easy.此外,如果您需要在您的 python 项目中使用外部语言,另一个想到的经典链接是https://wiki.python.org/moin/IntegratingPythonWithOtherLanguages ,通过以下操作将此类工具的构建系统添加到 CI 1的规则会很容易。

That said, I'd suggest stick to bulletpoint 1 as I know that will be more than good enough to get you started, also that particular point should cover many of the existing use-cases for python "standard" projects.也就是说,我建议坚持第 1 点,因为我知道这足以让您入门,而且该特定点应涵盖 Python“标准”项目的许多现有用例。

While this is not intended to be a full guide by following those you'll be able to publish your python project to the masses in no time.虽然这不是一个完整的指南,但通过遵循这些指南,您将能够立即向大众发布您的 Python 项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM