CentOS7 系统简单 Python 环境使用

2023-11-07

1. CentOS7 系统简单 Python 环境使用

1.1. 查看当前系统 Python 版本

  Python在CentOS7.6系统中默认已经安装完成,我们经常使用的 yum 包管理工具就是用Python语言实现的。我们可以使用 python -V 命令查看当前系统python的版本信息:

[root@localhost ~]# python -V
Python 2.7.5
[root@localhost ~]#

  如上图所示,系统中默认使用的python版本为 2.7.5

  要想运行python程序,我们可以使用python的交互解释运行环境,终端中直接输入 python 命令即可:

[root@localhost ~]# python
Python 2.7.5 (default, Apr 11 2018, 07:36:10) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world
>>> 
>>> exit()
[root@localhost ~]#

  上图中进入交互解释运行环境,显示了 python 环境的相关信息。然后尝试执行了第一行代码 print("hello world),成功运行并打印了 hello world 内容;接着我们执行了退出交互环境的函数 exit(),成功退出python交互解释环境。

  除了使用 python 交互解释环境,我们还可以直接编写 python 源代码文件使用 vim 编辑器编写一个简单的 hello.py ,然后使用 python 命令直接运行它,如图:

[root@localhost ~]# cat hello.py 
print("hello world")
[root@localhost ~]# python hello.py 
hello world
[root@localhost ~]# 

1.2. 使用 CentOS7 系统中的 Python3 版本

  默认情况下 CentOS7 系统中自带的 Python 版本是2.7版本。我们当前学习的 Python 版本为 Python3 版本,所以我们可以通过 yum 软件包管理工具安装 python3 版本, 不过由于 CentOS7 的 Base 软件包仓库默认没有 Python3版本 ,因此查找失败,如下图:

[root@localhost ~]# yum list "*python3*"
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: mirrors.163.com
 * updates: mirrors.163.com
Error: No matching Packages to list
[root@localhost ~]# 

  除了 Base 仓库,我们还可以使用 epel 仓库,要想使用 epel 仓库,我们需要先安装一下 epel 仓库,安装过程如下:

[root@localhost ~]# yum install epel-release -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.njupt.edu.cn
 * extras: mirrors.163.com
 * updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch             Version         Repository        Size
================================================================================
Installing:
 epel-release           noarch           7-11            extras            15 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 15 k
Installed size: 24 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/extras/packages/epel-release-7-11.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for epel-release-7-11.noarch.rpm is not installed
epel-release-7-11.noarch.rpm                               |  15 kB   00:00     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-5.1804.el7.centos.x86_64 (@anaconda)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-11.noarch                                     1/1 
  Verifying  : epel-release-7-11.noarch                                     1/1 

Installed:
  epel-release.noarch 0:7-11                                                    

Complete!
[root@localhost ~]# 

  epel 仓库安装完毕后,我们使用 yum 命令再次查询 python3 ,发现有多个不同版本的 python3 软件包。这里我节选了一部分内容,如下图:

root@localhost ~]# yum list "*python3*"
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                     | 7.1 kB     00:00     
 * base: centos.ustc.edu.cn
 * epel: mirrors.yun-idc.com
 * extras: mirrors.163.com
 * updates: centos.ustc.edu.cn
