02 Python学习之CentOS 7.6 yum安装Python3.6并配置虚拟环境&Pycharm远程连接Linux服务器

02 CentOS 7.6 yum安装Python3.6并配置虚拟环境

  • 实验环境
  • 实验步骤
    • 第一步:关闭SELinux和防火墙
    • 第二步:安装Python3.6
    • 第三步:配置pip国内源
    • 第四步:安装虚拟环境
    • 第五步:修改virtualenvwrapper.sh配置文件
    • 第六步:创建虚拟环境
    • 第七步:进入虚拟环境
    • 第八步:退出虚拟环境
    • 删除虚拟环境
    • PyCharm连接阿里云服务器
      • 第一步:
      • 第二步:
      • 第三步:
      • 第四步:
      • 第五步:
      • 第六步:

实验环境

实验名:01 CentOS7.6搭建Python开发环境
Linux版本: CentOS 7.6
Python版本: Python 3.6.8

实验步骤

第一步:关闭SELinux和防火墙

[root@localhost ~]# vim /etc/selinux/config
SELINUX=enforcing改成SELINUX=disabled

[root@localhost ~]# systemctl disable firewalld.service
执行结果如下:

Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

第二步:安装Python3.6

[root@localhost ~]# yum -y install python3
执行结果如下:

Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package python3.x86_64 0:3.6.8-13.el7 will be installed
--> Processing Dependency: python3-libs(x86-64) = 3.6.8-13.el7 for package: python3-3.6.8-13.el7.x86_64
--> Processing Dependency: python3-setuptools for package: python3-3.6.8-13.el7.x86_64
--> Processing Dependency: python3-pip for package: python3-3.6.8-13.el7.x86_64
--> Processing Dependency: libpython3.6m.so.1.0()(64bit) for package: python3-3.6.8-13.el7.x86_64
--> Running transaction check
---> Package python3-libs.x86_64 0:3.6.8-13.el7 will be installed
---> Package python3-pip.noarch 0:9.0.3-7.el7_7 will be installed
---> Package python3-setuptools.noarch 0:39.2.0-10.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================================================
 Package                                        Arch                               Version                                   Repository                           Size
=======================================================================================================================================================================
Installing:
 python3                                        x86_64                             3.6.8-13.el7                              base                                 69 k
Installing for dependencies:
 python3-libs                                   x86_64                             3.6.8-13.el7                              base                                7.0 M
 python3-pip                                    noarch                             9.0.3-7.el7_7                             updates                             1.8 M
 python3-setuptools                             noarch                             39.2.0-10.el7                             base                                629 k

Transaction Summary
=======================================================================================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 9.4 M
Installed size: 48 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/python3-3.6.8-13.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for python3-3.6.8-13.el7.x86_64.rpm is not installed
(1/4): python3-3.6.8-13.el7.x86_64.rpm                                                                                                          |  69 kB  00:00:00
(2/4): python3-setuptools-39.2.0-10.el7.noarch.rpm                                                                                              | 629 kB  00:00:00
Public key for python3-pip-9.0.3-7.el7_7.noarch.rpm is not installed 41% [========================                                   ] 4.4 MB/s | 3.9 MB  00:00:01 ETA
(3/4): python3-pip-9.0.3-7.el7_7.noarch.rpm                                                                                                     | 1.8 MB  00:00:00
(4/4): python3-libs-3.6.8-13.el7.x86_64.rpm                                                                                                     | 7.0 MB  00:00:01
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                  7.7 MB/s | 9.4 MB  00:00:01
Retrieving key from http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) "
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 From       : http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python3-setuptools-39.2.0-10.el7.noarch                                                                                                             1/4
  Installing : python3-libs-3.6.8-13.el7.x86_64                                                                                                                    2/4
  Installing : python3-3.6.8-13.el7.x86_64                                                                                                                         3/4
  Installing : python3-pip-9.0.3-7.el7_7.noarch                                                                                                                    4/4
  Verifying  : python3-pip-9.0.3-7.el7_7.noarch                                                                                                                    1/4
  Verifying  : python3-3.6.8-13.el7.x86_64                                                                                                                         2/4
  Verifying  : python3-setuptools-39.2.0-10.el7.noarch                                                                                                             3/4
  Verifying  : python3-libs-3.6.8-13.el7.x86_64                                                                                                                    4/4

