'Need help in resolving issue with two versions of python available in my Macbook
I recently bought macbook and i'm new to everything in this OS. Followed some tutorials to setup the machine towards programming and development. In that way, i had installed python(3.9) through Homebrew, later while checking path in both brew and terminal, both are pointing out to python 2.7.16, Then i realized Mac OS already had its own installation with 2.7.16. Now i am going through multiple suggestion on web, that how to overcome this and to make a single version as default. I found the below commands to link brew's version(3.9.15) with system's version(2.7.16). Copied from another post.
[[ Here is what was confusing me and how I solved it.
$ which python
/usr/bin/python
$ which python3
/usr/local/bin/python3
$ ls /usr/local/bin/python
ls: /usr/local/bin/python: No such file or directory
So notice I didn't have a HomeBrew installation of python2.7, but did have the python3 installation. The version under /usr/bin/python is using the system default. You can tell based on the module search path:
$ /usr/bin/python
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
`enter code here`Type "help", "copyright", "credits" or "license" for
more information.
>>> import sys
>>> sys.path
['', '/Library/Python/2.7/...
Notice the '/Library/Python'... that's Mac OS's version of python. But I want to stay strictly on a user installed version (i.e. HomeBrew).
So here's what I did to fix this:
$ brew install python
...
Warning: python 2.7.13 is already installed, it's just not linked.
You can use `brew link python` to link this version.
$ brew link --overwrite python
$ which python
/usr/local/bin/python
$ python
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[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.
>>> import sys
>>> sys.path
['', '/usr/local/Cellar/python/2.7.13...
Its no longer /Library/.. but /usr/local.
Now its finding all of my pip installed modules! Problem solved! ]]
The above Steps are actually dealing the merging of similar version ~ 2.7 which is of Python2.
But in my machine, i had installed Python3 and it has Python2 earlier.
Here is my question.
Do i have to update the System's version first to Python3 and then link it with Brew's version(3.9.15) by following the above commands or Any suggestions please??
Solution 1:[1]
Do i have to update the System's version first to Python3 and then link it with Brew's version(3.9.15)
python2 and python3 are incompatible software. You shouldn't try to "update" that version to python3. Because if you do that existing software might stop working, as it probably depends on python2. Your best course of action would be to use python3 version from brew explicitly, python2 is in deprecation mode anyway. (I use alias like alias py3=python3, so I can invoke it with py3 instead of writing python3)
As an aside - you should set up your projects/programs using virtualenv/venv; that will allow to not pollute your system installation. You can even share that with another computer by extracting project dependencies in requirements.txt.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 2-sticks |