epel                                                     | 4.7 kB     00:00     
(1/3): epel/x86_64/group_gz                                |  88 kB   00:00     
(2/3): epel/x86_64/updateinfo                              | 1.0 MB   00:03     
(3/3): epel/x86_64/primary_db                              | 6.6 MB   00:05     
Available Packages
abrt-addon-python3.noarch                2.1.11-49.el7                      epel
boost-python34.x86_64                    1.53.0-28.el7                      epel
boost-python34-devel.x86_64              1.53.0-28.el7                      epel
python3-urjtag.x86_64                    2017.10-2.el7                      epel
python3-virtualenv-doc.noarch            15.1.0-2.el7                       epel
python34.x86_64                          3.4.9-2.el7                        epel
python34-Cython.x86_64                   0.23.5-1.el7                       epel
python34-PyMySQL.noarch                  0.9.2-1.el7                        epel
python34-PyYAML.x86_64                   3.11-3.el7                         epel
·
·省略部分软件包信息.......
·
python34-aiosmtpd.noarch                 1.0-2.el7                          epel
python34-zope-interface.x86_64           4.3.3-1.el7                        epel
python34-zope-schema.noarch              4.4.1-2.el7                        epel
python34-zope-testing.noarch             4.1.2-2.el7                        epel
python36.x86_64                          3.6.6-2.el7                        epel
python36-PyYAML.x86_64                   3.11-3.el7                         epel
python36-asn1crypto.noarch               0.24.0-6.el7                       epel
python36-blosc.x86_64                    1.2.8-4.el7                        epel
python36-bsddb3.x86_64                   6.2.6-3.el7                        epel
python36-chardet.noarch                  2.3.0-4.el7                        epel
uwsgi-plugin-python34-gevent.x86_64      2.0.17.1-1.el7                     epel
uwsgi-plugin-python34-tornado.x86_64     2.0.17.1-1.el7                     epel
uwsgi-plugin-python36.x86_64             2.0.17.1-1.el7                     epel
uwsgi-plugin-python36-gevent.x86_64      2.0.17.1-1.el7                     epel
[root@localhost ~]# 

   根据上图中的 epel 仓库 python3 软件包信息,我们选择 python36.x86_64 版本进行安装,如图:

[root@localhost ~]# yum install python36 -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.njupt.edu.cn
 * epel: mirrors.yun-idc.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package python36.x86_64 0:3.6.6-2.el7 will be installed
--> Processing Dependency: python36-libs(x86-64) = 3.6.6-2.el7 for package: python36-3.6.6-2.el7.x86_64
--> Processing Dependency: libpython3.6m.so.1.0()(64bit) for package: python36-3.6.6-2.el7.x86_64
--> Running transaction check
---> Package python36-libs.x86_64 0:3.6.6-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package               Arch           Version                Repository    Size
================================================================================
Installing:
 python36              x86_64         3.6.6-2.el7            epel          66 k
Installing for dependencies:
 python36-libs         x86_64         3.6.6-2.el7            epel         8.6 M

Transaction Summary
================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 8.6 M
Installed size: 36 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/epel/packages/python36-3.6.6-2.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Public key for python36-3.6.6-2.el7.x86_64.rpm is not installed
(1/2): python36-3.6.6-2.el7.x86_64.rpm                     |  66 kB   00:01     
(2/2): python36-libs-3.6.6-2.el7.x86_64.rpm                | 8.6 MB   00:29     
--------------------------------------------------------------------------------
Total                                              297 kB/s | 8.6 MB  00:29     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-11.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python36-libs-3.6.6-2.el7.x86_64                             1/2 
  Installing : python36-3.6.6-2.el7.x86_64                                  2/2 
  Verifying  : python36-3.6.6-2.el7.x86_64                                  1/2 
  Verifying  : python36-libs-3.6.6-2.el7.x86_64                             2/2 

Installed:
  python36.x86_64 0:3.6.6-2.el7                                                 

Dependency Installed:
  python36-libs.x86_64 0:3.6.6-2.el7                                            

Complete!
[root@localhost ~]#

   python36 安装成功后,我们可以使用 rpm 命令查看一下 python36 具体包含哪些命令和文件,如图:

root@localhost ~]# rpm -ql python36
/usr/bin/pydoc3.6
/usr/bin/python3.6
/usr/bin/python3.6m
/usr/bin/python36
/usr/bin/pyvenv-3.6
/usr/share/doc/python36-3.6.6
/usr/share/doc/python36-3.6.6/README.rst
/usr/share/licenses/python36-3.6.6
/usr/share/licenses/python36-3.6.6/LICENSE
/usr/share/man/man1/python3.6.1.gz
[root@localhost ~]# 

  与 python2.7 版本一样,python36 依旧可以使用交互解释环境和 python 命令模式运行代码,这里我就不演示交互解释环境,我们直接使用 python36 命令运行之前编写好的 hello.py 程序 如下图:

[root@localhost ~]# python36 -V
Python 3.6.6
[root@localhost ~]# python36 hello.py 
hello world
[root@localhost ~]# 

1.3. CentOS7 系统中 Pycharm 环境使用

  之前演示的 hello.py 程序非常简单,属于演示测试。正常的 Python 项目根据业务需求和功能通常包含众多 python 源文件,再使用传统的 vim 进行编写和管理就不是和方便了。下面给大家在 Linux 环境下安装一款专业的集成开发工具 (IDE) Pycharm 。

  安装 Pycharm 之前,我们现在 CentOS 系统中创建一个专门用来学习 Python 开发的用户,比如,我们创建一个用户叫 python、devops,ai 等,然后再退出 root 用户,使用新建的用户登录系统,如下图:

