简体   繁体   English

如何使python在所有计算机上兼容?

[英]How to make python compatible on all computers?

I wrote an interesting script that I want to share with people, but it won't run on other computers unless they have the same dependencies installed. 我编写了一个有趣的脚本,希望与他人共享,但是除非在其他计算机上安装了相同的依赖项,否则它将无法在其他计算机上运行。 How do I bundle the dependencies into the script package so that other people don't need to install dependencies to run the script? 如何将依赖项捆绑到脚本包中,以便其他人无需安装依赖项即可运行脚本? Like, maybe let the script reference the dependencies in the same folder that the script is packaged in. 就像,也许让脚本引用与脚本打包在同一文件夹中的依赖项。

I want to keep it open source and share my code, so I don't want to use cx_freeze to turn it into an exe. 我想让它保持开源并共享我的代码,所以我不想使用cx_freeze将其变成一个exe。

Try packaging/bundling up your application using pyinstaller ; 尝试使用pyinstaller包装/捆绑您的应用程序; This us used for example by docker-compose to ship a cross-platform compatible binary ( where docker-compose is written in Python ). 例如,我们被docker-compose用来运送跨平台兼容的二进制文件( 其中docker-compose用Python编写 )。

In it's simplest usage it's as simple as: 在最简单的用法中,它很简单:

pip install pyinstaller  # Get pyinstaller
pyinstaller -F /path/to/myscript.py

You'll end end up with a bundled executable that you can share that includes all dependencies. 最后,您将获得可以共享的捆绑可执行文件,其中包括所有依赖项。

Note: You'll probably have to repeat these steps for other platforms / operating systems you want to support (eg: Windows); 注意:您可能必须对要支持的其他平台/操作系统(例如Windows)重复这些步骤。 but I have no practical experience in doing so so refere to the pyinstaller Documentation for how to bundle for other platforms. 但是我没有实际经验,因此请参阅pyinstaller文档以了解如何捆绑到其他平台。

Update: To provide both pre-built/bundled distributions of your program as well as downloadable source code simply follow the above advice for the first part and follow the Python Packaging Guide for declaring dependencies in your setup.py making it easy for other to either pip install or git clone and python setup.py install . 更新:要提供程序的预构建/捆绑发行版以及可下载的源代码,只需按照上述第一部分的建议进行操作,并遵循《 Python打包指南》setup.py声明依赖项,从而使其他人都容易pip installgit clonepython setup.py install

I'd suggest you create a virtual environment. 我建议您创建一个虚拟环境。

 Step 1:
     >> pip install virtualenv

 Step 2:
     >> cd my_project_folder
     >> virtualenv env
     Note: In this case I named my environment "env" you 
     can call it whatever you want.

 Step 3: Activate your virtual environment
     >> source env/bin/activate

 Step 4: Install whatever libraries your program needs.
       ex. >> pip install Flask

 Step 5: Create a requirements doc.
      >> pip freeze > requirements.txt
      Note: Be sure to update your requirements.txt doc 
      whenever you install new packages.

When you post your project on open source forums people can then pip install your requirements.txt file to have the same environment you had when you made it. 当您在开放源代码论坛上发布项目时,人们可以通过pip安装您的requirements.txt文件,使其具有与创建项目时相同的环境。

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

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