Installed:
  python3.x86_64 0:3.6.8-13.el7

Dependency Installed:
  python3-libs.x86_64 0:3.6.8-13.el7                   python3-pip.noarch 0:9.0.3-7.el7_7                   python3-setuptools.noarch 0:39.2.0-10.el7

Complete!

第三步:配置pip国内源

先创建一个隐藏的pip文件夹,然后创建配置文件pip.conf
[root@localhost ~]# mkdir /root/.pip
[root@localhost ~]# vim /root/.pip/pip.conf
在pip.conf中添加如下内容(豆瓣的或阿里云的都可以二选一即可):
豆瓣的源

[global]
index-url=https://pypi.douban.com/simple/
[install]
trusted-host=pypi.douban.com

阿里云的源

[global]
index-url=http://mirrors.cloud.aliyuncs.com/pypi/simple/
[install]
trusted-host=mirrors.cloud.aliyuncs.com

第四步:安装虚拟环境

创建虚拟环境目录
[root@localhost ~]# mkdir /root/.virtualenvs

安装virtualenv和virtualenvwrapper,由于CentOS7.6自带Python2,直接使用pip可能指向的是python2,所以我们需要使用pip3进行安装
[root@localhost ~]# pip3 install virtualenv
结果如下:

WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting virtualenv
  Downloading https://pypi.doubanio.com/packages/f0/55/f0de23cb61f2e90db3f8362f0e685a0bc04d0c4cb159b7a96cb7c4817d63/virtualenv-20.0.23-py2.py3-none-any.whl (4.7MB)
    100% |████████████████████████████████| 4.7MB 158kB/s
Collecting importlib-resources>=1.0; python_version < "3.7" (from virtualenv)
  Downloading https://pypi.doubanio.com/packages/b6/03/1865fdd49ec9a938f9f84b255d3d37863df9fbd18b48c1c3f761040cbf13/importlib_resources-2.0.0-py2.py3-none-any.whl
Collecting distlib<1,>=0.3.0 (from virtualenv)
  Downloading https://pypi.doubanio.com/packages/7d/29/694a3a4d7c0e1aef76092e9167fbe372e0f7da055f5dcf4e1313ec21d96a/distlib-0.3.0.zip (571kB)
    100% |████████████████████████████████| 573kB 1.3MB/s
Collecting six<2,>=1.9.0 (from virtualenv)
  Downloading https://pypi.doubanio.com/packages/ee/ff/48bde5c0f013094d729fe4b0316ba2a24774b3ff1c52d924a8a4cb04078a/six-1.15.0-py2.py3-none-any.whl
Collecting appdirs<2,>=1.4.3 (from virtualenv)
  Downloading https://pypi.doubanio.com/packages/3b/00/2344469e2084fb287c2e0b57b72910309874c3245463acd6cf5e3db69324/appdirs-1.4.4-py2.py3-none-any.whl
Collecting importlib-metadata<2,>=0.12; python_version < "3.8" (from virtualenv)
  Downloading https://pypi.doubanio.com/packages/98/13/a1d703ec396ade42c1d33df0e1cb691a28b7c08b336a5683912c87e04cd7/importlib_metadata-1.6.1-py2.py3-none-any.whl
Collecting filelock<4,>=3.0.0 (from virtualenv)
  Downloading https://pypi.doubanio.com/packages/93/83/71a2ee6158bb9f39a90c0dea1637f81d5eef866e188e1971a1b1ab01a35a/filelock-3.0.12-py3-none-any.whl
Collecting zipp>=0.4; python_version < "3.8" (from importlib-resources>=1.0; python_version < "3.7"->virtualenv)
  Downloading https://pypi.doubanio.com/packages/b2/34/bfcb43cc0ba81f527bc4f40ef41ba2ff4080e047acb0586b56b3d017ace4/zipp-3.1.0-py3-none-any.whl
Installing collected packages: zipp, importlib-metadata, importlib-resources, distlib, six, appdirs, filelock, virtualenv
  Running setup.py install for distlib ... done
