部署Discord机器人至Heroku:requirements.txt应添加哪些内容?
requirements.txt for Your Discord Bot Deployment on Heroku Hey there! Let's break down exactly what belongs in your requirements.txt file, especially for your Discord bot setup on Heroku. I'll cover both the basics you already know and the git-related entries you've seen on YouTube.
1. Basic PyPI Packages (Your Current Setup)
Since you've installed discord and selenium via pip install, these are standard packages hosted on PyPI (Python Package Index). For these, your requirements.txt can be as simple as listing the package names:
discord selenium
If you want to avoid unexpected compatibility issues (since newer versions might break your bot), you can lock in specific versions. To find the versions you're currently using, run pip freeze in your local terminal—you'll see output like discord==2.3.2 or selenium==4.15.2. Your file would then look like:
discord==2.3.2 selenium==4.15.2
This tells Heroku to install exactly those versions, not the latest ones.
2. Git-Related Entries (What You Saw on YouTube)
The git links you've seen are for situations where you need a package that isn't available on PyPI, or you need a specific branch, unreleased version, or custom fork of a package. Here's how they work:
The general format is:
git+https://github.com/[username]/[repository].git@[branch-or-tag]#egg=[package-name]
git+https://github.com/...: The URL to the Git repository hosting the package@branch-or-tag: Specifies which branch, commit hash, or release tag you want to install#egg=package-name: Tells pip what to name the package internally (so other dependencies can reference it correctly)
For example, if you were using a custom fork of discord.py with a new feature that hasn't been merged into the main PyPI package, you'd add something like:
git+https://github.com/your-github-username/discord.py@feature-new-command#egg=discord
You'd only need to use this if:
- You're relying on a development version of a package with bug fixes/features not on PyPI
- You've modified a package's code and host it in your own Git repo
- The original package is no longer maintained, and you're using a community fork
3. Your Specific Bot Use Case
For your current setup—using the standard discord and selenium packages from PyPI—you don't need any git-related entries in your requirements.txt. Just listing the package names (with optional version locks) is enough to get your bot deployed on Heroku successfully.
If later you add a dependency that requires a Git-hosted version, you can add the appropriate line then.
4. A Quick Note on Heroku's Deployment Process
Heroku automatically reads your requirements.txt during deployment and runs pip install -r requirements.txt to install all dependencies. Double-check for typos or incorrect version numbers—small mistakes here are a common cause of deployment failures.
内容的提问来源于stack exchange,提问作者Anthony Massaad

