Building a Discord Chatbot with Python (2) - Setting Up the Development Environment

In this article, we’ll prepare the development environment to build a Discord chatbot in Python.

We’ll be utilizing the discord.py library.
Ensure you have Python version 3.8 or higher to use this library.

Let’s begin by creating a working directory named discord-chatbot.
All our operations will be performed within this directory.

mkdir discord-chatbot
cd discord-chatbot
  1. Check your current Python version with the following command:

    python --version
    
  2. If you don’t have Python 3.8 or higher, you can either upgrade your current version or use pyenv to install a newer version.

  3. Here’s an example of installing Python using pyenv:

    pyenv update
    pyenv install 3.11.4
    pyenv local 3.11.4
    
  4. Verify that the desired Python version has been correctly installed:

    python --version
    

Next, we’ll set up a virtual environment. This allows you to develop programs without affecting the default environment.

While you can use external tools like conda or poetry to create a virtual environment, we’ll introduce the method using the standard library, venv.

  1. Run the following command within the working directory to create a virtual environment:

    python -m venv .venv
    
  2. Activate the virtual environment. For activating commands, refer here.
    Once activated, you’ll see (.venv) at the beginning of your terminal prompt.

    source .venv/bin/activate
    
  3. Let’s check the installed packages in our environment:

    $ pip list
    
    Package    Version
    ---------- -------
    pip        23.1.2
    setuptools 65.5.0
    
  1. With the virtual environment activated, install discord.py:

    python3 -m pip install -U discord.py
    
  2. Verify the installation by pip list. You should see discord.py along with its dependencies.

    $ pip list
    
    Package            Version
    ------------------ -------
    aiohttp            3.8.5
    aiosignal          1.3.1
    async-timeout      4.0.3
    attrs              23.1.0
    charset-normalizer 3.2.0
    discord.py         2.3.2
    frozenlist         1.4.0
    idna               3.4
    multidict          6.0.4
    pip                23.1.2
    setuptools         65.5.0
    yarl               1.9.2
    

With that, we’ve successfully set up our development environment.
In our next article, we’ll dive into configuring Discord for our chatbot.

Related Content