简体   繁体   English

如何将第三方模块“包含”到我的python脚本中以使其可移植?

[英]How to “include” third party modules into my python script to make it portable?

I've developed a little script that searches through a wallpaper database online and download the wallpapers, I want to give this script to another person that isn't exactly good with computers and I'm kinda starting with python so I don't know how to include the "imports" of the third party modules in my program so it can be 100% portable, Is there something that can help me do this? 我开发了一个小脚本,在网上搜索壁纸数据库并下载壁纸,我想把这个脚本给另一个不太适合电脑的人,我有点开始用python所以我不知道如何在我的程序中包含第三方模块的“导入”,以便它可以100%移植,是否有什么可以帮助我这样做? or I will have to enter and analyse my third party modules and copy&paste the functions that I use? 或者我将不得不输入和分析我的第三方模块并复制和粘贴我使用的功能?

Worse thing to do 更糟糕的事情要做

An easy thing you can do is simply bundle the other modules in with your code. 您可以轻松地将其他模块与您的代码捆绑在一起。 That does not mean that you should copy/paste the functions from the other modules into your code--you should definitely not do that, since you don't know what dependencies you'll be missing. 并不意味着你应该复制/从其他模块的功能,粘贴到你的代码-你绝对应该这样做,因为你不知道什么是依赖关系,你会丢失。 Your directory structure might look like: 您的目录结构可能如下所示:

/myproject
    mycode.py
    thirdpartymodule1.py
    thirdpartymodule2.py
    thirdpartymodule3/
        <contents>

Better thing to do 更好的事情

The real best way to do this is include a list of dependencies (usually called requirements.txt ) in your Python package that Python's package installer, pip , could use to automatically download. 最好的方法是在Python包中包含一个依赖项列表(通常称为requirements.txt ),Python的软件包安装程序pip可以用它来自动下载。 Since that might be a little too complicated, you could give your friend these instructions, assuming Mac or Linux: 由于这可能有点太复杂,你可以给你的朋友这些指示,假设Mac或Linux:

  1. Run $ curl http://python-distribute.org/distribute_setup.py | python 运行$ curl http://python-distribute.org/distribute_setup.py | python $ curl http://python-distribute.org/distribute_setup.py | python . $ curl http://python-distribute.org/distribute_setup.py | python This provides you with tools you'll need to install the package manager. 这为您提供了安装包管理器所需的工具。
  2. Run $ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python 运行$ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python $ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python . $ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python This installs the package manager. 这将安装包管理器。
  3. Give your friend a list of the names of the third-party Python modules you used in your code. 向您的朋友提供您在代码中使用的第三方Python模块的名称列表。 For purposes of this example, we'll say you used requests , twisted , and boto . 出于这个例子的目的,我们会说你使用了requeststwistedboto
  4. Your friend should run from command line $ pip install <list of package names> . 你的朋友应该从命令行运行$ pip install <list of package names> In our example, that would look like $ pip install requests twisted boto . 在我们的示例中,这看起来像$ pip install requests twisted boto
  5. Run the Python code! 运行Python代码! The lines like import boto should then work, since your friend will have the packages installed on their computer. import boto这样的行应该可以工作,因为你的朋友将在他们的计算机上安装这些包。

The easi(er) way: easi(呃)方式:

  1. Start with a clean virtual environment . 从干净的虚拟环境开始
  2. Install packages that you need to develop your code. 安装开发代码所需的软件包。
  3. Once you are done, create a list of requirements for your project. 完成后,创建项目的需求列表
  4. Send this file (from step 3) to your friend. 将此文件(从第3步)发送给您的朋友。

Your friend simply does pip install -r thefile.txt to get all the requirements for your application. 你的朋友只需要pip install -r thefile.txt来获得你的应用程序的所有要求。

Here's an example: 这是一个例子:

D:\>virtualenv --no-site-packages myproject
The --no-site-packages flag is deprecated; it is now the default behavior.
New python executable in myproject\Scripts\python.exe
Installing setuptools................done.
Installing pip...................done.

D:\>myproject\Scripts\activate.bat
(myproject) D:\>pip install requests
Downloading/unpacking requests
  Downloading requests-0.14.1.tar.gz (523Kb): 523Kb downloaded
  Running setup.py egg_info for package requests

    warning: no files found matching 'tests\*.'
Installing collected packages: requests
  Running setup.py install for requests

    warning: no files found matching 'tests\*.'
Successfully installed requests
Cleaning up...

(myproject) D:\>pip freeze > requirements.txt
(myproject) D:\>type requirements.txt
requests==0.14.1

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

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