Scott
Scott

Reputation: 5820

Python or Python3. What is the difference?

What is the difference between the following commands:

python setup.py

and

python3 setup.py

Upvotes: 41

Views: 110336

Answers (3)

tripleee
tripleee

Reputation: 189297

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 (including Ubuntu, Mint, etc) have a package called python-is-python3 which you can install with apt to configure python to refer to python3 system-wide. (There is another one called python-is-python2, too.)

  • More generally, Debian-based systems 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).

  • If you have installed multiple versions of Python, the directory it was installed in needs to be in your PATH; if multiple directories which are on your PATH contain a python or python3 binary or script, the one earlier in your PATH takes precedence. See also below.

    A related problem arises when you have a PYTHONPATH which overrides or complements Python's sys.path. Out of the box, this should point to wherever your Python packages were installed when you installed Python, but there are many ways to get this mixed up, especially if you are doing something to override the defaults of the Python version you are trying to use.

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.

Python can be obtained from many places (downloaded and double-clicked an installer? Conda? Homebrew? Chocolatey? The native package manager of your OS? Configured how? Compiled from source? Using which options?) and what to expect can depend on where you got it from and how you installed and configured it. The more details you can provide, the better a question can you ask.


Don't call me Shirley, please.

Upvotes: 29

Martin
Martin

Reputation: 386

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.

Upvotes: 6

Wasif
Wasif

Reputation: 15470

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

Upvotes: 5

Related Questions