Python Location on MAC Osx

python location on mac osx

[GCC 4.2.1 (Apple Inc. build 5646)] is the version of GCC that the Python(s) were built with, not the version of Python itself. That information should be on the previous line. For example:

# Apple-supplied Python 2.6 in OS X 10.6
$ /usr/bin/python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

# python.org Python 2.7.2 (also built with newer gcc)
$ /usr/local/bin/python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Items in /usr/bin should always be or link to files supplied by Apple in OS X, unless someone has been ill-advisedly changing things there. To see exactly where the /usr/local/bin/python is linked to:

$ ls -l /usr/local/bin/python
lrwxr-xr-x 1 root wheel 68 Jul 5 10:05 /usr/local/bin/python@ -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python

In this case, that is typical for a python.org installed Python instance or it could be one built from source.

Location for Python version on macOS (using universal-installer for 3.10.5)

TL;DR: 3.10 (major.minor) points already to the latest installed patch-version (5 here). Also recommend to use the Current folder for the PATH environment-variable like below.

From command to binary location

Assume

  • python3 is the command
  • after running python3 --version its version shows as 3.10.5

See also: How to Check Your Python Version | LearnPython.com

Then this command shows the location of the binary for command python3:

which python3

See also: python location on mac osx, What version of Python is on my Mac?

Apple's Framework convention

The Python installer follows the Framework Versions explained in Apple's Framework Developer Guide:

Frameworks (like Python instead <Name>) installed by convention in folder:

/System/Library/Frameworks/<Name>.framework/Versions/<major>.<minor>/

with following version numbering-scheme:

<major>.<minor> (like 3.10 for Python 3.10.5)

where <major> denotes the major-version (having breaking changes like 3 here) and <minor> denotes the minor-version (with only small feature-changes and bug-fixes, like 10 here). The patch- or maintenance-version (like 5 here) is ignored in the folder-structure.

A recommended pattern for PATH environment-variable is to use macOS managed-frameworks' version-alias Current which points to the latest version installed for the framework. Try listing its contents to see the symbolic-link (also symlink or alias) with command ls -l followed by the path, for example:

/Library/ManagedFrameworks/Python/Python3.framework/Versions/Current/

See also: macadmins/python: Framework files for use with popular python macadmin toolsets
and symlink - How to get the fully resolved path of a symbolic link in Terminal?

Versioning Schemes

  • Python versioning: PEP 440 – Version Identification and Dependency Specification
  • Wikipedia: Software versioning (major, minor, patch)
  • Semantic Versioning (semver)

Find path of Python installed by Homebrew

Use brew info <packagename>

it's probably in one of (and referenced in both)

/usr/local/Cellar/python@3.8/3.8.6_2
/usr/local/opt/python@3.8/libexec/bin

See also Apple SE Where can I find the installed package path via brew

If it's not there, it's plausible the version detection is wrong and only searches for the first two version digits (as conflicting versions may clobber each other). In this case, follow the warning and reinstall the specific version you want.

I can't locate my python path (but able to locate python3 path)

What version of MacOS are you running? Starting in 12.3, Python2 was removed from the system.

https://developer.apple.com/documentation/macos-release-notes/macos-12_3-release-notes

Python
Deprecations
Python 2.7 was removed from macOS in this update. Developers should use Python 3 or an alternative language instead. (39795874)

Finding a file's directory address on a Mac

The desktop is just a subdirectory of the user’s home directory. Because the latter is not fixed, use something like os.path.expanduser to keep the code generic. For example, to read a file called somefile.txt that resides on the desktop, use

import os
f = open(os.path.expanduser("~/Desktop/somefile.txt"))

If you want this to be portable across operating systems, you have to find out where the desktop directory is located on each system separately.

What version of Python is on my Mac?

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.

Why is Python Installation in Library directory macOS?

/Library/Frameworks/Python.framework/Versions/3.x/bin/python3 is the Python distribution shipped with macOS.

It's usually a good practice to create your own Python environment for your projects instead of using the default Python installation (consider reading about pyenv).

If you want to use your Homebrew installation, run brew info python to find your Python installation path, then append the parent directory's path to your PATH variable by editing ~/.zshrc (assuming you're using zsh, the default shell).

$ brew info python
....
Python has been installed as
/opt/homebrew/bin/python3
...
# inside ~/.zshrc
...
export PATH="/opt/homebrew/bin:$PATH"
....


Related Topics



Leave a reply



Submit