How do I install Python 3.6 alongside Python 2.7?

Python newbie here. I just bought a new Mac that came with Python 2.7. I am using an older version of Python for the class, so I need to keep it. I want to install the latest version of Python, 3.6, alongside the older version. The instruction I found on the internet is either outdated or confused. Can anyone point me in the right direction?

+3


source to share


4 answers


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 easiest way to get started with python 3 on macOS.

+3


source


if you download anaconda , a very common download for python development, you get a great package manager and a very easy way to create sandboxes. After loading anaconda (for your current Python, so 2.7) you can open your terminal and type:

conda create my_new_env_name python=3.6

      

which will create a new sandbox with python3.6. to use this environment type in your shell

source active my_new_env_name

      



now if you type python

from a shell which is in python3.6 or you can run python somefile.py

from a shell to run it in python3.6

This is a great way to maintain and manage different versions of libraries on your system. For example, if you need an old version of a specific Python library for a specific project, but don't want to downgrade that library for all of your Python code.

Learn more about managing conda environments on the documentation page

+2


source


There is another way to have multiple python versions using a virtual environment.

step1: Download the 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 have to do this every time, it's a one-time job. Once created, you simply activate it, and after completing it, you can deactivate it.

Also, working with virtualenv will help you keep your different package versions separate without messing up your system settings.

0


source


If you are using Ubuntu 17.10, python 3 is already installed. You can invoke it by typing python3. If you have already installed python 2 by typing python -version it will display the python 2 version and by typing python3 --version you will see the python 3 version so we can use both versions

0


source







All Articles