Heroku 不会部署我的 Python 代码,无法获取 "temp/build" 文件
Heroku won't deploy my Python code, failure to obtain "temp/build" file
我正在尝试部署测试应用程序。我当前的代码是一个名为 setup.py
的文件,包含以下内容:
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread
clients = {}
addresses = {}
HOST = ''
PORT = 3300
BUFFSIZ = 1024
ADDR = (HOST, PORT)
SERVER = socket(AF_INET, SOCK_STREAM)
SERVER.bind(ADDR)
def accept_incoming_connections():
while True:
client, client_address = SERVER.accept()
print("%s:%s has connected." % client_address)
client.send(bytes("Greetings! type your name and press enter!", "utf8"))
addresses[client] = client_address
Thread(target = handle_client, args=(client,)).start()
def handle_client(client): # Takes client socket as argument.
"""Handles a single client connection."""
name = client.recv(BUFFSIZ).decode("utf8")
welcome = 'Welcome %s! If you ever want to quit, type {quit} to exit.' % name
client.send(bytes(welcome, "utf8"))
msg = "%s has joined the chat!" % name
broadcast(bytes(msg, "utf8"))
clients[client] = name
while True:
msg = client.recv(BUFFSIZ)
if msg != bytes("{quit}", "utf8"):
broadcast(msg, name+": ")
else:
client.send(bytes("{quit}", "utf8"))
client.close()
del clients[client]
broadcast(bytes("%s has left the chat." % name, "utf8"))
break
def broadcast(msg, prefix=""):
"""Broadcasts a message to all the clients."""
for sock in clients:
sock.send(bytes(prefix, "utf8")+msg)
if __name__ == "__main__":
SERVER.listen(5) # Listens for 5 connections at max.
print("Waiting for connection...")
ACCEPT_THREAD = Thread(target=accept_incoming_connections)
ACCEPT_THREAD.start() # Starts the infinite loop.
ACCEPT_THREAD.join()
SERVER.close()
这是我在网上找到的用于制作简单聊天应用程序的页面中 99% 的测试代码,如果我可以在 Heroku 上运行,稍后将对其进行更多修改。我的命令运行如下:
> heroku login
> git init
> git add .
> git commit -m "initial commit"
> heroku create
> heroku git:remote -a myProjectName
> git push heroku master
构建日志显示如下:
-----> Building on the Heroku-20 stack
-----> Using buildpack: heroku/python
-----> Python app detected
-----> No Python version was specified. Using the buildpack default: python-3.9.6
To use a different version, see: https://devcenter.heroku.com/articles/python-runtimes
cp: cannot stat '/tmp/build_7a00c31d/requirements.txt': No such file or directory
-----> Installing python-3.9.6
-----> Installing pip 20.2.4, setuptools 47.1.1 and wheel 0.36.2
-----> Installing SQLite3
-----> Installing requirements with pip
Obtaining file:///tmp/build_7a00c31d (from -r /tmp/build_7a00c31d/requirements.txt (line 1))
在那一行之后它无限挂起或超时并给我“构建失败,请在构建日志中查看更多信息”以及 link 将我定向到该文件。
这里有一些问题。
眼前的问题是 setup.py
means something very specific in the Python ecosystem and Heroku assumes that it can do pip install -e .
if this file is present 而没有 requirements.txt
或 Pipfile
。
将您的文件重命名为其他名称(例如 server.py
),添加 requirements.txt
或 Pipfile
和 Pipfile.lock
或 compliant setup.py
定义您的依赖项,然后提交。
您可能还需要 Procfile
. Since you are expecting incoming requests you'll need to define a web
process (they're the only ones that can receive traffic from the Internet),例如如果您的新文件名为 server.py
:
web: python server.py
您将还 需要停止对您监听的端口进行硬编码,而是使用 the value Heroku provides via the PORT
environment variable,例如像这样:
import os
PORT = os.getenv("PORT", default=3300)
Heroku 会自动将来自端口 80 或 443 的流量路由到您的应用程序。
我正在尝试部署测试应用程序。我当前的代码是一个名为 setup.py
的文件,包含以下内容:
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread
clients = {}
addresses = {}
HOST = ''
PORT = 3300
BUFFSIZ = 1024
ADDR = (HOST, PORT)
SERVER = socket(AF_INET, SOCK_STREAM)
SERVER.bind(ADDR)
def accept_incoming_connections():
while True:
client, client_address = SERVER.accept()
print("%s:%s has connected." % client_address)
client.send(bytes("Greetings! type your name and press enter!", "utf8"))
addresses[client] = client_address
Thread(target = handle_client, args=(client,)).start()
def handle_client(client): # Takes client socket as argument.
"""Handles a single client connection."""
name = client.recv(BUFFSIZ).decode("utf8")
welcome = 'Welcome %s! If you ever want to quit, type {quit} to exit.' % name
client.send(bytes(welcome, "utf8"))
msg = "%s has joined the chat!" % name
broadcast(bytes(msg, "utf8"))
clients[client] = name
while True:
msg = client.recv(BUFFSIZ)
if msg != bytes("{quit}", "utf8"):
broadcast(msg, name+": ")
else:
client.send(bytes("{quit}", "utf8"))
client.close()
del clients[client]
broadcast(bytes("%s has left the chat." % name, "utf8"))
break
def broadcast(msg, prefix=""):
"""Broadcasts a message to all the clients."""
for sock in clients:
sock.send(bytes(prefix, "utf8")+msg)
if __name__ == "__main__":
SERVER.listen(5) # Listens for 5 connections at max.
print("Waiting for connection...")
ACCEPT_THREAD = Thread(target=accept_incoming_connections)
ACCEPT_THREAD.start() # Starts the infinite loop.
ACCEPT_THREAD.join()
SERVER.close()
这是我在网上找到的用于制作简单聊天应用程序的页面中 99% 的测试代码,如果我可以在 Heroku 上运行,稍后将对其进行更多修改。我的命令运行如下:
> heroku login
> git init
> git add .
> git commit -m "initial commit"
> heroku create
> heroku git:remote -a myProjectName
> git push heroku master
构建日志显示如下:
-----> Building on the Heroku-20 stack
-----> Using buildpack: heroku/python
-----> Python app detected
-----> No Python version was specified. Using the buildpack default: python-3.9.6
To use a different version, see: https://devcenter.heroku.com/articles/python-runtimes
cp: cannot stat '/tmp/build_7a00c31d/requirements.txt': No such file or directory
-----> Installing python-3.9.6
-----> Installing pip 20.2.4, setuptools 47.1.1 and wheel 0.36.2
-----> Installing SQLite3
-----> Installing requirements with pip
Obtaining file:///tmp/build_7a00c31d (from -r /tmp/build_7a00c31d/requirements.txt (line 1))
在那一行之后它无限挂起或超时并给我“构建失败,请在构建日志中查看更多信息”以及 link 将我定向到该文件。
这里有一些问题。
眼前的问题是 setup.py
means something very specific in the Python ecosystem and Heroku assumes that it can do pip install -e .
if this file is present 而没有 requirements.txt
或 Pipfile
。
将您的文件重命名为其他名称(例如 server.py
),添加 requirements.txt
或 Pipfile
和 Pipfile.lock
或 compliant setup.py
定义您的依赖项,然后提交。
您可能还需要 Procfile
. Since you are expecting incoming requests you'll need to define a web
process (they're the only ones that can receive traffic from the Internet),例如如果您的新文件名为 server.py
:
web: python server.py
您将还 需要停止对您监听的端口进行硬编码,而是使用 the value Heroku provides via the PORT
environment variable,例如像这样:
import os
PORT = os.getenv("PORT", default=3300)
Heroku 会自动将来自端口 80 或 443 的流量路由到您的应用程序。