Successfully installed appdirs-1.4.4 distlib-0.3.0 filelock-3.0.12 importlib-metadata-1.6.1 importlib-resources-2.0.0 six-1.15.0 virtualenv-20.0.23 zipp-3.1.0

[root@localhost ~]# pip3 install virtualenvwrapper
结果如下:

WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting virtualenvwrapper
  Downloading https://pypi.doubanio.com/packages/c1/6b/2f05d73b2d2f2410b48b90d3783a0034c26afa534a4a95ad5f1178d61191/virtualenvwrapper-4.8.4.tar.gz (334kB)
    100% |████████████████████████████████| 337kB 1.9MB/s
Requirement already satisfied: virtualenv in /usr/local/lib/python3.6/site-packages (from virtualenvwrapper)
Collecting virtualenv-clone (from virtualenvwrapper)
  Downloading https://pypi.doubanio.com/packages/83/b8/cd931487d250565392c39409117436d910232c8a3ac09ea2fb62a6c47bff/virtualenv_clone-0.5.4-py2.py3-none-any.whl
Collecting stevedore (from virtualenvwrapper)
  Downloading https://pypi.doubanio.com/packages/11/75/154f7b0bc00a580db2ccac141400dc601f7f2a1bf45bd56515edbda34850/stevedore-2.0.0-py3-none-any.whl (42kB)
    100% |████████████████████████████████| 51kB 7.7MB/s
Requirement already satisfied: six<2,>=1.9.0 in /usr/local/lib/python3.6/site-packages (from virtualenv->virtualenvwrapper)
Requirement already satisfied: filelock<4,>=3.0.0 in /usr/local/lib/python3.6/site-packages (from virtualenv->virtualenvwrapper)
Requirement already satisfied: importlib-metadata<2,>=0.12; python_version < "3.8" in /usr/local/lib/python3.6/site-packages (from virtualenv->virtualenvwrapper)
Requirement already satisfied: distlib<1,>=0.3.0 in /usr/local/lib/python3.6/site-packages (from virtualenv->virtualenvwrapper)
Requirement already satisfied: appdirs<2,>=1.4.3 in /usr/local/lib/python3.6/site-packages (from virtualenv->virtualenvwrapper)
Requirement already satisfied: importlib-resources>=1.0; python_version < "3.7" in /usr/local/lib/python3.6/site-packages (from virtualenv->virtualenvwrapper)
Collecting pbr!=2.1.0,>=2.0.0 (from stevedore->virtualenvwrapper)
  Downloading https://pypi.doubanio.com/packages/96/ba/aa953a11ec014b23df057ecdbc922fdb40ca8463466b1193f3367d2711a6/pbr-5.4.5-py2.py3-none-any.whl (110kB)
    100% |████████████████████████████████| 112kB 3.9MB/s
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.6/site-packages (from importlib-metadata<2,>=0.12; python_version < "3.8"->virtualenv->virtualenvwrapper)
Installing collected packages: virtualenv-clone, pbr, stevedore, virtualenvwrapper
  Running setup.py install for virtualenvwrapper ... done
Successfully installed pbr-5.4.5 stevedore-2.0.0 virtualenv-clone-0.5.4 virtualenvwrapper-4.8.4

第五步:修改virtualenvwrapper.sh配置文件

virtualenvwrapper.sh配置文件的位置是/usr/local/bin/virtualenvwrapper.sh
修改的原因是默认是Python,而系统的Python指向的是python2,不是python3
将virtualenvwrapper.sh文件的第50行的python改为python3
如下所示:
[root@localhost ~]# vim /usr/local/bin/virtualenvwrapper.sh

将:		VIRTUALENVWRAPPER_PYTHON="$(command \which python)
改为:	VIRTUALENVWRAPPER_PYTHON="$(command \which python3)

将虚拟环境加入到环境变量中
[root@localhost ~]# vim /etc/bashrc +
添加代码如下:

export WORKON_HOME=$HOME/.virtualenvs

注意:修改完成后要刷新配置文件
[root@localhost ~]# source /etc/bashrc
[root@localhost ~]# source /usr/local/bin/virtualenvwrapper.sh
结果如下:

virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /root/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/get_env_details

第六步:创建虚拟环境

[root@localhost ~]# mkvirtualenv study
结果如下:

