前言
在Linux系统上一般会预安装python,但有时候版本过低,通过apt或yum无法安装较新的python版本,只能通过编译python源码进行安装。然而通过源码安装会依赖大量的库,手动安装这些依赖库也是一件很烦心的事情。而且还可能出现依赖库冲突的问题,装不好可能会破坏系统的正常使用。
有时间的小伙伴可以试试通过源码编译安装,遇到问题然后解决问题也是一件很有成就感的事情。安装过程可能会一次性成功,也可能弄一天也安装不好,最好使用虚拟机,安装之前先做个快照,方便安装失败进行系统还原。下面简单说明一下源码安装过程。
python源码安装
进入Python官网,选择Downloads选项里面的Source code,选择合适的版本下载,比如3.6.7版本,下载后是一个Python-3.6.7.tar.xz文件。
然后解压 tar -xvf Python-3.6.7.tar.xz
运行 ./configure --prefix=/usr
然后 make ,编译完成后(当然是一切顺利的情况下)运行 make 进行安装。关于 make install 和 make 的区别可以查看Python官网文档说明。
好了,源码编译安装就介绍到这里,这不是今天要讲的内容。
Anaconda
在python编码编译安装过程中,你可能会安装很多依赖库,是不是有点心烦呢。现在介绍anaconda,可以不再让你心烦意乱,轻轻松松安装python环境(而且是任意python版本)。
我这里不在过多的介绍anaconda,详细介绍请看anaconda官网,学习一个工具或产品,或者遇到问题,要先去查看官网文档,一定要养成这个好的习惯。
安装Anaconda
官网下载安装包,这里选择的是Linux版本的Python3 64位。(建议选择Python3,Python2很快就停止维护了,除了一些以前用Python2开发好的现在需要维护的程序,现在统统都换成Python3吧)
下载完成后是一个Anaconda3-2018.12-Linux-x86_64.sh文件,下面开始安装吧
在Linux终端上运行 bash ~/Anaconda3-2018.12-Linux-x86_64.sh
Welcome to Anaconda3 2018.12
In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>>
按enter键继续,出现 Anaconda End User License Agreement
Do you accept the license terms? [yes|no]
[no] >>>
输入yes继续,出现
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] >>>
让我们选择安装路径,默认是/root/anaconda3,这里输入/opt/anaconda3安装到/opt目录下,
过程会安装很多依赖库,这里只是截取了一部分,这些依赖库不会和系统中的库冲突的。
installing: icu-58.2-h9c2bf20_1 ...
installing: jbig-2.1-hdba287a_0 ...
installing: jpeg-9b-h024ee3a_2 ...
installing: libffi-3.2.1-hd88cf55_4 ...
installing: liblief-0.9.0-h7725739_1 ...
installing: libsodium-1.0.16-h1bed415_0 ...
installing: libtool-2.4.6-h7b6447c_5 ...
installing: libuuid-1.0.3-h1bed415_2 ...
installing: libxcb-1.13-h1bed415_1 ...
installing: lz4-c-1.8.1.2-h14c3975_0 ...
installing: lzo-2.10-h49e0be7_2 ...
installing: mkl-2019.1-144 ...
installing: ncurses-6.1-he6710b0_1 ...
installing: openssl-1.1.1a-h7b6447c_0 ...
installing: patchelf-0.9-he6710b0_3 ...
installing: pcre-8.42-h439df22_0 ...
installing: pixman-0.34.0-hceecf20_3 ...
installing: snappy-1.1.7-hbae5bb6_3 ...
installing: xz-5.2.4-h14c3975_4 ...
installing: yaml-0.1.7-had09818_2 ...
installing: zlib-1.2.11-h7b6447c_3 ...
安装完成后,询问我们是否要将环境变量加入~/.bashrc文件中
installation finished.
Do you wish the installer to initialize Anaconda3
in your /root/.bashrc ? [yes|no]
[no] >>> yes
Initializing Anaconda3 in /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!
我们输入yes,安装完成。
有时候还会出现询问是否安装VSCode
Anaconda is partnered with Microsoft! Microsoft VSCode is a streamlined
code editor with support for development operations like debugging, task
running and version control.
To install Visual Studio Code, you will need:
- Administrator Privileges
- Internet connectivity
Visual Studio Code License: https://code.visualstudio.com/license
Do you wish to proceed with the installation of Microsoft VSCode? [yes|no]
>>> no
输入no,至此安装完成。
查看anaconda版本信息
上面安装完成后,运行 source ~/.bashrc
[root@neokylin ~]# conda --version
conda 4.5.12
[root@neokylin ~]#
更多命令使用 conda --help
Anaconda使用
默认python及pip使用
python绝对路径为/opt/anaconda3/bin/python
[root@neokylin ~]# /opt/anaconda3/bin/python
Python 3.7.1 (default, Dec 14 2018, 19:28:38)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world!")
hello world!
>>> exit()
[root@neokylin ~]#
pip绝对路径为/opt/anaconda3/bin/pip, /opt/anaconda3/bin/pip list
使用虚拟环境
假如我们不想在默认的anaconda环境中安装其他模块,或者不想使用默认的Python3.7.1,那我们可以使用虚拟环境,
我们创建一个名为py36的虚拟环境,指定python版本为3.6.7,可以执行 conda create -n py36 python=3.6.7
# To activate this environment, use
#
# $ conda activate py36
#
# To deactivate an active environment, use
#
# $ conda deactivate
我们使用 conda activate py36 进入虚拟环境,查看python版本,使用pip list,最后再通过 conda deactivate
[root@neokylin ~]# conda activate py36
(py36) [root@neokylin ~]# python --version
Python 3.6.7 :: Anaconda, Inc.
(py36) [root@neokylin ~]# python
Python 3.6.7 |Anaconda, Inc.| (default, Oct 23 2018, 19:16:44)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world!")
hello world!
>>> exit()
(py36) [root@neokylin ~]# pip list
Package Version
---------- --------
certifi 2019.3.9
pip 19.0.3
setuptools 40.8.0
wheel 0.33.1
(py36) [root@neokylin ~]# conda deactivate
(base) [root@neokylin ~]# conda deactivate
[root@neokylin ~]#
如果通过 conda deactivate 退出后,出现 (base) ,可以通过再次执行 conda deactivate
所有的虚拟环境都在anaconda安装目录下的envs目录下。
我们也可以直接使用虚拟环境中的python的绝对路径来运行 /opt/anaconda3/envs/py36/bin/python --version 而不需要进入虚拟环境。类似的pip也是可以的 /opt/anaconda3/envs/py36/bin/pip list