How do I have two different pythons at the same place - python

Why does the same executable file report two different versions depending on the user/superuser status?
host:tmp$ /usr/bin/python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D
host:tmp drf$ sudo bash
bash-3.2# /usr/bin/python
Python 2.6.8 (unknown, Mar 9 2014, 22:16:00)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D
bash-3.2# ls -la /usr/bin/python
-rwxr-xr-x 2 root wheel 58608 May 15 17:32 /usr/bin/python
bash-3.2# file /usr/bin/python
/usr/bin/python: Mach-O universal binary with 2 architectures
/usr/bin/python (for architecture x86_64): Mach-O 64-bit executable x86_64
/usr/bin/python (for architecture i386): Mach-O executable i386

/usr/bin/python in Mac OSX is a proxy program that selects between different versions of python based on environment variables. The python(1) manual page describes this in some detail. In short, the python interpreter version can be selected by:
defining the VERSIONER_PYTHON_VERSION environment variable to a system python version or
setting the Version property of the com.apple.versioner.python preference using defaults write
In either case, it will only select from the interpreters in /System/Library/Frameworks/Python.framework/Versions.

Related

How to switch AWS CLI to Python 3.7 on macOS?

On macOS Mojave 10.14.6, have the following versions of python installed:
Python 2.7.16
Python 2.7.16 (default, May 17 2019, 18:32:33)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Python 3
Python 3.7.4 (v3.7.4:e09359112e, Jul 8 2019, 14:54:52)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Installed AWS CLI using these steps:
curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
unzip awscli-bundle.zip
sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
Now, when I test aws from the command line using aws --version
aws-cli/1.16.247 Python/2.7.16 Darwin/18.7.0 botocore/1.12.237
Question(s):
Is this correct?
Is it supposed to be using Python 3.7.4 instead?
If so, how can I switch this over?
By default the installer will use whatever is in your path for python.
If you want to use Python 3, you need to run something like this to run the installer:
sudo /usr/local/bin/python3 awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
Replace /usr/local/bin/python3 with your path for Python 3 if it's different.
All of this said, if you have both Python environments installed, I'm not aware of any reason to prefer one version over the other for the AWS CLI tool.

How to change the default gcc compiler for python

I have installed gcc 4.9.2 in my computer (mac osx 10.10, 64 bit), while gcc version 4.2 is shown in Python environment. I don't know why this happened.
I encountered some issues when calling numpy.f2py function, I thought it may be related with the incompatible gcc compiler.
1.Python shell
▶ python
Python 3.5.5 | packaged by conda-forge | (default, Jul 23 2018, 23:45:11)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
2.In terminal
▶ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/lto-wrapper
Target: x86_64-apple-darwin14.0.0
Configured with: ../gcc-4.9-20141029/configure --enable-languages=c++,fortran
Thread model: posix
gcc version 4.9.2 20141029 (prerelease) (GCC)
I thought this problem may be due to that the gcc compiler was installed after python. How to fix this issue, wish for your advice.
I believe that [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] you get when you start the python interactive shell is the compiler that was used to compile the python interpreter, it has nothing to do with which compiler is used with f2py.

What does GCC have to do with a python interpreter?

I just noticed this on my macintosh. Running $ python fires up an interpreter session with the following lines:
$ python2.7
Python 2.7.10 (default, Feb 6 2017, 23:53:20)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
The second line of the starting up text mentions GCC and clang versions.
How are these two related to the python interpreter? Given that python is an interpreted language, there should be no whisper of a compiler at all, so I was curious as to why this is shown.
Now here's the same thing with python3.6:
$ python3.6
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
This time there's no mention of clang. Why's that?
The CPython interpreter is itself written in C. It matters what compiler was used to convert the C code into a binary executable; behaviour and performance can differ in subtle ways, so it is mentioned in the banner.
You have two different Python binaries, the differences in the banner reflect differences in how those binaries where built. Since the Python 2.7 release is the one that comes bundled with OS X, it was built by Apple engineers using a different toolchain (using the clang compiler) from the Python 3.6 installation, which you must have installed separately (OS X doesn't include Python 3.6 yet). The latter was compiled with the GCC compiler.

ImportError from non-standard Python even though module exists

We have a setup based on CentOS 6.4 but with Python 2.7 (due to historical reasons). Note that CentOS 6 brings Python 2.6. Python 2.7 has been compiled from a Fedora 20 SRPM. In addition, to make it possible to freely use Python RPMs from CentOS 6 along with the 2.7 interpreter, we have created a /usr/lib/python2.7/site-packages/setup.pth file with the contents:
/usr/lib64/python2.6/site-packages
/usr/lib/python2.6/site-packages
Things used to work fine till the need to use protobuf-python-2.3.0-9.el6.x86_64 arose. This RPM has been downloaded from the EPEL repo and is available: here.
$ /usr/bin/python2.7
Python 2.7.4 (default, Mar 17 2015, 00:48:39)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import google.protobuf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named google.protobuf
One thing that could be helpful is that the import error does not come with Python 2.6:
$ /usr/bin/python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import google.protobuf
This error does not come with other import statements (e.g. import pip.vcs). It is somehow related to protobuf-python. What could be the reason? Any insight will be appreciated.
EDIT: The protobuf-python RPM installs files into /usr/lib/python2.6/site-packages/google/protobuf and the the 2.7 interpreter's sys.path is:
$ /usr/bin/python2.7 -c 'import sys; print sys.path' | sed -e 's/,/\n/g'
[''
'/usr/lib64/python27.zip'
'/usr/lib64/python2.7'
'/usr/lib64/python2.7/plat-linux2'
'/usr/lib64/python2.7/lib-tk'
'/usr/lib64/python2.7/lib-old'
'/usr/lib64/python2.7/lib-dynload'
'/usr/lib64/python2.7/site-packages'
'/usr/lib/python2.7/site-packages'
'/usr/lib64/python2.6/site-packages'
'/usr/lib/python2.6/site-packages']

MacPorts on Snow Leopard: Python install seems to succeed but doesn't install a non-system Python

I've installed Python via MacPorts. According to this question, the files in /opt/local/bin should run the "correct" Python version. However, all those files are symlinks to:
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin/
Running them directly from that folder (using no symlinks) runs an Apple build of Python!
Python 2.6.6 (r266:84292, Jan 6 2011, 13:25:25)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
For comparison, running /usr/bin/python shows a slightly different version:
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.
>>>
They're both Apple builds! How do I install the correct version?
There are no "Apple builds" except the system Python (which is 2.6.1 on Snow Leopard, afai can deduce). The ports install seems to have worked as expected in this case. The reference to Apple is a reference to which GCC that was used.
I think it is running the build you want.
macports install every thing in /opt/local. So /opt/local/bin/python is a macport distribution.
/usr/bin/python is pre-install python distribution that comes with mac os x.
The following line, is an information about gcc build which is used for building macport python distribution.
[GCC 4.2.1 (Apple Inc. build 5664)]
If you have multiple versions of macport python installed, you can select the version using python-select
sudo port install python-select sudo
python-select python26
Also to ensure that you use macport python you could add the following to the PATH
export PATH=/opt/local/bin:$PATH

Categories

Resources