[root@localhost ~]# passwd python
Changing password for user python.
New password: 
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@localhost ~]# 

  使用刚才创建的 python 用户登录后,打开命令行终端,我们使用 wget 命令下载 pycharm 官网的免费开源版安装包 pycharm-community-2018.3.5.tar.gz ,如下图:

[python@localhost ~]$ wget https://download.jetbrains.com/python/pycharm-community-2018.3.5.tar.gz
--2019-03-25 17:27:00--  https://download.jetbrains.com/python/pycharm-community-2018.3.5.tar.gz
Resolving download.jetbrains.com (download.jetbrains.com)... 54.72.98.183, 52.18.241.155, 2a05:d018:93b:d103:524e:e6ed:6592:1823, ...
Connecting to download.jetbrains.com (download.jetbrains.com)|54.72.98.183|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://download.jetbrains.8686c.com/python/pycharm-community-2018.3.5.tar.gz [following]
--2019-03-25 17:27:02--  https://download.jetbrains.8686c.com/python/pycharm-community-2018.3.5.tar.gz
Resolving download.jetbrains.8686c.com (download.jetbrains.8686c.com)... 60.221.21.254, 2408:876c:0:100::25
Connecting to download.jetbrains.8686c.com (download.jetbrains.8686c.com)|60.221.21.254|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 311914955 (297M) [application/octet-stream]
Saving to: ‘pycharm-community-2018.3.5.tar.gz’

100%[=================================================================================================>] 311,914,955 1.22MB/s   in 7m 53s 

2019-03-25 17:34:55 (644 KB/s) - ‘pycharm-community-2018.3.5.tar.gz’ saved [311914955/311914955]

[python@localhost ~]$
[python@localhost ~]$ ll
total 304644
drwxr-xr-x. 2 python python      4096 Mar 25 17:02 Desktop
drwxr-xr-x. 2 python python      4096 Mar 25 17:02 Documents
drwxr-xr-x. 2 python python      4096 Mar 25 17:02 Downloads
drwxr-xr-x. 2 python python      4096 Mar 25 17:02 Music
drwxr-xr-x. 2 python python      4096 Mar 25 17:02 Pictures
drwxr-xr-x. 2 python python      4096 Mar 25 17:02 Public
-rw-rw-r--. 1 python python 311914955 Feb 27 19:15 pycharm-community-2018.3.5.tar.gz
drwxr-xr-x. 2 python python      4096 Mar 25 17:02 Templates
drwxr-xr-x. 2 python python      4096 Mar 25 17:02 Videos
[python@localhost ~]$

  上图中,下载软件包过程比较慢,建议提前下载好对应的软件包,方便后续安装操作。安装过程比较简单,直接解压开就可以使用:

[python@localhost ~]$ mkdir software
[python@localhost ~]$ cd software/
[python@localhost software]$ cd ..
[python@localhost ~]$ tar xf pycharm-community-2018.3.5.tar.gz -C software/
[python@localhost ~]$ cd software
[python@localhost software]$ mv pycharm-community-2018.3.5 pycharm180305
[python@localhost software]$ cd pycharm180305/
[python@localhost pycharm180305]$ cd bin
[python@localhost bin]$ ls
format.sh     fsnotifier-arm   libdbm64.so  pycharm64.vmoptions  pycharm.svg
fsnotifier    idea.properties  log.xml      pycharm.png          pycharm.vmoptions
fsnotifier64  inspect.sh       printenv.py  pycharm.sh           restart.py
[python@localhost bin]$ ./pycharm.sh 
Mar 25, 2019 5:45:20 PM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
Mar 25, 2019 5:45:20 PM java.util.prefs.FileSystemPreferences$6 run
WARNING: Prefs file removed in background /home/python/.java/.userPrefs/prefs.xml
[python@localhost bin]$ 

  上图中直接可以在 pycharm 的bin目录下运行 pycharm.sh 但是当前的终端被占用了。
  下面我们设置一个 pycharm 的桌面快捷方程式,避免在终端中使用命令开启,具体步骤如下图:

