Move from python 2 to python 3 on Mac - python

I have both python 2.7.16 and python 3.7.3 on my Macbook air.
I don't use python 2.7.16 so I want to remove it, but I understood that this could break my Mac.
I am frustrated from using python3 and pip3 instead of python and pip is there a way to make all of the python3 commands to be accessed by using python (without 3) instead of python 2 and make python 2 be accessible by using python2?
tnx ahead

(base) shrub$ /usr/bin/py
pydoc python python2.7 pythonw
pydoc2.7 python-config python2.7-config pythonw2.7
Looking in /usr/bin there is a python executable (which when run opens a shell with python3) and a python2.7 executable (which when run opens a shell with python2).
(base) shrub$ python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
(base) shrub$ python2.7
Python 2.7.10 (default, Feb 22 2019, 21:55:15)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Running python scripts with python (python hello.py for example) should default to python3 and running scripts with python2.7 (python2.7 hello.py for example) will run them with python2.
Also there is a script called 2to3 which can help you with converting your python2 code to python3 :)
https://docs.python.org/3.0/library/2to3.html

Related

How to setup python3 version on mac?

I have installed python 3.8.0 via pyenv.
pyenv which python
/Users/myname/.pyenv/versions/3.8.0/bin/python
If I run python3 I get
python3
Python 3.6.8rc1 (v3.6.8rc1:cc3e73212a, Dec 11 2018, 17:37:34)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
If I check
which python3
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
How can I set the 3.8.0 version as default?
By typing pyenv global 3.8
You check get the list of options by typing pyenv -h
If you have multiple versions of python installed, you can do python -3.x -m ...
Also check if the env is active before opening it.

Did installing Miniconda destroy Python 2.7 on my Mac?

Have I lost Python 2.7 and does Mac OS still need it? If so, how do I fix this?
I'm a longtime Python 3.x user on Mac OS. I wanted to try Miniconda3 to see if I could recommend it to Mac and Windows students (non-CS). Now I when I try $ type -a python3 I get:
python3 is /Applications/miniconda3/bin/python3
python3 is /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
python3 is /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
python3 is /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
python3 is /usr/local/bin/python3
Try $ type -a python2 and get:
-bash: type: python2: not found
Try $ type -a python and get:
python is /Applications/miniconda3/bin/python
python is /usr/bin/python
I used the Miniconda 64-bit (.pkg installer) under Python 3.7 here:
https://docs.conda.io/en/latest/miniconda.html
Type /usr/bin/python and:
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Okay, it's all good. Thanks, juanpa.arrivillaga!

"python" command can't find Python2 installation

I have screwed up my python install on my MacOS machine.
Here it is -
REMs-MBP:opt rem$ which python
/usr/bin/python
REMs-MBP:opt rem$ python
-bash: /usr/local/opt/python/libexec/bin/python: No such file or directory
REMs-MBP:opt rem$ which python2
/Library/Frameworks/Python.framework/Versions/2.7/bin/python2
REMs-MBP:opt rem$ python2
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 12:01:12)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
python2 works fine but default python is not working fine.
How can I fix it?
brew install python by default tries to install python3. I need python2.7 as my default.
Updates:
pip still pointing to python3 installation.
PREMs-MBP:opt prem$ which python
/usr/bin/python
PREMs-MBP:opt prem$ pip
-bash: /usr/local/bin/pip: /usr/local/Cellar/python/3.7.0/bin/python3.7: bad interpreter: No such file or directory

How to make python to choose a specific version of opencv?

I am using ubuntu14.04 and python2.7 and I have installed opencv3.2.0 (/usr/local) and opencv2.4.8(/usr/local/opencv/2.4.8) in my machine. The outcome of command
pkg-config --modversion opencv
is 2.4.8
while python script
print cv2.__version__
is 3.2.0.
What should I do to change it to 2.4.8?
=========================================================================
I have tried export PYTHONPATH=/usr/local/opencv/2.4.8/:$PYTHONPATH
It seems no use
$ export PYTHONPATH=/usr/local/opencv/2.4.8/:$PYTHONPATH
$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print cv2.__version__
3.2.0-dev
>>>
Although It shows the version is 3.2.0, it is actually using the 2.4.8, thanks for IronFarm's answer
Add the directory for the v2.4.8 to the beginning of your PYTHONPATH environment variable before running Python.
On Linux:
export PYTHONPATH=/usr/local/opencv/2.4.8/:$PYTHONPATH

Howto symlink Python to Python2.7 as non-superuser

I am a non-superuser of a linux machine.
Currently it has 2 versions of Python.
When I invoke standard python command it gave version 2.6
$ python
[neversaint#mach71 ~]$ python
Python 2.6.2 (r262:71600, Jan 28 2011, 13:47:39)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ which python
/opt/somedir/bin/python
It's only when I invoke with python2.7 it gives the version 2.7
[neversaint#mach71 ~]$ python2.7
Python 2.7.6 (default, Nov 11 2013, 13:13:15)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ which phython2.7
/usr/bin/python2.7
My question is how can I set it such that whenever I call $ python it will give me version 2.7.
You can simlink it into some directory both accessible to your user and in your $PATH. For example if /home/<your-username>/local/bin is in your $PATH then you can do
ln -s /usr/bin/python2.7 /home/<your-username>/local/bin/python
In this example /home/<your-username>/local/bin should be in your path before /usr/bin. If there is no such entry in your $PATH you can add it there:
export PATH=$HOME/local/bin:$PATH
You can also add this line to .bashrc or similar to activate it on shell start.
Use a shell alias, alias python=/usr/bin/python2.7 and then python will execute the result of that alias.
in /usr/bin create symlink to python27 or whatever python version you have
sudo ln -s python2.7 python

Categories