我开发了一个小脚本,可以在线搜索壁纸数据库并下载壁纸,我想将此脚本提供给另一个不太擅长计算机的人,我有点从 python 开始,所以我不知道如何在我的程序中包含第三方模块的“导入”,使其可以 100% 移植,有什么可以帮助我做到这一点的吗?还是我必须输入并分析我的第三方模块并复制和粘贴我使用的功能?
			
			2753 次
		
2 回答
            4        
        
		
更糟糕的事情
您可以做的一件简单的事情就是将其他模块与您的代码捆绑在一起。这并不意味着您应该将其他模块中的函数复制/粘贴到您的代码中——您绝对不应该这样做,因为您不知道会丢失哪些依赖项。您的目录结构可能如下所示:
/myproject
    mycode.py
    thirdpartymodule1.py
    thirdpartymodule2.py
    thirdpartymodule3/
        <contents>
更好的事情要做
真正做到这一点的最佳方法是在 Python 包中包含一个依赖项列表(通常称为requirements.txt),Python 的包安装程序pip可以使用它来自动下载。由于这可能有点太复杂,你可以给你的朋友这些说明,假设 Mac 或 Linux:
- 运行
$ curl http://python-distribute.org/distribute_setup.py | python。这为您提供了安装包管理器所需的工具。 - 运行
$ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python。这将安装包管理器。 - 给您的朋友一份您在代码中使用的第三方 Python 模块的名称列表。出于本示例的目的,我们会说您使用了
requests、twisted和boto。 - 你的朋友应该从命令行运行
$ pip install <list of package names>。在我们的示例中,它看起来像$ pip install requests twisted boto. - 运行 Python 代码!然后像这样的行
import boto应该可以工作,因为您的朋友将在他们的计算机上安装软件包。 
于 2012-10-14T02:22:35.927   回答
    
    
            2        
        
		
更简单的方法:
您的朋友只是pip install -r thefile.txt为了满足您的应用程序的所有要求。
这是一个例子:
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
    于 2012-10-14T05:58:31.657   回答