Reputation: 149
Python Newbie here. I just bought a new Mac that came with Python 2.7. I'm using the older version of Python for a class so I need to keep it. I want to install the latest version of Python, 3.6, side by side the older version. The instruction I found online were either outdated or confusing. Can anyone point me in the right direction?
Upvotes: 8
Views: 8464
Reputation: 31
If you are using Ubuntu 17.10 python 3 is already installed. You can invoke it by typing python3. If you already installed python 2, by typing python --version it shows python 2 version and by typing python3 --version it shows python 3 version. so we can use both versions
Upvotes: 0
Reputation: 242
There is one more way of having multiple python versions, using virtual environment.
step1: Download python versions you want to run.
step2: virtualenv -p {python_location} {env_name}
step3: (for mac) . env_name/bin/activate
For example (Running Python 3.6):
~ abhinavkumar$ virtualenv -p /usr/local/bin/python3.6 py36
Running virtualenv with interpreter /usr/local/bin/python3.6
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/abhinavkumar/py36/bin/python3.6
Also creating executable in /Users/abhinavkumar/py36/bin/python
Installing setuptools, pip, wheel...done.
~ abhinavkumar$ . py36/bin/activate
(py36) ~ abhinavkumar$ which python
/Users/abhinavkumar/py36/bin/python
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Running python 2.7
~ abhinavkumar$ virtualenv -p /usr/bin/python2.7 py27
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /Users/abhinavkumar/py27/bin/python
Installing setuptools, pip, wheel...done.
~ abhinavkumar$ . py27/bin/activate
(py27) ~ abhinavkumar$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
You don't need to do this everytime, this is one time job. Once created you just have activate it and once done you can deactivate.
Additionally working with virtualenv help you to segregate your different packages versions, without messing up with your systems settings.
Upvotes: 0
Reputation: 8954
if you download anaconda, a very common download for python development, you get a great package manager and a very easy way to create sandboxed environments. After downloading anaconda (for your current Python, so 2.7), you can open up your terminal and enter:
conda create my_new_env_name python=3.6
that will create a new sandboxed environment with python3.6. to use that environment, enter in your shell
source active my_new_env_name
now if you enter python
from the shell you're in python3.6, or you can run python somefile.py
from the shell to run it in python3.6
This is a great way to maintain and manage different versions of libraries on your system as well. For example, if you need an old version of a specific Python library for a particular project, but don't want to downgrade that library for all your Python code.
More on managing conda environments at the documentation page
Upvotes: 4
Reputation: 186
You can use brew to install python3.
$ brew install python3
$ python # to start the python 2.7
$ python3 # to start the python 3
This is the simplest way to get started with python 3 on macOS.
Upvotes: 6