linux中python安装

Posted 七彩蜗牛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux中python安装相关的知识,希望对你有一定的参考价值。

1、查看当前环境中是否存在python安装包

[[email protected] ~]$  rpm -qa | grep python
gnome-python2-gnome-2.28.0-3.el6.x86_64
python-slip-0.2.20-1.el6_2.noarch
python-iniparse-0.3.1-2.1.el6.noarch
python-pycurl-7.19.0-9.el6.x86_64
gnome-python2-gconf-2.28.0-3.el6.x86_64
gnome-python2-extras-2.25.3-20.el6.x86_64
gnome-python2-libegg-2.25.3-20.el6.x86_64
notify-python-0.1.1-10.el6.x86_64
python-libs-2.6.6-66.el6_8.x86_64
python-ethtool-0.6-6.el6.x86_64
python-mako-0.3.4-1.el6.noarch
abrt-python-2.0.8-43.el6.centos.x86_64
gnome-python2-gnomekeyring-2.28.0-5.el6.x86_64
gnome-python2-bonobo-2.28.0-3.el6.x86_64
python-meh-0.12.1-3.el6.noarch
python-2.6.6-66.el6_8.x86_64
libuser-python-0.56.13-8.el6_7.x86_64

2、在官网上查看最新的版本;也可以通过yum install 安装python安装包,但是有可能yum源上面没有最新的python版本,所以采用下载的方式安装最新的安装包。

https://www.python.org/downloads/source/

[[email protected] home]# yum info python  //查看yum源上的py安装包信息
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Installed Packages
Name        : python
Arch        : x86_64
Version     : 2.6.6
Release     : 66.el6_8
Size        : 78 k
Repo        : installed
From repo   : anaconda-CentOS-201703281317.x86_64
Summary     : An interpreted, interactive, object-oriented programming language
URL         : http://www.python.org/
License     : Python
Description : Python is an interpreted, interactive, object-oriented programming
            : language often compared to Tcl, Perl, Scheme or Java. Python includes
            : modules, classes, exceptions, very high level dynamic data types and
            : dynamic typing. Python supports interfaces to many system calls and
            : libraries, as well as to various windowing systems (X11, Motif, Tk,
            : Mac and MFC).
            : 
            : Programmers can write new built-in modules for Python in C or C++.
            : Python can be used as an extension language for applications that need
            : a programmable interface.
            : 
            : Note that documentation for Python is provided in the python-docs
            : package.
            : 
            : This package provides the "python" executable; most of the actual
            : implementation is within the "python-libs" package.
[[email protected] home]# 
[[email protected] home]# yum search python3  //查询yum源是否有?
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Warning: No matches found for: python3
No Matches found

3、安装python源码:


  •  下载:将安装包上传到linux的家目录下
[[email protected] home]# cd zyj
[[email protected] zyj]# ll
total 31312
drwxr-xr-x. 2 zyj zyj     4096 Apr 14 09:13 Desktop
drwxr-xr-x. 2 zyj zyj     4096 Apr 14 09:13 Documents
drwxr-xr-x. 2 zyj zyj     4096 Apr 14 09:13 Downloads
drwxr-xr-x. 2 zyj zyj     4096 Apr 14 09:13 Music
drwxr-xr-x. 2 zyj zyj     4096 Apr 14 09:13 Pictures
drwxr-xr-x. 2 zyj zyj     4096 Apr 14 09:13 Public
-rw-rw-r--. 1 zyj zyj 22994617 May  1  2018 Python-3.6.5.tgz
drwxr-xr-x. 2 zyj zyj     4096 Apr 14 09:13 Templates
drwxr-xr-x. 2 zyj zyj     4096 Apr 14 09:13 Videos
-rw-rw-r--. 1 zyj zyj  9034460 Apr 27 21:23 mysql-server-5.1.73-8.el6_8.x86_64.rpm
[[email protected] zyj]# mv Python-3.6.5.tgz ../
[[email protected] home]# ll
total 22472
-rwxrwxrwx.  1 root root       10 Apr 25 11:05 1.txt
-rw-rw-r--.  1 zyj  zyj  22994617 May  1  2018 Python-3.6.5.tgz
-rwxrwxrwx.  1 zyj  zyj        86 Apr 15 09:23 hello.txt
drwxr-xrwx.  2 root root     4096 Apr 15 07:00 x
drwx------. 24 zyj  zyj      4096 May  1 00:00 zyj
  • 查看源码(.c结尾为c语言):在windows下打开压缩包,查看里面文件的后缀。
  • 准备编译环境(检查c语言编译环境,gcc)
[[email protected] home]# rpm -qa | grep gcc
libgcc-4.4.7-18.el6.x86_64
gcc-4.4.7-18.el6.x86_64
[[email protected] home]# yum install gcc-c++ //安装c++编译环境
  • 检查(依赖、兼容),预编译:
