[Fix] Bash: Python3: command not found When Installing discord.py on Windows

Bash Python3 Command Not Found When Installing Discord

Have you tried to install the popular discord.py library on Windows, only to see the frustrating “bash: python3: command not found” error? You’re not alone. This is a common issue faced by Windows users looking to leverage Python for Discord bot development.

In this guide, we’ll learn exactly why you get this error, the various solutions to resolve it, and how to ensure Python and command line tools play nicely moving forward. By the end, you’ll have discord.py up and running to build awesome Discord bots on Windows. Let’s get started!

What Causes the “python3: command not found” Error?

First, what exactly causes this error?

On Linux and macOS, the default Python runtime is usually version 2.x. So, the python command runs Python 2 while python3 explicitly runs Python 3. However, recent distributions are slowly discarding python2 in favor of python3 while retaining the python3 command.

But, there is no “python3” command on Windows. The Python executable is called python regardless of 2.x or 3.x.

So when installing a Python 3 package on Windows, referencing python3 causes the “command not found” error.

Let’s walk through a real example to solidify this:

$ git clone https://github.com/Rapptz/discord.py 
$ cd discord.py
$ python3 -m pip install -U .[voice]
bash: python3: command not found

Here we:

  1. Clone the discord.py repo
  2. Navigate into the repo directory
  3. Try to install dependencies with python3

Since Windows has no native python3 command, step 3 fails with our error.

The root of the problem is assuming python3 would work cross-platform when it’s actually only present on Linux/macOS.

Also read: Downgrade From Python 3.7 to 3.6 on Windows, MacOS, and Linux

Solutions for “python3: command not found”

Alright, enough diagnosis – how do we fix this? There are a few potential solutions, each with their trade-offs:

SolutionOverviewDownsides
Use python instead of python3On Windows, just use the python commandOn Windows, use the python command
Create a python3 symbolic linkCreate a symlink to map python3 to actual Python executableMore complex setup, need to recreate on Python upgrade
Call Python executable directlyInvoke pip/discord.py install using full executable pathLess portable
Use the py commandLeverage the py command available on WindowsInvoke pip/discord.py install using the full executable path

Below, we explore each solution in more detail, with examples.

Also read: Fixing “ImportError: No module named ‘selenium’” in Python

1. Use python instead of python3

Since Windows has no notion of a separate python3 command, the simplest solution is just using python instead.

So our installation would become:

$ git clone https://github.com/Rapptz/discord.py
$ cd discord.py  
$ python -m pip install -U .[voice]

This avoids the command not found error by calling the default python executable, which runs our desired Python 3 version.

The catch is if you have legacy Python 2 present, it may conflict. The python command will run whichever Python version appears first in your system PATH. So if an old Python 2 creeps first, you’ll need to adjust PATH order.

But in most cases, especially for fresh installs, using python should “just work”.

For systems with Python 2 legacy concerns, we can create a symbolic link to essentially “forge” a python3 command that redirects to the actual desired Python 3 executable.

The steps would be:

  1. Open a command prompt/PowerShell as administrator
  2. Navigate to any folder in your system PATH
  3. Create a symbolic link named python3.exe pointing to the real Python 3 executable location

For example:

> mklink python3.exe C:\Program Files\Python37\python.exe

Now when we run python3, Windows resolves the symlink to launch our intended Python 3 interpreter.

The downside is this mapping breaks whenever you upgrade Python. You’d have to recreate the symbolic link to the new executable path.

3. Invoke Python Using Full Executable Path

Rather than rely on the python3 command, we can fully qualify invocation by calling the pip install using the full executable path:

C:\Program Files\Python37\python.exe -m pip install -U .[voice]

This sidesteps command confusion by being completely explicit about which Python runtime to leverage.

The only real downside is it makes your code less portable. Hard-coding full paths introduces a dependency on that exact directory structure.

4. Use the py Command

Windows also ships with a py command that routes to the appropriate Python runtime. So you can use:

py -3 -m pip install -U .[voice] 

Where the -3 flag targets Python 3 explicitly.

Just be warned – while handy, py is not an officially supported Python command. Its behavior can vary across underlying Windows versions. So while it usually works as expected, it also may introduce subtle inconsistencies. Use at your own risk!

Whichever solution you choose, we recommend taking a bit of time to configure your Windows command line environment for smooth Python sailing:

  • Add Python install directories to your system PATH – Ensures you can invoke Python from any directory
  • Standardize on forward slashes – Use C:/ not C:\ for paths. Avoids headaches with slash translation.
  • Stick to PowerShell over cmd.exe – More modern, powerful shell with better Python integration.
  • Use virtual environments – Create isolated Python environments to manage dependencies and avoid conflicts.

Investing a bit of setup here pays off tremendously down the road. You get to focus on building awesome apps rather than wrangling environment issues.

Summary

The “python3 command not found” error is a common stumbling block when installing Python libraries like discord.py on Windows. It stems from differences in Python runtime conventions across operating systems.

Thankfully, several straightforward solutions including using python instead of python3, creating symlinks, calling the full path, or leveraging py. Each approach comes with tradeoffs to consider.

We recommend configuring your Windows command line environment for Python as well. Steps like adding Python to PATH, forward slashes for paths, embracing PowerShell, and utilizing virtual environments go a long way.

You can eliminate frustrating command line headaches with the right troubleshooting and configuration. Allowing you to fully leverage Python’s capabilities for your next big Discord or automation project!

References: StackOverflow question