[python@localhost ~]$ cd Desktop/
[python@localhost Desktop]$ touch pycharm.desktop 
[python@localhost Desktop]$ vi pycharm.desktop 
[python@localhost Desktop]$ cat pycharm.desktop 
[Desktop Entry]
Name=pycharm
Exec=/home/python/software/pycharm180305/bin/pycharm.sh
Type=Application
Icon=/home/python/software/pycharm180305/bin/pycharm.png
Terminal=false
[python@localhost Desktop]$ 

1.4. Pycharm 中安装 Jupyter

  我们通过 CentOS7 系统桌面中创建的 pycharm 快捷方程式打开 pycharm ,创建一个 test 项目然后点击 pycharm中的控制台,进行 pip 的跟新和 jupyter 的安装,如图:

(venv) [python@localhost test]$ pip install --upgrade pip
(venv) [python@localhost test]$ pip install jupyter
(venv) [python@localhost test]$ jupyter notebook --ip=0.0.0.0 --port=8000
[I 18:25:00.814 NotebookApp] Serving notebooks from local directory: /home/python/PycharmProjects/test
[I 18:25:00.815 NotebookApp] The Jupyter Notebook is running at:
[I 18:25:00.815 NotebookApp] http://(localhost.localdomain or 127.0.0.1):8000/?token=f7d20e19f9abf2cd8323034c6a60e6bf0a3c30a98f866fbc
[I 18:25:00.815 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 18:25:00.833 NotebookApp] 
    
    To access the notebook, open this file in a browser:
        file:///run/user/1001/jupyter/nbserver-8326-open.html
    Or copy and paste one of these URLs:
        http://(localhost.localdomain or 127.0.0.1):8000/?token=f7d20e19f9abf2cd8323034c6a60e6bf0a3c30a98f866fbc
^C[I 18:25:14.249 NotebookApp] interrupted
Serving notebooks from local directory: /home/python/PycharmProjects/test
0 active kernels
The Jupyter Notebook is running at:
http://(localhost.localdomain or 127.0.0.1):8000/?token=f7d20e19f9abf2cd8323034c6a60e6bf0a3c30a98f866fbc
Shutdown this notebook server (y/[n])? y
[C 18:25:16.200 NotebookApp] Shutdown confirmed
[I 18:25:16.210 NotebookApp] Shutting down 0 kernels
(venv) [python@localhost test]$
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CentOS7 系统简单 Python 环境使用 的相关文章

