What version of Python is on my Mac?

前端 未结 6 762
臣服心动
臣服心动 2020-12-07 13:32

I have a mac, when I do:

python --version 

I got:

Python 2.7.6

but when I got to:

/System         


        
相关标签:
6条回答
  • 2020-12-07 13:39

    Use the which command. It will show you the path

    which python
    
    0 讨论(0)
  • 2020-12-07 13:43

    Take a look at the docs regarding Python on Mac.

    The version at /System/Library/Frameworks/Python.framework is installed by Apple and is used by the system. It is version 3.3 in your case. You can access and use this Python interpreter, but you shouldn't try to remove it, and it may not be the one that comes up when you type "Python" in a terminal or click on an icon to launch it.

    You must have installed another version of Python (2.7) on your own at some point, and now that is the one that is launched by default.

    As other answers have pointed out, you can use the command which python on your terminal to find the path to this other installation.

    0 讨论(0)
  • 2020-12-07 13:44

    You could have multiple Python versions on your macOS.

    You may check that by command, type or which command, like:

    which -a python python2 python2.7 python3 python3.6
    

    Or type python in Terminal and hit Tab few times for auto completion, which is equivalent to:

    compgen -c python
    

    By default python/pip commands points to the first binary found in PATH environment variable depending what's actually installed. So before installing Python packages with Homebrew, the default Python is installed in /usr/bin which is shipped with your macOS (e.g. Python 2.7.10 on High Sierra). Any versions found in /usr/local (such as /usr/local/bin) are provided by external packages.

    It is generally advised, that when working with multiple versions, for Python 2 you may use python2/pip2 command, respectively for Python 3 you can use python3/pip3, but it depends on your configuration which commands are available.

    It is also worth to mention, that since release of Homebrew 1.5.0+ (on 19 January 2018), the python formula has been upgraded to Python 3.x and a python@2 formula will be added for installing Python 2.7. Before, python formula was pointing to Python 2.

    For instance, if you've installed different version via Homebrew, try the following command:

    brew list python python3
    

    or:

    brew list | grep ^python
    

    it'll show you all Python files installed with the package.

    Alternatively you may use apropos or locate python command to locate more Python related files.

    To check any environment variables related to Python, run:

    env | grep ^PYTHON
    

    To address your issues:

    • Error: No such keg: /usr/local/Cellar/python

      Means you don't have Python installed via Homebrew. However double check by specifying only one package at a time (like brew list python python2 python3).

    • The locate database (/var/db/locate.database) does not exist.

      Follow the advice and run:

      sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
      

      After the database is rebuild, you can use locate command.

    0 讨论(0)
  • 2020-12-07 13:46

    Use below command to see all python installations :

    which -a python
    
    0 讨论(0)
  • 2020-12-07 13:53

    To check third version, we can use python3 --version

    0 讨论(0)
  • 2020-12-07 13:57

    If you have both Python2 and Python3 installed on your Mac, you can use

    python --version
    

    to check the version of Python2, and

    python3 --version
    

    to check the version of Python3.

    However, if only Python3 is installed, then your system might use python instead of python3 for Python3. In this case, you can just use

    python --version
    

    to check the version of Python3.

    0 讨论(0)
提交回复
热议问题