简体   繁体   English

Python脚本“独立”

[英]Python script “standalone”

I've recently started writing python scripts and I'm still newbie to the language. 我最近开始编写python脚本,但我仍然是该语言的新手。

I'm stuck with a problem: My script requires the 'requests' library(and the other packages that comes with it when using pip) to be installed by pip for the script to work(and some folders like the 'database', where I store a sqlite3 file) and I need to install the script in a lot of machines, that have different Ubuntu versions, therefore different Python versions, and I want my script to run 'standalone' and to not have to install/update Python, pip and the 'requests' package every time I setup the script in a new machine. 我遇到了一个问题:我的脚本要求pip安装“请求”库(以及使用pip时随附的其他软件包),脚本才能工作(以及一些文件夹(如“数据库”),我存储了一个sqlite3文件),我需要在很多计算机上安装脚本,这些计算机具有不同的Ubuntu版本,因此具有不同的Python版本,并且我希望我的脚本可以“独立”运行,而不必安装/更新Python,每次我在新计算机上设置脚本时,请选择pip和“请求”包。 I'm developing in a virtualenv on my machine that is currently setuped with all the necessary packages to run the script. 我正在我的计算机上的virtualenv中进行开发,该计算机目前已安装了运行脚本所需的所有必需软件包。

Can I make a make a 'copy' of my virtualenv so it can be moved with my Python script to other computers, including my database folder, without having to install/update python and pip on every machine, instead using this standalone version of python? 我可以为我的virtualenv制作一个“副本”,以便可以将它与我的Python脚本一起移动到其他计算机上,包括我的数据库文件夹,而不必在每台计算机上安装/更新python和pip,而是使用此独立版本的python ? All the machines are Linux. 所有的机器都是Linux。

I already tried to copy my virtualenv to my project folder but the virtualenv crashed when I tried running my script using the python interpreter inside it in the shebang line, even when using the --relocatable argument, so I guess it's not the case. 我已经尝试将我的virtualenv复制到我的项目文件夹中,但是当我尝试使用shebang行中的python解释器运行脚本时,即使使用--relocatable参数,virtualenv也崩溃了,所以我猜不是这样。

I've also tried using PyInstaller, no success. 我也尝试过使用PyInstaller,但没有成功。

Welcome to the world of deployment! 欢迎来到部署的世界! The answer you seek is far from trivial. 您寻求的答案绝非易事。

First off, Python is an interpreted language that isn't really supposed to be distributed as a desktop application. 首先,Python是一种解释型语言,实际上不应该作为桌面应用程序分发。 If you would like to create executables, then there are some libraries for that, such as py2exe . 如果您想创建可执行文件,那么可以使用一些库,例如py2exe However, these are ad-hoc solutions at best. 但是,这些充其量只是临时解决方案。 They "freeze" the whole of Python along with your code, and then you ship everything together. 他们将代码和整个Python“冻结”,然后将所有内容一起运送。

The best practice way to stipulate your dependencies is in a requirements.txt file. 规定依赖关系的最佳实践方法是在requirements.txt文件中。 You can create one with this command: 您可以使用以下命令创建一个:

pip freeze > requirements.txt

What this does is checks all the libraries that are currently in whatever env you're working in, and saves them to a file called requirements.txt. 这样做是检查当前正在使用的任何环境中的所有库,并将它们保存到名为requirements.txt的文件中。 That file will then have all of your required libraries in it, and anyone who receives your code can just run 然后,该文件将包含您所有需要的库,任何接收您代码的人都可以运行

pip install -r requirements.txt 

and it will install all the dependencies. 它将安装所有依赖项。

However, that just takes care of library dependencies. 但是,这只需要处理库依赖项。 What about the version of python itself, OS environment etc... So this is where you may need to start looking at solutions like Docker. python本身的版本,OS环境等如何?因此,您可能需要在这里开始研究Docker之类的解决方案。 With Docker, you can specify the full environment in a Dockerfile. 使用Docker,您可以在Dockerfile中指定完整环境。 Then anyone on another machine can run the docker images, with all of the dependencies included. 然后,另一台机器上的任何人都可以运行docker映像,其中包括所有依赖项。 This is fast become the de-facto way of shipping code (in all languages, but especially useful in Python). 这很快成为事实上的代码交付方式(在所有语言中,但在Python中特别有用)。

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

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