随机推荐

  • 非常好用的在线架构图网页

    processon https www processon com
  • odoo 根据权限规则隐藏编辑按钮

    业务分析 正常情况下odoo的编辑按钮是不会隐藏的 如果有权限限制会提示权限访问规则错误 也就是产生一个userError 这已经在用户毫不知情的情况下让用户实现了一系列无用的操作 这对用户很不友好 用户只有点击编辑 完成所编辑的内容之后再
  • 免费股票行情软件

    飞狐 分析家 指南针 通达信 大智慧 双子星 钱龙 胜龙 海融 排名有先后哦 下面是MACD中股神MM对两个垃圾的垃圾行为的总结性分析 相信对你有帮助哈 gt 利多方舟 操盘手的十大败笔 大家进来讨论一下 主题就是公式 指标 输了钱怨软件
  • 剑指offer_第8题_跳台阶

    题目描述 一只青蛙一次可以跳上1级台阶 也可以跳上2级 求该青蛙跳上一个n级的台阶总共有多少种跳法 先后次序不同算不同的结果 理解 完全蒙啊 那我们就用特例先分析一下 如果有1级台阶 那有1种 如果有2级台阶 那有2种 如果有3级台阶 那一
  • 微信小程序API

    开发者文档永远滴神 本文会随实际使用不断丰富 毕竟逐一介绍是文档的事情 作为开发人员 用到再看才是常态女 wx getSystemInfoSync 获取用户信息 返回一个携带用户各种信息的对象 onReady function consol
  • 【转】DSP 缓存机制

    本文主要以DSP讲解cache原理 但原理与CPU是相通的 故转载 原文地址 https blog csdn net qq 39376747 article details 112794096 文章目录 DSP 缓存机制 1 Cache基础
  • 创建一个数组, 实现函数init()初始化数组、 实现empty()清空数组、 实现reverse()函数完成数组元素的逆置。 要求:自己设计函数的参数,返回值

    创建一个数组 实现函数init 初始化数组 实现empty 清空数组 实现reverse 函数完成数组元素的逆置 要求 自己设计函数的参数 返回值 include
  • IDEA构建Android项目失败

    一同步项目就报 This version of the Android Support plugin for IntelliJ IDEA or Android Studio cannot open this project please r
  • HTTP 网关

    本文摘自书籍 HTTP 权威指南 此系列文章对应 github地址 网关 HTTP 扩展和接口的发展是由用户需求驱动的 要在 Web 上发布更复杂资源的需求出现时 人们很快就明确了一点 单个应用程序无法处理所有这些能想到的资源 为了解决这个
  • 智能人像处理-ON1 Portrait AI 2021.1 v15.1.0工具

    介绍 ON1 Portrait AI是一款人像AI智能处理软件 可以根据自身喜好对图像进行修复 重点是对人脸的一些修饰项目 虽然没有PS功能齐全 但对于人脸处理方面来说要更加细腻方便 可以一键优化人脸效果 只需使用ON1 Portrait
  • XXX iPhone has denied the launch request

    在Xcode运行 应用的时候 出现 iPhone has denied the launch request 这个问题 目前我遇到的原因是 Signing 需要重新配置一下 重新选一下Automatically manage signing
  • 不会服务治理,还怎么搞微服务?

    目录 单体架构 微服务架构 服务治理之注册与发现和负载均衡 服务治理之限流熔断 服务治理之服务监控 今天给大家分享一个话题 是关于微服务架构的服务治理的 很多小伙伴可能都觉得自己玩儿过微服务架构 然后可能也听说过服务治理 但是服务治理到底是
  • 大数据是什么意思?

    一 大数据的概念 大数据是指无法在一定时间内用常规软件工具对其内容进行抓取 管理和处理的数据集合 大数据技术 是指从各种各样类型的数据中 快速获得有价值信息的能力 适用于大数据的技术 包括大规模并行处理 MPP 数据库 数据挖掘电网 分布式
  • stm32中使用cJSON

    STM32中使用cJSON cJSON 下载地址 https github com DaveGamble cJSON 将其拉取到本地是有很多文件 但只有两个比较重要 cJSON c cJSON h 我们将其添加到自己工程目录下 其中 在进行
  • 【华为面试题】深度优先搜索(一)

    题目 Jungle居住在蓝鲸城 一个拥有规则街道的城市 然而 街道每天的封闭情况都是不同的 为了测试Jungle的导航技巧 我们设置了以下挑战 Jungle必须从他的家 表示为 S 出发 前往公司 表示为 T 街道图由以下元素构成 代表可走
  • 华三交换机端口镜像抓包实战

    目录 1 端口镜像的使用场景 2 华三交换机配置端口镜像 web 命令行 3 wireshark分析配置端口镜像前后抓包的数据区别 1 端口镜像的使用场景 端口镜像 Mirror Port 功能通过在交换机或路由器上 将一个或多个源端口的数
  • Qt学习笔记(QFile)

    文件操作 基础课以文件操作结尾 QFile 无非就是读和写操作 QFile file 路径 file open 打开方式 QIODevice ReadOnly file readAll readLine file atEnd 判断是否到文件
  • C++实现 快速排序

    目录 一 快速排序主函数 代码如下 二 分区函数 1 选取支点 2 定义左右指针 移动指针 3 返回分割点的位置 代码如下 三 swap函数 元素互换 代码如下 四 printArr函数 打印输出 代码如下 完整代码如下 测试方法如下 运行
  • 小米VS华为:水军?黑稿?到底是谁黑了谁?

    那边罗永浩和黄章互相吐槽还没结束 雷军又向华为开炮 雷军发微博称 其被华为水军黑了 事情的起因其实很简单 一位微博名为 IT华少 的网友称 小米手机4的芯片没有进行点胶处理 所以认定其 做工粗糙 不如华为的荣耀6 雷军在看到华强电子产业研究
  • CentOS7 系统简单 Python 环境使用

    文章目录 1 CentOS7 系统简单 Python 环境使用 1 1 查看当前系统 Python 版本 1 2 使用 CentOS7 系统中的 Python3 版本 1 3 CentOS7 系统中 Pycharm 环境使用 1 4 Pyc