![](/img/trans.png)
[英]Portable python script: Is it possible to include third party libraries in script?
[英]How to “include” third party modules into my python script to make it portable?
我開發了一個小腳本,在網上搜索壁紙數據庫並下載壁紙,我想把這個腳本給另一個不太適合電腦的人,我有點開始用python所以我不知道如何在我的程序中包含第三方模塊的“導入”,以便它可以100%移植,是否有什么可以幫助我這樣做? 或者我將不得不輸入和分析我的第三方模塊並復制和粘貼我使用的功能?
您可以輕松地將其他模塊與您的代碼捆綁在一起。 這並不意味着你應該復制/從其他模塊的功能,粘貼到你的代碼-你絕對不應該這樣做,因為你不知道什么是依賴關系,你會丟失。 您的目錄結構可能如下所示:
/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 http://python-distribute.org/distribute_setup.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
。 這將安裝包管理器。 requests
, twisted
和boto
。 $ pip install <list of package names>
。 在我們的示例中,這看起來像$ pip install requests twisted boto
。 import boto
這樣的行應該可以工作,因為你的朋友將在他們的計算機上安裝這些包。 easi(呃)方式:
你的朋友只需要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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.