Linux下Python环境部署

版权声明:xueliangwang https://blog.csdn.net/ShyLoneGirl/article/details/83017139

前言

首先说明一点,在CentOS 7.2下系统自带python 2.7,如果对版本有更高要求则需要升级,本文安装的是python2 和 python 3 共存环境。
Linux下部署python(3.6)环境(事实上就是安装python啦)有3种简单方法,可任选其一。

  1. 下载python源码包编译安装
  2. 使用系统的包管理工具一键式安装
    如RedHat系列(CentOS,Fedora)的yum,Ubuntu系列的apt。
  3. 或者直接Anaconda
    Anaconda指的是一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其依赖项。如果只需要某些包,或者需要节省带宽或存储空间,也可以使用Miniconda这个较小的发行版(仅包含conda和 Python)。

源码编译安装

1/2_执行命令下载源码,编译,安装。

[root@VM_0_13_centos home]# wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
[root@VM_0_13_centos home]# tar -zxvf Python-3.6.2.tgz
[root@VM_0_13_centos home]# cd Python-3.6.2/
[root@VM_0_13_centos Python-3.6.2]# ./configure --prefix=/usr/local/python3
[root@VM_0_13_centos Python-3.6.2]# make
[root@VM_0_13_centos Python-3.6.2]# make install

2/2_创建软连接,测试

[root@VM_0_13_centos Python-3.6.2]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@VM_0_13_centos Python-3.6.2]# python3 --version                           Python 3.6.2

YUM方式

1/4_安装EPEL和IUS源

EPEL的全称叫 Extra Packages for Enterprise Linux 。EPEL是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS、Scientific Linux 等提供高质量软件包的项目。装上了 EPEL之后,就相当于添加了一个第三方源。IUS只为RHEL和CentOS这两个发行版提供较新版本的rpm包。如果在os或epel找不到某个软件的新版rpm,软件官方又只提供源代码包的时候,可以来ius源中找,几乎都能找到。
下面详细说明安装过程,黄色阴影部分为输入的命令。

[root@VM_0_13_centos ~]# yum install epel-release


Total download size: 15 k
Installed size: 24 k
Is this ok [y/d/N]: y


Installed:
epel-release.noarch 0:7-11
Complete!
[root@VM_0_13_centos ~]# yum install https://centos7.iuscommunity.org/ius-release.rpm
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
ius-release.rpm


Install 1 Package
Total size: 8.5 k
Installed size: 8.5 k
Is this ok [y/d/N]: y


Installed:
ius-release.noarch 0:1.0-15.ius.centos7
Complete!

2/4_安装python 3.6

[root@VM_0_13_centos ~]# yum install python36u
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile


Install 1 Package (+1 Dependent package)
Total download size: 8.8 M
Installed size: 40 M
Is this ok [y/d/N]: y


Retrieving key from file:///etc/pki/rpm-gpg/IUS-COMMUNITY-GPG-KEY
Importing GPG key 0x9CD4953F:
Userid : “IUS Community Project [email protected]
Fingerprint: 8b84 6e3a b3fe 6462 74e8 670f da22 1cdf 9cd4 953f
Package : ius-release-1.0-15.ius.centos7.noarch (installed)
From : /etc/pki/rpm-gpg/IUS-COMMUNITY-GPG-KEY
Is this ok [y/N]: y
Dependency Installed:
python36u-libs.x86_64 0:3.6.5-1.ius.centos7
Complete!

3/4_配置常用命令

首先使用一下python,python3,以及python3.6命令试试。

[root@VM_0_13_centos ~]# python --version
Python 2.7.5
[root@VM_0_13_centos ~]# python3 --version
-bash: python3: command not found
[root@VM_0_13_centos ~]# python3.6 --version
Python 3.6.5

默认python版本为python 2.7,但是python 3会更经常用一些。所以最好是,也有必要创建一个软连接,目的是使python3 这个命令有效。

[root@VM_0_13_centos ~]# ln -s /usr/bin/python3.6 /usr/bin/python3
[root@VM_0_13_centos ~]# python3 --version
Python 3.6.5

4/4_安装pip

这是一个通用的 Python 包管理工具。提供了对 Python 包的查找、下载、安装、卸载的功能。可以说是不可缺少的
[root@VM_0_13_centos ~]# yum install python36u-pip
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile


Install 1 Package (+1 Dependent package)
Total download size: 2.4 M
Installed size: 13 M
Is this ok [y/d/N]: y


Complete!
同样是建立一个软链接。

[root@VM_0_13_centos ~]# ln -s /bin/pip3.6 /bin/pip
[root@VM_0_13_centos ~]# pip --version
pip 9.0.1 from /usr/lib/python3.6/site-packages (python 3.6)

这样就OK啦。

Anaconda安装

1/3_下载anaconda

2/3_安装Anaconda

执行安装命令安装Anaconda,安装过程有几处选项需要选,不要把Enter按的太快而错过,下面详细说明。

[root@VM_0_13_centos ~]# sh Anaconda3-5.2.0-Linux-x86_64.sh
  • 2.1 Entet 开始
    Please, press ENTER to continue
    >>>

  • 2.2 同意条款

Do you accept the license terms? [yes|no]
[no] >>> yes

  • 2.3 安装位置可选(最好是自定义位置安装),一般来说安在这里

Anaconda3 will now be installed into this location:
/root/anaconda3

  • Press ENTER to confirm the location
  • Press CTRL-C to abort the installation
  • Or specify a different location below
    [/root/anaconda3] >>> /usr/local/anaconda3
    PREFIX=/usr/local/anaconda3
    然后就开始安装了…

Do you wish the installer to prepend the Anaconda3 install location
to PATH in your /root/.bashrc ? [yes|no]
[no] >>> yes
Appending source /usr/local/anaconda3/bin/activate to /root/.bashrc
A backup will be made to: /root/.bashrc-anaconda3.bak

For this change to become active, you have to open a new terminal.

Thank you for installing Anaconda3!

  • 2.5 安装已经完成 问安不安vscode,选择不安装。

Vscode ? no
Do you wish to proceed with the installation of Microsoft VSCode? [yes|no]
>>> no

3/3_测试anaconda

首先执行命令使甘刚刚的设置立即生效。
[root@VM_0_13_centos ~]# source ~/.bashrc
然后测试一下以下命令

[root@VM_0_13_centos ~]# python --version
Python 3.6.5 :: Anaconda, Inc.
[root@VM_0_13_centos ~]# conda --version
conda 4.5.4

OK

.
.
.
.
.
.

猜你喜欢

转载自blog.csdn.net/ShyLoneGirl/article/details/83017139
今日推荐