20

What is the difference between the following commands:

python setup.py

and

python3 setup.py
  • What if I only have python3.6 installed? python and python3 would do the same thing?
  • Does it make difference only when I have different versions of python installed? If so, which version will be used with python setup.py?
Scott
  • 4,974
  • 6
  • 35
  • 62
  • 6
    Maybe `python` is Python 2. Maybe it's Python 3. Maybe it's a different version of Python 3. Maybe it's nothing. Depends how your system is set up. Use `python -V` to check. – khelwood Nov 12 '20 at 09:30
  • Because we had python2 before and that has reached end of life. So on newer systems without python2, python and python3 both use python3. If you have multiple python versions the latest one will be used. But even then, you can use python virtual environment to "lock" your dependencies and python version (pipdeptree > req.txt, , pip - r req.txt ) – Rambarun Komaljeet Nov 12 '20 at 09:33
  • Related: https://stackoverflow.com/questions/57368515/make-python-run-python3-at-the-prompt (not sure if that can be considered a duplicate) – Tomerikoo Nov 12 '20 at 09:34

3 Answers3

17

There is no single correct answer here, but we can offer some common observations.

  • Some Linux distributions decided during the transition from Python 2 to Python 3 that python should always refer to Python 2, and the command to run Python 3 would be python3 with a 3 at the end. Now that Python 2 is becoming obsolete, this is being relaxed in some distros (i.e. now that they no longer ship Python 2 at all, the python command can be allowed to point to Python 3, too), but this is going to continue to exist in some form for some time.
  • Debian-based Linux distros have a mechanism called update-alternatives which lets you define which version of python exactly will be linked to the system-wide standard /usr/bin/python. There is similarly a set of alternatives for /usr/bin/python3.
  • If you have manually installed Python 3 somewhere, what works depends on where it was installed and whether that location is in your PATH. A common arrangement is to have Python 3.14.159 somewhere like /opt/python-3.14.159/bin/python3.14.159 and then rely on users who want to use this to create a symlink or alias to this binary so that they can simply call it python (or python3, or obviously also e.g. shirley if they prefer that)
  • If you have an interactive alias, function, or personal shell script in your PATH with either of these names, obviously that overrides any system-wide setting, and could basically do anything at all, including but not limited to running a specific version of Python, ideally with all the command line arguments intact and correctly quoted (/path/to/system-wide/default/python "$@") but obviously with no guarantees of any sort.
  • On Windows, where many of these facilities don't exist, a common arrangement is for Python to be installed in C:\python3.14.159\bin\Python.exe; you then have to have C:\python3.14.159\bin in your PATH for the python command to work. The installer lets you install the package anywhere, but if you just go with the defaults, this is what you commonly end up with. Because of the cumbersomeness of Windows, the standard install also includes a command py which works around some of the rigidity of the platform to let you flexibly indicate which Python version exactly to run. There is usually not a separate python3 command, though users are of course free to create a CMD file with that name to run Python (or play a video of Rick Astley performing his biggest hit if they so prefer).

To help us help you debug questions about your installation, you will commonly need to tell us exactly what you installed where and how, and show us the value of your PATH. If you have relevant functions, aliases, or personal shell scripts which shadow the system-wide default ones, show them too. On Unix-based platforms, the type and command commands can be used to show what will be executed (many guidances will tell you to use which or whereis, but don't; now we have to guess which non-standard command you are using, and where it is going to look). The Windows command whereis provides roughly the same functionality.


Don't call me Shirley, please.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
tripleee
  • 175,061
  • 34
  • 275
  • 318
5

Yes, it will make difference if you have different versions of python installed.

This depends on the entries in on the PATH environment variable. Suppose you have two python installations, 2.7 and 3.8, now you have installed 2.7 before 3.8, and both were added to PATH, so when you type python, 2.7 interpreter launches. If you have done vice versa, then 3.8 would launch. You can type where python to determine location.

Also one thing is that there is a launcher named py, just type py -3.8 3.8 interpreter will launch and same on py -2.7

Wasif
  • 14,755
  • 3
  • 14
  • 34
  • 1
    For the longest time, `py` was specific to Windows, but a similar mechanism is now available for other systems as an option, too. – tripleee Dec 30 '21 at 06:03
2

NOTE: Answer provided with specific consideration for it now being 2023, and for Windows users

Nowadays, python = python3

To answer your questions, given the example of...

> python  setup.py
> python3 setup.py
  • What if I only have python3.6 installed? These would/should run in the same version of python, e.g. 3.6
  • Do python and python3 do the same thing? In 2023, they should yes
  • Does it make difference when different versions are installed? If so, which version will be used? The other answers cover this in really good detail

Some config may be required (and recommended)

  • If you want your system to accept either python setup.py or python3 setup.py and run these commands on the whichever version of python you have installed then I would recommend creating a symbolic link from python3 ==> python
  • If you don't know how to do that, copy the code below into notepad.exe, save it as 'python3-as-python.bat` (remember to select 'All files'), then right click and choose 'Run as Administrator'
python3-as-python.bat
---------------------

@ECHO OFF
:: Creates an alias for python3, any python3 [...] commands will be executed as if typed as python [...]
:: Checks default locations for single-user install and all-user install
:: Run as adminstrator if possible (required if python was installed for all users)

IF EXIST "%ProgramFiles%\Python3*" (
    PUSHD "%ProgramFiles%\Python3*"
    MKLINK python3.exe python.exe >nul
    IF EXIST python3.exe ECHO Python found in 'Program Files', alias created for python3 --^> python
)

IF EXIST "%LocalAppData%\Programs\Python\Python*" (
    PUSHD "%LocalAppData%\Programs\Python\Python*"
    MKLINK python3.exe python.exe >nul
    IF EXIST python3.exe ECHO Python found in user's 'AppData' folder, alias created for python3 --^> python
)

ECHO.
PAUSE
GOTO:EOF

Rationale

  • Having just installed the latest version of python (3.11) from python.org myself, this was something I was also forced to try and work out as when I ran any commands like python3 setup.py I got an error saying python was not installed
  • As of April 2023 (and possibly earlier), the official python installer only provides/creates python.exe and while it adds this to the %PATH% for ease-of-reference it does NOT create python3.exe, so any scripts or code with commands like python3 setup.py would need to be converted to python setup.py
  • With genuine credit and respect to the previous answers provided, in my option the simplest answer is that nowadays python == python3. Given the nature of it being 2023 and python3 coming out in 2008, the number of instances where people explicitly need to run python scripts on a 15-year old version(!) should be extremely minimal
  • NOTE: I am not trying to dismiss any of the advice or cautions given, but rather provide a simple, up-to-date answer that applies to most people.
Martin
  • 280
  • 1
  • 10