Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何分发python代码?_Python_Python 3.x_Packaging - Fatal编程技术网

如何分发python代码?

如何分发python代码?,python,python-3.x,packaging,Python,Python 3.x,Packaging,我正在从事我的一个python项目,我希望它能够像应用程序一样运行。所以我为它制作了一个gui,我想把它分发给其他人使用。但是我使用了像requests和tkinter这样的包。如何使程序更具可移植性?所以人们只要点击.py文件,我的gui就会出现 #!/usr/bin/env python3 # imports import requests import time from tkinter import * import random # variables test = 'https:/

我正在从事我的一个python项目,我希望它能够像应用程序一样运行。所以我为它制作了一个gui,我想把它分发给其他人使用。但是我使用了像requests和tkinter这样的包。如何使程序更具可移植性?所以人们只要点击.py文件,我的gui就会出现

#!/usr/bin/env python3
# imports
import requests
import time
from tkinter import *
import random

# variables
test = 'https://api.nicehash.com/api?method=stats.provider.ex&addr=37sCnRwMW7w8V7Y4zyVZD5uCmc9N1kZ2Q8&callback=jQuery111304118770088094035_1506738346881&_=1506738346882'
url = 'https://api.coinbase.com/v2/prices/USD/spot?'



# def function to update
def update_bitcoin_ui():

    # update the data sourced form the website
    req = requests.get(url)
    data = req.json()
    bit = (data['data'][0]['amount'])

    # update the gui to reflect new value
    thelabel.config(text = "1 BTC = %s USD" % bit)

    # verify the Ui is updating
    #thelabel.config(text=str(random.random()))
    root.after(1000, update_bitcoin_ui)

# gui workspace
root = Tk()
thelabel = Label(root, text = "")

# set more of the gui and launch the ui
thelabel.pack()
root.after(1000, update_bitcoin_ui)
root.mainloop()

编辑:我找到了我要找的东西。我在寻找pyinstaller的效果

你可以用几种方法来实现

使用类似于。注册然后创建一个存储库。用户可以转到您的存储库(
https://GitHub.com/username/repo_name
)并通过浏览器下载。或者,*nix用户可以执行
git克隆https://GitHub.con/username/repo_name

或者,将脚本上载到PyPi。然后,您可以在Windows/*nix上执行
pip安装[package name]
,依赖项将自动安装。阅读如何做到这一点

如果您只是在寻找安装依赖项的简单方法:

您可以使用requirements.txt,这是一个包含所有依赖项的文本文件。然后用户可以执行
pip安装-r requirements.txt
,您的所有依赖项都将被安装

或者,您可以创建一个
setup.py
。它将包含诸如您的依赖关系、作者等信息。然后,用户可以使用
python setup.py install
安装它

最后,您可以简单地做一个try/except语句,如下所示:

import pip
try:
    import module
except:
    pip.main(['install', 'module'])

但是等等!也许您的用户没有Python。没有人希望安装所有这些只是为了一些用途。在这种情况下,您可以查看Py2Exe

可能的重复,我认为OP并不是要求分发代码的方法,而是要使其可移植,这样用户就不必安装依赖项。