zoukankan      html  css  js  c++  java
  • linux中python安装

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

    [zyj@localhost ~]$  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/

    [root@localhost 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.
    [root@localhost home]# 
    [root@localhost 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的家目录下
    [root@localhost home]# cd zyj
    [root@localhost 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
    [root@localhost zyj]# mv Python-3.6.5.tgz ../
    [root@localhost 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)
    [root@localhost home]# rpm -qa | grep gcc
    libgcc-4.4.7-18.el6.x86_64
    gcc-4.4.7-18.el6.x86_64
    [root@localhost home]# yum install gcc-c++ //安装c++编译环境
    • 检查(依赖、兼容),预编译:
    [root@localhost home]# tar -zxvf Python-3.6.5.tgz //解压安装包
    [root@localhost home]# ls
    1.txt  Python-3.6.5  Python-3.6.5.tgz  hello.txt  x  zyj
    [root@localhost home]# cd Python-3.6.5 //进入安装包目录下
    [root@localhost 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

    执行可执行文件:
    [root@localhost 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/>.
    
    [root@localhost 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需要依赖这两个包。

    [root@localhost home]# yum install zlib* openssl*  //安装依赖包
    [root@localhost home]# cd Python-3.6.5
    [root@localhost 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
    [root@localhost Python-3.6.5]# ./configure --prefix=/usr/python-3.6.5 --enable-optimizations  //完整的预编译
    ...
    creating Modules/Setup
    creating Modules/Setup.local
    creating Makefile  //没有任何报错
    [root@localhost Python-3.6.5]# 
    • 编译:make
    [root@localhost Python-3.6.5]# make
    ...
    make[1]: Entering directory `/home/Python-3.6.5'
    find . -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'
    [root@localhost Python-3.6.5]# //一般检查最后几行有没有error
    • 安装:make install
    [root@localhost 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
    [root@localhost Python-3.6.5]#
    
    [root@localhost Python-3.6.5]# cd /usr/python-3.6.5/
    [root@localhost python-3.6.5]# ls
    bin include lib share
    [root@localhost python-3.6.5]# pwd  //python安装后的目录
    /usr/python-3.6.5
    
    [root@localhost python-3.6.5]# cd /home/Python-3.6.5
    [root@localhost Python-3.6.5]# pwd  //python源码目录,可以删除掉
    /home/Python-3.6.5
    
    [root@localhost Python-3.6.5]# cd /usr/python-3.6.5/
    [root@localhost python-3.6.5]# ls
    bin include lib share
    [root@localhost python-3.6.5]# ll
    total 16
    drwxr-xr-x. 2 root root 4096 May 1 01:09 bin
    drwxr-xr-x. 3 root root 4096 May 1 01:09 include
    drwxr-xr-x. 4 root root 4096 May 1 01:09 lib
    drwxr-xr-x. 3 root root 4096 May 1 01:09 share
    [root@localhost python-3.6.5]# cd bin
    [root@localhost bin]# ll
    total 17440
    lrwxrwxrwx. 1 root root 8 May 1 01:09 2to3 -> 2to3-3.6
    -rwxr-xr-x. 1 root root 108 May 1 01:09 2to3-3.6
    -rwxr-xr-x. 1 root root 249 May 1 01:09 easy_install-3.6
    lrwxrwxrwx. 1 root root 7 May 1 01:09 idle3 -> idle3.6
    -rwxr-xr-x. 1 root root 106 May 1 01:09 idle3.6
    -rwxr-xr-x. 1 root root 221 May 1 01:09 pip3
    -rwxr-xr-x. 1 root root 221 May 1 01:09 pip3.6
    lrwxrwxrwx. 1 root root 8 May 1 01:09 pydoc3 -> pydoc3.6
    -rwxr-xr-x. 1 root root 91 May 1 01:09 pydoc3.6
    lrwxrwxrwx. 1 root root 9 May 1 01:09 python3 -> python3.6
    lrwxrwxrwx. 1 root root 16 May 1 01:09 python3-config -> python3.6-config
    -rwxr-xr-x. 2 root root 8912198 May 1 01:06 python3.6
    lrwxrwxrwx. 1 root root 17 May 1 01:09 python3.6-config -> python3.6m-config
    -rwxr-xr-x. 2 root root 8912198 May 1 01:06 python3.6m
    -rwxr-xr-x. 1 root root 3110 May 1 01:09 python3.6m-config
    lrwxrwxrwx. 1 root root 10 May 1 01:09 pyvenv -> pyvenv-3.6
    -rwxr-xr-x. 1 root root 448 May 1 01:09 pyvenv-3.6
    
    [root@localhost bin]# python
    Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> exit
    Use exit() or Ctrl-D (i.e. EOF) to exit
    >>> exit()
    [root@localhost bin]# 
    [root@localhost bin]# python3  //需要设置环境变量,如果命令找不到,则此命令的执行文件不在系统的环境变量(PATH)中。
    bash: python3: command not found
    • 配置环境变量:让系统自动找到命令执行文件路径。PATH的值是一堆目录,之间通过冒号隔开。
    [root@localhost bin]# printenv //查看环境变量或使用echo $PATH查看
    HOSTNAME=localhost.localdomain
    SELINUX_ROLE_REQUESTED=
    SHELL=/bin/bash
    TERM=xterm
    HISTSIZE=1000
    SSH_CLIENT=192.168.166.1 50260 22
    SELINUX_USE_CURRENT_RANGE=
    SSH_TTY=/dev/pts/0
    USER=zyj
    PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/zyj/bin  目录之间的分隔使用冒号隔开。
    MAIL=/var/spool/mail/zyj
    PWD=/usr/python-3.6.5/bin
    LANG=en_US:.UTF-8
    SELINUX_LEVEL_REQUESTED=
    HISTCONTROL=ignoredups
    SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
    HOME=/root
    SHLVL=2
    LOGNAME=zyj
    SSH_CONNECTION=192.168.166.1 50260 192.168.166.128 22
    LESSOPEN=||/usr/bin/lesspipe.sh %s
    G_BROKEN_FILENAMES=1
    OLDPWD=/usr/python-3.6.5
    _=/usr/bin/printenv
    [root@localhost bin]# 

     设置环境变量:

    [root@localhost bin]# vi ~/.bash_profile
    [root@localhost bin]# vi ~/.bashrc  
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
            . /etc/bashrc
    fi
    PATH=$PATH:/usr/python-3.6.5/bin   //在原有PATH下追加新的路径。
    #第二种修改环境变量的方式
    PYTHON_HOME=/usr/python-3.6.5
    PATH=$PATH:$PYTHON_HOME/bin
    :wq
    [root@localhost bin]# source ~/.bashrc  //执行生效,只要PATH修改,均需要source生效。
    [root@localhost ~]# echo $PATH
    /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/zyj/bin:/usr/python-3.6.5/bin
    
    [root@localhost bin]# python3
    Python 3.6.5 (default, May  1 2018, 00:59:27) 
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 

     注意:一般遵守改变自己用户的配置文件,不要改整个系统的配置文件。

     /etc/profile :整个系统的环境变量配置文件

    ~/.bashrc:当前用户的环境变量配置文件,推荐使用。

    补充:安装一个python工具,ipython3,这个工具可以在python中执行linux的命令,比较好用,推荐安装。

    [root@localhost bin]# pip3 install ipython3  
    Collecting ipython
    [root@localhost bin]# ipython3
    /usr/python-3.6.5/lib/python3.6/site-packages/IPython/core/history.py:226: UserWarning: IPython History requires SQLite, your history will not be saved
    warn("IPython History requires SQLite, your history will not be saved")
    Python 3.6.5 (default, May 1 2018, 00:59:27) 
    Type 'copyright', 'credits' or 'license' for more information
    IPython 6.3.1 -- An enhanced Interactive Python. Type '?' for help.
    
    In [1]: ls
    2to3@ idle3@ iptest3* pip3* pydoc3.6* python3-config@ python3.6m* pyvenv-3.6*
    2to3-3.6* idle3.6* ipython* pip3.6* pygmentize* python3.6* python3.6m-config*
    easy_install-3.6* iptest* ipython3* pydoc3@ python3@ python3.6-config@ pyvenv@
    In [2]: cd /home 
  • 相关阅读:
    微型三维图像传感器采用飞行时间技术
    利用反射计芯片进行非接触式液位测量
    嵌入式传感器是智能移动增长的关键
    传感器融合带来多重好处
    参与机器人技术开发
    管理多供应商物联网项目的10个关键步骤
    基于ARMv8的固件系统体系结构
    嵌入式Linux设备驱动程序:在运行时读取驱动程序状态
    Elasticsearch之配置详解
    Spring Data Elasticsearch 和 x-pack 用户名/密码验证连接
  • 原文地址:https://www.cnblogs.com/zhaoyujiao/p/8975669.html
Copyright © 2011-2022 走看看