created virtual environment CPython3.6.8.final.0-64 in 610ms
  creator CPython3Posix(dest=/root/.virtualenvs/study, clear=False, global=False)
  seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed-app-data/v1.0.1)
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
virtualenvwrapper.user_scripts creating /root/.virtualenvs/study/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/study/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/study/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/study/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/study/bin/get_env_details

第七步:进入虚拟环境

workon 可以列出全部的虚拟环境,如果要进入某个虚拟环境直接使用:workon + 虚拟环境名
[root@localhost ~]# workon study
结果如下:

(study) [root@localhost ~]#

在命令行前面多了一个study,说明我们在study这个虚拟环境中

第八步:退出虚拟环境

直接输入:deactivate就可以退出了
(study) [root@localhost ~]# deactivate
结果如下:

[root@localhost ~]#

删除虚拟环境

[root@localhost ~]# rmvirtualenv study
结果如下:

Removing study...

如果使用workon就不会有虚拟环境列出了

PyCharm连接阿里云服务器

第一步:

点击File —— Project:xxxx(xxx是你的项目名) —— Project Interpreter 选择 Add

02 Python学习之CentOS 7.6 yum安装Python3.6并配置虚拟环境&Pycharm远程连接Linux服务器_第1张图片

选择SSH Interpreter ,输入IP和用户名,点击下一步 输入密码,再点击下一步

02 Python学习之CentOS 7.6 yum安装Python3.6并配置虚拟环境&Pycharm远程连接Linux服务器_第2张图片
02 Python学习之CentOS 7.6 yum安装Python3.6并配置虚拟环境&Pycharm远程连接Linux服务器_第3张图片

第二步:

设置解释器路径和文件映射关系,设置完毕后点击finish
02 Python学习之CentOS 7.6 yum安装Python3.6并配置虚拟环境&Pycharm远程连接Linux服务器_第4张图片
我们可以改个名字,方便选择
02 Python学习之CentOS 7.6 yum安装Python3.6并配置虚拟环境&Pycharm远程连接Linux服务器_第5张图片

第三步:

定位到 Build, Execution, Deployment —— Deployment —— Connection

  1. 选择解释器,点击上方的对勾 ’ Use as Default’, 设置为默认(为什么这么做,因为不是默认解释器的话,即使下面一步设置了自动上传也不会将代码上传到远程服务器, 这一步很重要 ,设置完成后,字体会变粗,下图中是未设置状态,字体是细体)
  2. 确认Root path 路径,是否为刚才选择的映射关系路径
    02 Python学习之CentOS 7.6 yum安装Python3.6并配置虚拟环境&Pycharm远程连接Linux服务器_第6张图片
    再定位到 Mappings,将Deployment path 和 Web path 设置为根目录
    02 Python学习之CentOS 7.6 yum安装Python3.6并配置虚拟环境&Pycharm远程连接Linux服务器_第7张图片

第四步:

选择 OptionsUpload Changed files automatically to default server 的值改为Ctrl + s
这样我们在按下Ctrl + s 保存的时候,机会上传到远程服务器,默认是 Always,建议修改,不然比较占用资源
02 Python学习之CentOS 7.6 yum安装Python3.6并配置虚拟环境&Pycharm远程连接Linux服务器_第8张图片

第五步:

一直确定下去,然后回到PyCharm,等待一会,因为PyCharm要进行一些配置,几分钟就好
然后创建一个文件 testfile.py ,登录远程服务器就可以看到文件已经上传上去了,如下图:

02 Python学习之CentOS 7.6 yum安装Python3.6并配置虚拟环境&Pycharm远程连接Linux服务器_第9张图片
File Transfer 是文件的上传下载记录

第六步:

执行一下代码,我们可以看到,用的就是远程服务器上的解释器,如下图所示

02 Python学习之CentOS 7.6 yum安装Python3.6并配置虚拟环境&Pycharm远程连接Linux服务器_第10张图片
在pycharm的右侧也有一个 Remote Host选项卡,这里展示的就是远程服务器上的内容,如果本地代码删除了,可以从这里进行下载
02 Python学习之CentOS 7.6 yum安装Python3.6并配置虚拟环境&Pycharm远程连接Linux服务器_第11张图片

你可能感兴趣的:(102,Python,学习笔记)