[[email protected] home]# tar -zxvf Python-3.6.5.tgz //解压安装包
[[email protected] home]# ls
1.txt  Python-3.6.5  Python-3.6.5.tgz  hello.txt  x  zyj
[[email protected] home]# cd Python-3.6.5 //进入安装包目录下
[[email protected] Python-3.6.5]# ll
total 1036
drwxr-xr-x. 18 sl sl   4096 Mar 28 03:24 Doc
drwxr-xr-x.  2 sl sl   4096 Mar 28 02:19 Grammar
drwxr-xr-x.  2 sl sl   4096 Mar 28 02:19 Include
-rw-r--r--.  1 sl sl  12763 Mar 28 02:19 LICENSE
drwxr-xr-x. 33 sl sl   4096 Mar 28 02:19 Lib
drwxr-xr-x.  8 sl sl   4096 Mar 28 02:19 Mac
-rw-r--r--.  1 sl sl  61032 Mar 28 02:19 Makefile.pre.in
drwxr-xr-x.  2 sl sl   4096 Mar 28 03:24 Misc
drwxr-xr-x. 13 sl sl   4096 Mar 28 02:19 Modules
drwxr-xr-x.  4 sl sl   4096 Mar 28 02:19 Objects
drwxr-xr-x.  6 sl sl   4096 Mar 28 02:19 PC
drwxr-xr-x.  2 sl sl   4096 Mar 28 02:19 PCbuild
drwxr-xr-x.  2 sl sl   4096 Mar 28 02:19 Parser
drwxr-xr-x.  2 sl sl   4096 Mar 28 02:19 Programs
drwxr-xr-x.  3 sl sl   4096 Mar 28 02:19 Python
-rw-r--r--.  1 sl sl   9281 Mar 28 02:19 README.rst
drwxr-xr-x. 23 sl sl   4096 Mar 28 02:19 Tools
-rw-r--r--.  1 sl sl  13335 Mar 28 02:19 aclocal.m4
-rwxr-xr-x.  1 sl sl  44259 Mar 28 02:19 config.guess
-rwxr-xr-x.  1 sl sl  36515 Mar 28 02:19 config.sub
-rwxr-xr-x.  1 sl sl 490243 Mar 28 02:19 configure
-rw-r--r--.  1 sl sl 163178 Mar 28 02:19 configure.ac
-rwxr-xr-x.  1 sl sl   7122 Mar 28 02:19 install-sh
-rw-r--r--.  1 sl sl  42152 Mar 28 02:19 pyconfig.h.in
-rw-r--r--.  1 sl sl 104570 Mar 28 02:19 setup.py

执行可执行文件:
[[email protected] Python-3.6.5]# ./configure --help
`configure‘ configures python 3.6 to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
      --help=short        display options specific to this package
      --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
  -q, --quiet, --silent   do not print `checking ...‘ messages
      --cache-file=FILE   cache test results in FILE [disabled]
  -C, --config-cache      alias for `--cache-file=config.cache‘
  -n, --no-create         do not create output files
      --srcdir=DIR        find the sources in DIR [configure dir or `..‘]

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]
......

Report bugs to <https://bugs.python.org/>.

[[email protected] Python-3.6.5]# ./configure --prefix=/usr/python-3.6.5  //第三方软件安装在/usr/ 目录下。
checking whether gcc -pthread supports computed gotos... yes
checking for build directories... done
checking for -O2... yes
checking for glibc _FORTIFY_SOURCE/memmove bug... no
checking for gcc ipa-pure-const bug... no
checking for stdatomic.h... no
checking for GCC >= 4.7 __atomic builtins... no
checking for ensurepip... upgrade
checking if the dirent structure of a d_type field... yes
checking for the Linux getrandom() syscall... no
checking for the getrandom() function... no
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Modules/Setup.config
config.status: creating Misc/python.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile
If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations

准备安装依赖包:zlib、openssl。python的pip需要依赖这两个包。

[[email protected] home]# yum install zlib* openssl*  //安装依赖包
[[email protected] home]# cd Python-3.6.5
[[email protected] Python-3.6.5]# ls
Doc      LICENSE  Makefile         Misc     PC       Programs    Tools         config.log     configure     pyconfig.h
Grammar  Lib      Makefile.pre     Modules  PCbuild  Python      aclocal.m4    config.status  configure.ac  pyconfig.h.in
Include  Mac      Makefile.pre.in  Objects  Parser   README.rst  config.guess  config.sub     install-sh    setup.py
[[email protected] Python-3.6.5]# ./configure --prefix=/usr/python-3.6.5 --enable-optimizations  //完整的预编译
...
creating Modules/Setup
creating Modules/Setup.local
creating Makefile  //没有任何报错
[[email protected] Python-3.6.5]# 
  • 编译:make
[[email protected] Python-3.6.5]# make
...
make[1]: Entering directory `/home/Python-3.6.5find . -name *.gc?? -exec rm -f {} ;
find . -name *.profclang? -exec rm -f {} ;
find . -name *.dyn -exec rm -f {} ;
rm -f /home/Python-3.6.5/coverage.info
rm -rf /home/Python-3.6.5/lcov-report
make[1]: Leaving directory `/home/Python-3.6.5[[email protected] Python-3.6.5]# //一般检查最后几行有没有error
  • 安装:make install
[[email protected] Python-3.6.5]#make install
......
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.3 setuptools-39.0.1
[[email protected] Python-3.6.5]#

 

  • 配置环境变量

 



以上是关于linux中python安装的主要内容,如果未能解决你的问题,请参考以下文章

python 用于数据探索的Python代码片段(例如,在数据科学项目中)

如何在 python 中并行化以下代码片段?

Python 向 Postman 请求代码片段

在 Python 多处理进程中运行较慢的 OpenCV 代码片段

在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途

linux中怎么查看mysql数据库版本