Bash: Python3: command not found (Windows, discord.py)
Asked Answered
B

7

31

I've been trying to configure the discord API discord.py and for the purpose of running the Red-MusicBot on my server. I've installed Python 3.5, and added the PATH variables (I clicked the "add Python to PATH" option in install). Here's what my path variables currently look like:

C:\Users\Corey Rigney\AppData\Local\Programs\Python\Python35\Scripts\
C:\Users\Corey Rigney\AppData\Local\Programs\Python\Python35\

Those are the only ones related to Python. Now, as part of discord.py's install process, it wants me to run this command in Git Bash:

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

The first two lines work perfectly, but the third line returns:

bash: python3: command not found

I also cloned pip from GitHub as an attempted fix, although the python install site says it comes packaged with 3.5.

I'm running windows 10, 64-bit.

The overall goal of this is to install a discord music bot, if it would help I can post the errors I get when trying to run that.

Broomstick answered 1/12, 2016 at 15:12 Comment(15)
git-bash and bash are 2 absolutely different programs. also, have a look at this questionCorrinecorrinne
Are you sure it is called python3 and not just python?Heliometer
@Corrinecorrinne I looked at it and ran the 'env|grep PATH' command. It returned the two python paths mentioned above in the regular PATH section.Broomstick
@Heliometer According to the tutorial, python3 is the correct command. I can try python, thoughBroomstick
what is the name of the executables for python3 ? i.e. is there a python3.exe in the C:\Users\Corey Rigney\AppData\Local\Programs\Python\Python35\Scripts\ folder ? Try running the exact name insteadCorrinecorrinne
@Heliometer Wow, that actually worked! (Why the heck is the tutorial on that wrong?) Thank you!Broomstick
The python executable is sometimes called python3 on some platforms, where the default (python) is the old python 2. OS X, for example.Heliometer
@Heliometer Does that mean that I installed this using the wrong version of python? To my knowledge, no other version of python or pip is installed on my computer.Broomstick
No! You installed 3.5 which is the current stable version. You are on Windows, so I would not expect there to be a python already there. On many UNIX-based (inc. Linux and OS X) systems, python 2 is used by system utilities, changing it could have bad consequences on those platforms, hence the name "python3". On Windows you should be fine - there are other issues on Windows but you won't get those unless you try to use more than one python version.Heliometer
@Heliometer Good to know, and thank you for your help! (Since I'm kind of a noob to this site, how do I set yours as the answer?Broomstick
@Yakman3: you can't. Don't worry about it, glad to help. Have fun!Heliometer
@cdarke: Rather than say "you can't", why not post your solution as an answer? Yes it was a simple fix, but I think your previous comment has enough information to make it worth posting as an answer.Stelmach
Entered as an answer. Apologies @ScottWeldon for not doing it sooner, I was pushed for time.Heliometer
@Heliometer No prob; thanks!Stelmach
@Yakman3: You can now accept the answer that cdarke posted by clicking the checkmark next to the answer.Stelmach
H
42

On Windows the normal name for the python executable is python.exe (console program) or pythonw.exe (for GUI programs).

The python executable is sometimes called python3 on some platforms, where the default (python) is the old python 2. On many UNIX-based (inc. Linux and OS X) systems, python 2 is used by system utilities, changing it could have bad consequences on those platforms, hence the name "python3".

On Windows you should be fine - there are other issues on Windows but you won't get those unless you try to use more than one python version.

Heliometer answered 2/12, 2016 at 11:43 Comment(2)
May I ask, I have installed and checked that I have Python version 3.6.5 within my Windows 10 system (checked from cmd with python --version). Now I need to run a python file that as the authors say should use the python3.6 baseline.py in order to execute it. I use git bash to run the code but when typing the aforementioned command I get the error bash: python3.6: command not found. If I use simply python baseline.py it seems as it tries to run..Is it wrong trying to execute it that way?Plump
You only need to run python baseline.py on Windows. That should suffice.Cephalothorax
W
62

In the python installed("c:\\Installationpath\Python3.6.0") path you will find "python.exe", just copy paste in the same place and rename it as "python3.exe", now in the command prompt you can check python3 command should display your python installation. Don't forget to open a new terminal.

Washroom answered 16/1, 2018 at 10:32 Comment(5)
That helped me just copy paste in the same place and rename it as "python3.exe", now in the command prompt you can check python3Ramakrishna
Worked for me in 2019Goglet
Fixed in less than 10 seconds. Appreciate it! Win10, VS code, Python 3.8Trevar
such a simple yet beautiful solutionPomelo
That's a really bad suggestion, please don't do it if you want to avoid issues and wasted time in the future. It's bad because you would have to repeat it every time you're upgrading Python to a newer version. It's likely that you'll forget it and you'll end up with a broken or weirdly behaving Python. There are better sugestions in other answers, check them. You can for example create the python3.exe file as suggested in this answer, but instead of the copy of the Python interpreter, it's just a symbolic link (a pointer) to it.Spanish
H
42

On Windows the normal name for the python executable is python.exe (console program) or pythonw.exe (for GUI programs).

The python executable is sometimes called python3 on some platforms, where the default (python) is the old python 2. On many UNIX-based (inc. Linux and OS X) systems, python 2 is used by system utilities, changing it could have bad consequences on those platforms, hence the name "python3".

On Windows you should be fine - there are other issues on Windows but you won't get those unless you try to use more than one python version.

Heliometer answered 2/12, 2016 at 11:43 Comment(2)
May I ask, I have installed and checked that I have Python version 3.6.5 within my Windows 10 system (checked from cmd with python --version). Now I need to run a python file that as the authors say should use the python3.6 baseline.py in order to execute it. I use git bash to run the code but when typing the aforementioned command I get the error bash: python3.6: command not found. If I use simply python baseline.py it seems as it tries to run..Is it wrong trying to execute it that way?Plump
You only need to run python baseline.py on Windows. That should suffice.Cephalothorax
A
24

In windows using git bash, python3 didn't worked for me:

$ python --version
Python 2.7.15

but if I use py

$ py --version
Python 3.8.1
Acquire answered 22/1, 2020 at 11:33 Comment(1)
I guess it is because you have both pythion2 and python3 installed.Osteo
P
14

Duplicating the Python 3 executable python.exe and renaming it to python3.exe suggested in another answer is a terrible idea, please don't do it, because you would have to repeat it every time you upgrade Python to a newer version and it's likely that you'll forget it and you'll be surprised that your Python is broken after the upgrade.

I suggest the following simple setup.

Solution: Symbolic Link python3.exe

Just create a symbolic link named python3.exe in a directory which is in your PATH environment variable (but which is not under the Python 3 installation directory) pointing to the Python 3 executable python3/python.exe. The symbolic link stays there and keeps pointing to the correct executable even if you upgrade the Python (since it's outside the Python 3 installation directory, it's not affected even when the whole directory of an outdated Python is deleted and a new Python is placed there).

It's very easy to prepare:

  1. Execute an elevated Powershell Core (pwsh.exe), Powershell (powershell.exe), or Windows command shell (cmd.exe)
  2. Decide where you want to create the symbolic link:
    • Pick a directory already in your PATH environment variable (use echo $env:PATH in Powershell or echo %PATH% in cmd.exe to print the variable contents)
    • Add any directory you like to the PATH variable (see below)
  3. Navigate to the directory you chose in the previous step and create there a symbolic link named python3.exe pointing to the Python 3 executable (the target parameter), both paths may be absolute or relative:

Now, if you execute python3 or python3.exe from any directory, Windows searches it in the current directory and then all directories in your PATH environment variable. It finds the symbolic link you have created which "redirects" it to the Python 3 executable and Windows executes it.

Notes

Which version executes python command?

What Python version is being executed by the command python when both Python 2 and 3 are installed, depends on the order of Python directories in the PATH environment variable.

When you execute a command and it's not being found in the current working directory, Windows iterates through all directories in the PATH variable while keeping the order as they're listed there and executes the first executable whose name matches the command (and it stops the searching).

So, when your PATH variable contains Python installation directories in the order c:\dev\python2\;c:\dev\python3;..., then the python command executes python.exe in the c:\dev\python2\ because it was found first.

The order depends on the order in which you have installed both Python versions. Each installation adds (if you check that option) its instalation directory at the beggining of PATH, so the most recently installed version will be executed when you execute just python. But you can reorder them manually, of course.

pip

There's no issue with pip, because there's already an executable named pip3.exe that's located in a directory automatically added to the PATH during the installation by Python (<installation-root>\Scripts, so just use pip3 for the Python 3's pip and pip/pip2 for the Python 2's pip.

Editing Environment Variables

  1. Open the Windows' About dialog by pressing Win + Pause/Break or right-clicking This PC and selecting Properties
  2. Open the System Properties dialog by clicking the link Advanced system settings on the right side of the Settings dialog
  3. Open the Environment Variables dialog by clicking the button Environment Variables... at the bottom of the System Properties dialog
  4. Here, you can manage user variables and if you have the admin rights then also system variables
Patroclus answered 8/5, 2021 at 9:17 Comment(1)
Perfect and detailed explanation. It solves the problem on my windows problem.Pigtail
N
5

Instead of copying the executable, add a script that acts as python3.

A Python 3 script with #!python3 shebang line will fail to run, because python3.exe is not exists on Windows - it can be achieved by py -3.

To solve the problem, add this script as python3 in to your PATH: it will avoke the proper Python command depending on the operating system (works on Windows and Linux as well).

#!/usr/bin/env bash
# Fix problem with `python3` shebang on Windows MSYS Bash

if [[ "$OSTYPE" =~ ^msys ]]; then
  py -3 $*
else
  python3
fi
Numen answered 14/11, 2018 at 9:20 Comment(3)
I have to change the "py -3 $*" line to "python $*" to make it work with conda envs.Delighted
File "<stdin>", line 1 if [[ "$OSTYPE" =~ ^msys ]]; then ^ SyntaxError: invalid syntax >>> ..Freddiefreddy
Aren't there two problems in this answer? (1) python3 is missing the arguments "$@". (2) Won't this infinitely recurse as "python3" will just invoke the script again?Volcanic
L
5

On Windows 10 you might find that py works where python or python3 doesn't.

Limpkin answered 23/5, 2020 at 9:1 Comment(0)
P
1

None of the solutions above worked for me, however, I was able to find success with Python 3.7 when instead of writing python3 -m pip install discord.py, I wrote C:\InstallPath\python.exe -m pip install discord.py

This probably worked because while the command python3 was not available in cmd, the path to the python core file worked and took the arguments as the python3 command would.

NOTE: The normal python command was not working for me, as I already have 2 installed. Discord for some reason requires 3.5 and above?

Pecker answered 19/10, 2018 at 8:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.