简体   繁体   English

如何安装Python 3.6和Python 2.7?

[英]How to Install Python 3.6 along with Python 2.7?

Python Newbie here. Python新手在这里。 I just bought a new Mac that came with Python 2.7. 我刚刚买了一台Python 2.7附带的新Mac。 I'm using the older version of Python for a class so I need to keep it. 我正在使用旧版本的Python,所以我需要保留它。 I want to install the latest version of Python, 3.6, side by side the older version. 我想安装最新版本的Python,3.6,并排旧版本。 The instruction I found online were either outdated or confusing. 我在网上发现的指令要么过时,要么令人困惑。 Can anyone point me in the right direction? 谁能指出我正确的方向?

You can use brew to install python3. 你可以使用brew来安装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. 这是在macOS上开始使用python 3的最简单方法。

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. 如果你下载anaconda ,一个非常常见的python开发下载,你会得到一个很棒的包管理器和一个非常简单的方法来创建沙盒环境。 After downloading anaconda (for your current Python, so 2.7), you can open up your terminal and enter: 下载anaconda(对于你当前的Python,所以2.7),你可以打开你的终端并输入:

conda create my_new_env_name python=3.6

that will create a new sandboxed environment with python3.6. 这将使用python3.6创建一个新的沙盒环境。 to use that environment, enter in your shell 要使用该环境,请输入您的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 现在如果你从shell中输入python你在python3.6中,或者你可以从shell运行python somefile.py来在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. 例如,如果您需要特定项目的旧版本特定Python库,但不希望为所有Python代码降级该库。

More on managing conda environments at the documentation page 有关在文档页面上管理conda环境的更多信息

There is one more way of having multiple python versions, using virtual environment. 使用虚拟环境还有一种拥有多个python版本的方法。

step1: Download python versions you want to run. step1:下载要运行的python版本。

step2: virtualenv -p {python_location} {env_name} step2:virtualenv -p {python_location} {env_name}

step3: (for mac) . step3 :(对于mac)。 env_name/bin/activate env_name /斌/启动

For example (Running Python 3.6): 例如(运行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 运行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. 此外,使用virtualenv可帮助您隔离不同的软件包版本,而不会弄乱您的系统设置。

If you are using Ubuntu 17.10 python 3 is already installed. 如果您使用的是Ubuntu 17.10已经安装了python 3。 You can invoke it by typing python3. 您可以通过键入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. 如果你已经安装了python 2,通过输入python --version它会显示python 2版本,输入python3 --version会显示python 3版本。 so we can use both versions 所以我们可以使用这两个版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM