【实战学习(二)】开源项目学习之准备工作

目录


1)阅读README

*遇到的问题之图片显示不出来

  • 原因:

    出现这种情况基本是因为DNS被污染/ 官方更新了DNS,但DNS缓存未被更新,导致错误解析/ host设置错误。

  • 解决方法:

    1、用www.ipaddress.com去查询raw.githubusercontent.com的IP地址(raw.githubusercontent.com站点存放github图片素材),注意:raw.GitHub的ip经常更换。

    2、修改hosts:

    • windows的方式可见本栏《【实战学习(零)】github与开源项目》
    • Ubuntu,CentOS及macOS直接在终端输入:sudo vi /etc/hosts

    3、刷新DNS

1.1 技术栈

1.1.1 何为技术栈?

一般来说是指将N种技术互相组合在一起(N>1),作为一个有机的整体来实现某种目的或功能。也可以指掌握这些技术以及配合使用的经验。

以本项目为例:

  • 语言:Python3.* (Django)
  • 数据库:MySql、 redis
  • 任务队列(异步处理):celery(django-celery)
  • 分布式文件存储:FastDFS
  • 搜索引擎(商品检索):haystack(django-haystack)、whoosh、二次开发
  • web服务器配置:Nginx+ uwsgi
  • 开发环境:PyCharm、Linux、vim

1.1.2 扩展之Django

Python下有许多款不同的 Web 框架,Django是重量级选手中最有代表性的一位,许多成功的网站和APP都基于Django。

Django 本身基于 MVC 模型,即 Model(模型)+ View(视图)+ Controller(控制器)设计模式,MVC 模式使后续对程序的修改和扩展简化,并且使程序某一部分的重复利用成为可能。

Python 加 Django 是快速开发、设计、部署网站的最佳组合。

  • 特点:
    • 强大的数据库功能
    • 自带强大的后台功能
    • 优雅的网址

具体的介绍可见本栏《【实战学习(三)】开源项目学习之Django框架简介以及启动》

1.1.3 扩展之redis

REmote DIctionary Server(Redis) 是一个由 Salvatore Sanfilippo 写的 key-value 存储系统,是跨平台的非关系型数据库。

Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库,并提供多种语言的 API。Redis 通常被称为数据结构服务器,因为值(value)可以是字符串(String)、哈希(Hash)、列表(list)、集合(sets)和有序集合(sorted sets)等类型。

  • Redis 与其他 key - value 缓存产品有以下三个特点:

    • Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
    • Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
    • Redis支持数据的备份,即master-slave模式的数据备份。

1.1.4 扩展之Celery

Celery 是一个由 Python 编写的简单、灵活、可靠的用来处理大量信息的分布式系统,它同时提供操作和维护分布式系统所需的工具。Celery 专注于实时任务处理,支持任务调度。

Celery 本身不是任务队列,它是管理分布式任务队列的工具,或者换一种说法,它封装好了操作常见任务队列的各种操作,我们可以用 Celery 提供的接口快速实现并管理一个分布式的任务队列。

一个Celery系统可以由多个worker(工作者,类似与生产/消费模型中的消费者,其从队列中取出任务并执行)和broker(中间人,在这里就是指任务队列本身)组成,从而实现高可用性和横向扩展。Celery 扮演生产者和消费者的角色,brokers 就是生产者和消费者存放/拿取产品的地方(队列)。常见的 brokers 有 rabbitmq、redis、Zookeeper 等。

Celery是用Python编写的,但协议可以用任何语言实现(即语言无关的)。除了Python之外,还有Node.js的Node-celery,PHP客户端,golang的gocelery和Rust的rusty-celery。

1.1.5 扩展之fastDFS

fastDFS 是以C语言开发的一项开源轻量级分布式文件系统(Distributed File System,DFS),由跟踪服务器(tracker server)、存储服务器(storage server)和客户端(client)三个部分组成,主要解决了海量数据存储问题,特别适合以中小文件(建议范围:4KB < file_size <500MB)为载体的在线服务。

他对文件进行管理,主要功能有:文件存储、文件同步、文件访问(文件上传/下载),特别适合以文件为载体的在线服务,如图片网站、视频网站等。

  • 分布式文件系统(Distributed File System,DFS):

    分布式文件系统把大量数据分散到不同的节点上存储,大大减小了数据丢失的风险。分布式文件系统具有冗余性,部分节点的故障并不影响整体的正常运行,而且即使出现故障的计算机存储的数据已经损坏,也可以由其它节点将损坏的数据恢复出来。因此,安全性是分布式文件系统最主要的特征。

    分布式文件系统通过网络将大量零散的计算机连接在一起,形成一个巨大的计算机集群,使各主机均可以充分发挥其价值。此外,集群之外的计算机只需要经过简单的配置就可以加入到分布式文件系统中,具有极强的可扩展能力。

1.1.6 扩展之uwsgi

uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。

WSGI是一种Web服务器网关接口,它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask框架写的程序)通信的一种规范。

  • 要注意 WSGI / uwsgi / uWSGI 这三个概念的区分:
    • WSGI是一种通信协议。
    • uwsgi是一种线路协议而不是通信协议,是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述。常用于在uWSGI服务器与其他网络服务器的数据通信。
    • 而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。

具体介绍及相关操作可见本栏的《【实战学习(五)】开源项目学习之uWSGI》。

1.2 部署架构

部署架构是指将软件如何部署,这种图的呈现方式没有定论,也可以是UML的部署视图,举例来说可以将所有的软件模块放在一台WEB服务器上,就打一个war包,也可以用微服务的方式部署在不同的服务器上,当然缓存、数据库、文件服务器等都可以独立部署。

从这个角度讲,部署架构其实算是物理架构的一种,也就是说你在逻辑架构上拆分出来的组件(或模块)是如何分解到不同的物理设备上的。为什么说它不等价于物理架构,是因为物理架构的概念会更加宽泛,不一定是服务器,比如可能是某个嵌入式硬件;如果针对嵌入式硬件进一步做系统设计分解,那么此时物理架构可能被分解到某一个MCU、DSP甚至是FPGA上。


2)在windows下部署开发环境

2.1 Pycharm

  • 专业版VS社区版

    pyCharm专业版是功能最丰富的,与社区版相比,PYcharm专业版增加了Web开发、Python We框架、Python分析器、远程开发、支持数据库与SQL等更多高级功能。

    pyCharm的社区版中没有Web开发、Python We框架、Python分析器、远程开发、支持数据库与SQL等这些功能。

  • 专业版安装

    (2021/4/18)可参考:bilibili,cv10858020

2.2 Django

Django 是一个 Python Web 框架,因此在电脑上需要安装 Python。

  • pip 是 Python 的包管理器,它使得 Python 包(例如 Django)的安装和卸载变的非常简单。

2.2.1 虚拟环境

需要注意的是:在同一个python环境中安装同一个包的不同版本,后安装的包会把原来安装的包覆盖掉。这样,如果同一台机器上两个项目依赖于相同包的不同版本,则会导致一些项目运行失败。

解决这个问题的方案是:虚拟环境。虚拟环境是真实python环境的复制版本。在虚拟环境中使用的python是复制的python,安装python包也是安装在复制的python中。

安装virtualenv 和 virtualenvwrapper

virtualenv(虚拟环境) 和 virtualenvwrapper(虚拟环境扩展包) 为每个 Django 项目提供独立环境。

仅需要输入:

pip install virtualenvwrapper-win  # 在Linux下输入pip install virtualenvwrapper就可以,因为这是在Windows下安装,所以加了-win。

virtualwrapper会将创建的虚拟环境放到同一个目录下面。

  • pip安装过程中遇到的问题之“timed out”(超时问题)报错
    C:\Users\Facek>pip3 install Django==2.1.7
    Collecting Django==2.1.7
      Downloading Django-2.1.7-py3-none-any.whl (7.3 MB)
         |█████████                       | 2.0 MB 7.7 kB/s eta 0:11:21ERROR: Exception:
    Traceback (most recent call last):
      File "c:\python37\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 171, in _merge_into_criterion
        crit = self.state.criteria[name]
    KeyError: 'django'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "c:\python37\lib\site-packages\pip\_vendor\urllib3\response.py", line 438, in _error_catcher
    ...
      File "c:\python37\lib\ssl.py", line 929, in read
        return self._sslobj.read(len, buffer)
    socket.timeout: The read operation timed out
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
     ...
      File "c:\python37\lib\site-packages\pip\_vendor\urllib3\response.py", line 443, in _error_catcher
        raise ReadTimeoutError(self._pool, None, "Read timed out.")
    pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
    
  • 解决方法:

    使用:pip --default-timeout=100 install -U 库名

1、成功安装:
C:\Users\Facek>pip --default-timeout=100 install -U virtualenvwrapper-win
Defaulting to user installation because normal site-packages is not writeable
Collecting virtualenvwrapper-win
  Using cached virtualenvwrapper-win-1.2.6.tar.gz (21 kB)
Collecting virtualenv
  Using cached virtualenv-20.4.3-py2.py3-none-any.whl (7.2 MB)
Collecting six<2,>=1.9.0
  Using cached six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting appdirs<2,>=1.4.3
  Using cached appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
Collecting filelock<4,>=3.0.0
  Using cached filelock-3.0.12-py3-none-any.whl (7.6 kB)
Collecting importlib-metadata>=0.12; python_version < "3.8"
  Using cached importlib_metadata-4.0.0-py3-none-any.whl (16 kB)
Collecting distlib<1,>=0.3.1
  Using cached distlib-0.3.1-py2.py3-none-any.whl (335 kB)
Collecting zipp>=0.5
  Using cached zipp-3.4.1-py3-none-any.whl (5.2 kB)
Collecting typing-extensions>=3.6.4; python_version < "3.8"
  Using cached typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Using legacy setup.py install for virtualenvwrapper-win, since package 'wheel' is not installed.
Installing collected packages: six, appdirs, filelock, zipp, typing-extensions, importlib-metadata, distlib, virtualenv, virtualenvwrapper-win
  WARNING: The script virtualenv.exe is installed in 'C:\Users\Facek\AppData\Roaming\Python\Python37\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
    Running setup.py install for virtualenvwrapper-win ... done
Successfully installed appdirs-1.4.4 distlib-0.3.1 filelock-3.0.12 importlib-metadata-4.0.0 six-1.15.0 typing-extensions-3.7.4.3 virtualenv-20.4.3 virtualenvwrapper-win-1.2.6 zipp-3.4.1
WARNING: You are using pip version 20.1.1; however, version 21.0.1 is available.
You should consider upgrading via the 'c:\program files\python37\python.exe -m pip install --upgrade pip' command.
2、验证virtualenv的使用:
C:\Users\Facek>mkvirtualenv myproject  # 创建新的虚拟环境
created virtual environment CPython3.7.9.final.0-64 in 3675ms
  creator CPython3Windows(dest=C:\Users\Facek\Envs\myproject, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\Facek\AppData\Local\pypa\virtualenv)
    added seed packages: pip==21.0.1, setuptools==54.1.2, wheel==0.36.2
  activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

(myproject) C:\Users\Facek>  # 进入新建的虚拟环境
(myproject) C:\Users\Facek>
(myproject) C:\Users\Facek>deactivate  # 退出虚拟环境

C:\Users\Facek>
C:\Users\Facek>
C:\Users\Facek>workon  # 查看当前有哪些虚拟环境

Pass a name to activate one of the following virtualenvs:
==============================================================================
myproject

C:\Users\Facek>workon myproject  # 进入myproject虚拟环境
(myproject) C:\Users\Facek>
(myproject) C:\Users\Facek>deactivate

C:\Users\Facek>rmvirtualenv myproject  # 删除某个虚拟环境

    Deleted C:\Users\Facek\Envs\myproject

3、在虚拟环境中安装开发包:
C:\Users\Facek>workon myproject  # 进入虚拟环境
(myproject) C:\Users\Facek>pip list  # 查看已经按照的安装包
Package    Version
---------- -------
pip        21.0.1
setuptools 54.1.2
wheel      0.36.2

(myproject) C:\Users\Facek>pip install requests  # 安装requests
Collecting requests
  Using cached requests-2.25.1-py2.py3-none-any.whl (61 kB)
Collecting certifi>=2017.4.17
  Using cached certifi-2020.12.5-py2.py3-none-any.whl (147 kB)
Collecting chardet<5,>=3.0.2
  Using cached chardet-4.0.0-py2.py3-none-any.whl (178 kB)
Collecting urllib3<1.27,>=1.21.1
  Using cached urllib3-1.26.4-py2.py3-none-any.whl (153 kB)
Collecting idna<3,>=2.5
  Using cached idna-2.10-py2.py3-none-any.whl (58 kB)
Installing collected packages: urllib3, idna, chardet, certifi, requests
Successfully installed certifi-2020.12.5 chardet-4.0.0 idna-2.10 requests-2.25.1 urllib3-1.26.4

(myproject) C:\Users\Facek>pip uninstall requests  # 卸载requests包
Found existing installation: requests 2.25.1
Uninstalling requests-2.25.1:
  Would remove:
    c:\users\facek\envs\myproject\lib\site-packages\requests-2.25.1.dist-info\*
    c:\users\facek\envs\myproject\lib\site-packages\requests\*
Proceed (y/n)? y
  Successfully uninstalled requests-2.25.1

2.2.2 安装Django

可以轻松地在虚拟环境中使用 pip 安装Django 。

在命令提示行中,确认虚拟环境是激活的,然后运行以下命令:

(myproject) C:\Users\Facek>pip install Django==2.1.7  # 安装版本2.1.7的Django
Collecting Django==2.1.7
  Using cached Django-2.1.7-py3-none-any.whl (7.3 MB)
Requirement already satisfied: pytz in c:\users\facek\envs\myproject\lib\site-packages (from Django==2.1.7) (2021.1)
Installing collected packages: Django
Successfully installed Django-2.1.7

(myproject) C:\Users\Facek>django-admin --version  # 验证安装的Django
2.1.7


3)在Ubuntu下部署开发环境

3.1 Pycharm

3.1.1下载

在官网 https://www.jetbrains.com/pycharm/ 获取 PyCharm。下载好的文件的名称可能是 “pycharm-professional-2021.1.tar.gz”。以 “tar.gz” 结尾的文件是被 gzip 工具压缩过的,并且把文件夹用 tar 工具归档到了一起。

3.1.2 解压文件并安装PyCharm软件

he@he-ThinkPad-X200:~$ cd Downloads  # 进入下载文件所在的文件夹
he@he-ThinkPad-X200:~/Downloads$ ls pycharm*  # 找到下载的文件的名字
pycharm-professional-2021.1.tar.gz
he@he-ThinkPad-X200:~/Downloads$ tar -xvzf pycharm-professional-2021.1.tar.gz -C ~  # 解压文件并安装在home目录中
  • Linux中的tar命令
    linux下最常用的打包程序就是tar(tape archive,磁带归档)。扩展名为.tar和.tgz的文件,分别代表普通的tar归档文件和经过gzip压缩的归档。tar命令是用来压缩和解压文件。tar本身不具有压缩功能。他是调用压缩功能实现的 。
    • 命令格式:

      tar[必要参数][选择参数][文件]

    • 必要命令参数:
        -A 新增压缩文件到已存在的压缩
        -B 设置区块大小
        -c 建立新的压缩文件
        -d 记录文件的差别
        -r 添加文件到已经压缩的文件
        -u 添加改变了和现有的文件到已经存在的压缩文件
        -x 从压缩的文件中提取文件
        -t 显示压缩文件的内容
        -z 支持gzip解压文件
        -j 支持bzip2解压文件
        -Z 支持compress解压文件
        -v 显示操作过程
        -l 文件系统边界设置
        -k 保留原有文件不覆盖
        -m 保留文件不被覆盖
        -W 确认压缩文件的正确性
      
    • 可选参数:
        -b 设置区块数目
        -C 切换到指定目录
        -f 指定压缩文件
        --help 显示帮助信息
        --version 显示版本信息
      

3.1.3 运行PyCharm软件

he@he-ThinkPad-X200:~$ ls  # 查找文件夹名
Desktop  Documents  Downloads  github-learning  Music  Pictures  Public  pycharm-2021.1  Templates  test  Videos
he@he-ThinkPad-X200:~$ cd pycharm-2021.1/bin  # 进入pycharm目录
he@he-ThinkPad-X200:~/pycharm-2021.1/bin$ ls
format.sh   fsnotifier64     inspect.sh   log.xml    printenv.py          pycharm.png  pycharm.svg        restart.py
fsnotifier  idea.properties  libdbm64.so  ltedit.sh  pycharm64.vmoptions  pycharm.sh   pycharm.vmoptions
he@he-ThinkPad-X200:~/pycharm-2021.1/bin$ sh pycharm.sh &  # 运行pycharm
  • Linux常用命令之运行.sh文件
    • 方法一)直接./加上文件名.sh,如运行hello.sh为./hello.sh【hello.sh必须有x权限】
    • 方法二)直接sh 加上文件名.sh,如运行hello.sh为sh hello.sh【hello.sh可以没有x权限】
  • Linux后台运行脚本的方法和命令

    直接在后台运行,即在命令最后加一个&。

    1、直接在后台运行脚本test.sh:./test.sh &

    2、查看当前shell环境中已启动的任务情况:jobs

    3、将test.sh切换到前台运行:fg %number(”number”为使用jobs命令查看到的 [ ] 中的数字,不是pid)

    4、中断后台运行的test.sh脚本:先fg %number切换到前台,再ctrl+c;或是直接kill %number

如果是在一个桌面环境比如 GNOME、KDE、Unity、Cinnamon 或者其他现代桌面上运行,那么也可以通过桌面环境的菜单或者快捷方式来找到 PyCharm 。

3.2 python

3.2.1 查看Ubuntu所有的python版本以及默认的版本

1、查看所有的python版本:

he@he-ThinkPad-X200:~$ python3 --version  # 查看python3的版本有哪些
Python 3.8.5
he@he-ThinkPad-X200:~$ python2 --version  # 查看python2的版本有哪些

Command 'python2' not found, but can be installed with:

sudo apt install python2

注意:一定不要擅自删除Ubuntu自带的python版本,否则系统会崩溃!!(因为系统的其他模块对系统自带的python有依赖)

2、查看默认python的版本:

he@he-ThinkPad-X200:~$ ls -l /usr/bin | grep python  # 查看python的指向
-rwxr-xr-x 1 root root      179922 3月  15  2020 glade2script-python3
lrwxrwxrwx 1 root root          23 1月  27 23:41 pdb3.8 -> ../lib/python3.8/pdb.py
lrwxrwxrwx 1 root root          31 4月  10 01:26 py3versions -> ../share/python3/py3versions.py
lrwxrwxrwx 1 root root           9 4月  10 01:26 python3 -> python3.8
-rwxr-xr-x 1 root root     5486384 1月  27 23:41 python3.8
-rwxr-xr-x 1 root root         384 3月  28  2020 python3-futurize
-rwxr-xr-x 1 root root         388 3月  28  2020 python3-pasteurize

可以看到,此时python指向的是python3.8。

3.2.2 安装python3.7

由于项目所用的Django==2.1.7版本支持 Python3.5、3.6 以及 3.7,因此需要另外安装python3.7。

3.2.2.1 直接用apt-get安装(失败了)
he@he-ThinkPad-X200:~$ sudo apt-get install python3.7
[sudo] password for he: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'python3.7-2to3' for regex 'python3.7'
Note, selecting 'libpython3.7-stdlib' for regex 'python3.7'
0 upgraded, 0 newly installed, 0 to remove and 46 not upgraded.
3.2.2.2手动安装

在python官网找到python-3.7.9.tgz的地址:https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz

1、下载和解压
he@he-ThinkPad-X200:~$ wget wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz  # 下载安装包
--2021-04-24 18:32:12--  http://wget/
Resolving wget (wget)... failed: Temporary failure in name resolution.
wget: unable to resolve host address ‘wget’
--2021-04-24 18:32:12--  https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
Resolving www.python.org (www.python.org)... 151.101.228.223, 2a04:4e42:36::223
Connecting to www.python.org (www.python.org)|151.101.228.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 23277790 (22M) [application/octet-stream]
Saving to: ‘Python-3.7.9.tgz’
...

he@he-ThinkPad-X200:~$ tar -zxvf Python-3.7.9.tgz  # 解压安装包
Python-3.7.9/
Python-3.7.9/Doc/
Python-3.7.9/Doc/c-api/
...
2、 安装python依赖的lib
sudo apt-get update; sudo apt-get install --no-install-recommends make build-essential libssl-dev \
zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev\
 libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

sudo apt-get install build-essential checkinstall
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev \
    libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
3、检测和编译
  • ./configure 是用来检测你的安装平台的目标特征的。比如它会检测你是不是有CC或GCC,并不是需要CC或GCC,它是个shell脚本。执行该命令,则安装后可执行文件默认放在/usr /local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/include,其它的资源文件放在/usr /local/share。

  • make 是用来编译的,它从Makefile中读取指令,然后编译

  • make install是用来安装的,它也从Makefile中读取指令,安装到指定的位置

he@he-ThinkPad-X200:~$ cd Python-3.7.9/
he@he-ThinkPad-X200:~/Python-3.7.9$ ./configure  # 检测安装平台的目标特征的
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.7... no
checking for python3... python3
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/home/he/Python-3.7.9':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
  • 报错之no acceptable C compiler found in $PATH
    • 原因:没有c编译器,默认系统$PATH中有c编译器,一般是指向gcc的。
    • 解决方法:如果是ubuntu,那么apt install build-essential,以及安装gcc(sudo apt-get install gcc),其他缺什么装什么。

编译用sudo make(没有安装make的安装一下)。

4、安装

安装用sudo make install命令。

  • 查看安装目录:

    he@he-ThinkPad-X200:~$ cd /usr/local/lib
    he@he-ThinkPad-X200:/usr/local/lib$ ll
    total 34924
    drwxr-xr-x  5 root root      4096 4月  24 20:07 ./
    drwxr-xr-x 10 root root      4096 2月  10 02:47 ../
    -r-xr-xr-x  1 root root  35739946 4月  24 20:06 libpython3.7m.a*
    drwxr-xr-x  2 root root      4096 4月  24 20:07 pkgconfig/
    drwxr-xr-x 35 root root      4096 4月  24 20:07 python3.7/
    drwxrwsr-x  3 root staff     4096 2月  10 02:47 python3.8/
    
    

    可以看到此时python3.7安装到了/usr/local/lib/(若步骤3执行./configure --prefix=/usr/local/python3.7.9,python3.7安装到了/usr/local/python3.7.9/lib/)

  • 测试:输入python3.7

    he@he-ThinkPad-X200:~$ python3.7
    Python 3.7.9 (default, Apr 24 2021, 20:06:16) 
    [GCC 9.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    
3.2.2.3更改默认的python版本
1、查看python命令的指向
he@he-ThinkPad-X200:~$ ls -l /usr/bin | grep python  # 查看python命令的指向
-rwxr-xr-x 1 root root      179922 3月  15  2020 glade2script-python3
lrwxrwxrwx 1 root root          23 1月  27 23:41 pdb3.8 -> ../lib/python3.8/pdb.py
lrwxrwxrwx 1 root root          31 4月  10 01:26 py3versions -> ../share/python3/py3versions.py
lrwxrwxrwx 1 root root           9 4月  10 01:26 python3 -> python3.8
-rwxr-xr-x 1 root root     5486384 1月  27 23:41 python3.8
-rwxr-xr-x 1 root root         384 3月  28  2020 python3-futurize
-rwxr-xr-x 1 root root         388 3月  28  2020 python3-pasteurize
2、更改

由于python3.7是自己安装的,不在/usr/bin下,而在usr/local/bin下:

he@he-ThinkPad-X200:~$ ls /usr/local/bin -l
total 32472
lrwxrwxrwx 1 root root        8 4月  24 20:07 2to3 -> 2to3-3.7
-rwxr-xr-x 1 root root      101 4月  24 20:07 2to3-3.7
-rwxr-xr-x 1 root root      238 4月  24 20:07 easy_install-3.7
lrwxrwxrwx 1 root root        7 4月  24 20:07 idle3 -> idle3.7
-rwxr-xr-x 1 root root       99 4月  24 20:07 idle3.7
-rwxr-xr-x 1 root root      229 4月  24 20:07 pip3
-rwxr-xr-x 1 root root      229 4月  24 20:07 pip3.7
lrwxrwxrwx 1 root root        8 4月  24 20:07 pydoc3 -> pydoc3.7
-rwxr-xr-x 1 root root       84 4月  24 20:07 pydoc3.7
lrwxrwxrwx 1 root root        9 4月  24 20:07 python3 -> python3.7
-rwxr-xr-x 2 root root 16608592 4月  24 20:06 python3.7
lrwxrwxrwx 1 root root       17 4月  24 20:07 python3.7-config -> python3.7m-config
-rwxr-xr-x 2 root root 16608592 4月  24 20:06 python3.7m
-rwxr-xr-x 1 root root     2902 4月  24 20:07 python3.7m-config
lrwxrwxrwx 1 root root       16 4月  24 20:07 python3-config -> python3.7-config
lrwxrwxrwx 1 root root       10 4月  24 20:07 pyvenv -> pyvenv-3.7
-rwxr-xr-x 1 root root      441 4月  24 20:07 pyvenv-3.7
he@he-ThinkPad-X200:~$ ls -l /usr/local/bin | grep python
lrwxrwxrwx 1 root root        9 4月  24 20:07 python3 -> python3.7
-rwxr-xr-x 2 root root 16608592 4月  24 20:06 python3.7
lrwxrwxrwx 1 root root       17 4月  24 20:07 python3.7-config -> python3.7m-config
-rwxr-xr-x 2 root root 16608592 4月  24 20:06 python3.7m
-rwxr-xr-x 1 root root     2902 4月  24 20:07 python3.7m-config
lrwxrwxrwx 1 root root       16 4月  24 20:07 python3-config -> python3.7-config

3.3 安装虚拟环境

3.3.1 安装

命令:

sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper
# 注意:安装的顺序不能颠倒,virtualenvwrapper必须依赖于virtualenv。

3.3.2 创建虚拟环境

1、创建虚拟环境目录
mkdir ~/.virtualenvs
2、设置环境变量
  • 打开./bashrc文件:sudo vim ~/.bashrc

  • 找到virtualenvwrapper.sh所在的路径:sudo find / -name virtualenvwrapper.sh

在该文件末尾添加以下内容:

export WORKON_HOME=$HOME/.virtualenvs    # 所有虚拟环境存储的目录
source /usr/local/bin/virtualenvwrapper.sh  # virtualenvwrapper.sh所在路径
  • 启用配置文件:source ~/.bashrc

3.3.3 虚拟环境的使用

1、创建虚拟环境
he@he-ThinkPad-X200:~$ mkvirtualenv -p python3.7 test  # 指定python版本创建虚拟环境
created virtual environment CPython3.7.9.final.0-64 in 369ms
  creator CPython3Posix(dest=/home/he/.virtualenvs/test, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/he/.local/share/virtualenv)
    added seed packages: pip==21.0.1, setuptools==56.0.0, wheel==0.36.2
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
*遇到的问题之Command ‘’ not found, but can be installed with
he@he-ThinkPad-X200:~$ mkvirtualenv -p python3.7 test
created virtual environment CPython3.7.9.final.0-64 in 369ms
  creator CPython3Posix(dest=/home/he/.virtualenvs/test, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/he/.local/share/virtualenv)
    added seed packages: pip==21.0.1, setuptools==56.0.0, wheel==0.36.2
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

Command '' not found, but can be installed with:

sudo apt install mailutils-mh  # version 1:3.7-2.1, or
sudo apt install meshio-tools  # version 4.0.4-1
sudo apt install mmh           # version 0.4-2
sudo apt install nmh           # version 1.7.1-6
sudo apt install termtris      # version 1.3-1

通过在bashrc中添加以下解决:

export VIRTUALENVWRAPPER_PYTHON='/usr/local/bin/python3.7'
  • This line means that virtualenvwrapper will use python 3.7. You have to replace ‘/usr/local/bin/python3.7’ with your python path.

重新启用配置文件后,就无该问题出现了:

he@he-ThinkPad-X200:~$ mkvirtualenv test2
created virtual environment CPython3.7.9.final.0-64 in 411ms
  creator CPython3Posix(dest=/home/he/.virtualenvs/test2, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/he/.local/share/virtualenv)
    added seed packages: pip==21.0.1, setuptools==56.0.0, wheel==0.36.2
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
virtualenvwrapper.user_scripts creating /home/he/.virtualenvs/test2/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/he/.virtualenvs/test2/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/he/.virtualenvs/test2/bin/preactivate
virtualenvwrapper.user_scripts creating /home/he/.virtualenvs/test2/bin/postactivate
virtualenvwrapper.user_scripts creating /home/he/.virtualenvs/test2/bin/get_env_details
(test2) he@he-ThinkPad-X200:~$ 

2、切换到某个虚拟环境、退出当前虚拟环境
he@he-ThinkPad-X200:~$ workon test2
(test2) he@he-ThinkPad-X200:~$ workon test  # 切换到名字为test的虚拟环境
(test) he@he-ThinkPad-X200:~$ deactivate  # 退出当前虚拟环境
he@he-ThinkPad-X200:~$ 

3、列出所有虚拟环境、删除某个虚拟环境
he@he-ThinkPad-X200:~$ lsvirtualenv  # 列出所有虚拟环境
test2
=====


test37
======


test
====


he@he-ThinkPad-X200:~$ rmvirtualenv test2  # 删除某个虚拟环境
Removing test2...
he@he-ThinkPad-X200:~$ rmvirtualenv test
Removing test...
he@he-ThinkPad-X200:~$ rmvirtualenv test37
Removing test37...
he@he-ThinkPad-X200:~$ lsvirtualenv
he@he-ThinkPad-X200:~$ 

显示所有虚拟环境也可通过:workon + 两次tab键。

4、进入虚拟环境所在的目录
(test) he@he-ThinkPad-X200:~$ cdvirtualenv  # 进入到虚拟环境所在的目录
(test) he@he-ThinkPad-X200:~/.virtualenvs/test$ 
5、查看/安装虚拟环境中的python包
he@he-ThinkPad-X200:~$ workon test
(test) he@he-ThinkPad-X200:~$ pip list
Package    Version
---------- -------
pip        21.0.1
setuptools 56.0.0
wheel      0.36.2

  • 安装: 进入虚拟环境,在终端输入: pip install 包的名字

注意:一定不要使用sudo pip … ,这里是在虚拟环境中安装python包,如果使用了sudo权限,python包会被安装在主机非虚拟环境下,在虚拟环境中找不到这个包。

3.4 mysql的安装和使用

3.4.1 安装MySQL

在Ubuntu中,默认情况下,只有最新版本的MySQL包含在APT软件包存储库中,要安装它,只需更新服务器上的包索引并安装默认包apt-get:

sudo apt-get update

sudo apt-get install mysql-server

3.4.2 配置MySQL

3.4.2.1 初始化配置
he@he-ThinkPad-X200:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: N
Please set the password for root here.

New password: 

Re-enter new password: 
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : N

 ... skipping.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.

All done! 
3.4.2.2 检查mysql服务状态、启动/停止/重启mysql服务
he@he-ThinkPad-X200:~$ systemctl status mysql.service 
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-04-24 23:36:02 CST; 10min ago
   Main PID: 182326 (mysqld)
     Status: "Server is operational"
      Tasks: 39 (limit: 4528)
     Memory: 331.1M
     CGroup: /system.slice/mysql.service
             └─182326 /usr/sbin/mysqld

4月 24 23:35:59 he-ThinkPad-X200 systemd[1]: Starting MySQL Community Server...
4月 24 23:36:02 he-ThinkPad-X200 systemd[1]: Started MySQL Community Server.
# 说明mysql服务是正常的
  • 启动/停止/重启服务:
he@he-ThinkPad-X200:~$ systemctl stop mysql.service  # 停止服务
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to stop 'mysql.service'.
Authenticating as: he,,, (he)
Password: 
==== AUTHENTICATION COMPLETE ===
he@he-ThinkPad-X200:~$ systemctl status mysql.service 
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Sat 2021-04-24 23:53:04 CST; 3s ago
    Process: 182326 ExecStart=/usr/sbin/mysqld (code=exited, status=0/SUCCESS)
   Main PID: 182326 (code=exited, status=0/SUCCESS)
     Status: "Server shutdown complete"

4月 24 23:35:59 he-ThinkPad-X200 systemd[1]: Starting MySQL Community Server...
4月 24 23:36:02 he-ThinkPad-X200 systemd[1]: Started MySQL Community Server.
4月 24 23:53:03 he-ThinkPad-X200 systemd[1]: Stopping MySQL Community Server...
4月 24 23:53:04 he-ThinkPad-X200 systemd[1]: mysql.service: Succeeded.
4月 24 23:53:04 he-ThinkPad-X200 systemd[1]: Stopped MySQL Community Server.

he@he-ThinkPad-X200:~$ sudo systemctl start mysql.service  # 启动服务
he@he-ThinkPad-X200:~$ systemctl status mysql.service 
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-04-24 23:55:46 CST; 7s ago
    Process: 183920 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 183928 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 4528)
     Memory: 335.3M
     CGroup: /system.slice/mysql.service
             └─183928 /usr/sbin/mysqld

4月 24 23:55:43 he-ThinkPad-X200 systemd[1]: Starting MySQL Community Server...
4月 24 23:55:46 he-ThinkPad-X200 systemd[1]: Started MySQL Community Server.

he@he-ThinkPad-X200:~$ systemctl restart mysql.service  # 重启服务
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'mysql.service'.
Authenticating as: he,,, (he)
Password: 
==== AUTHENTICATION COMPLETE ===
he@he-ThinkPad-X200:~$ systemctl status mysql.service 
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-04-24 23:56:17 CST; 3s ago
    Process: 184004 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 184012 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 4528)
     Memory: 332.5M
     CGroup: /system.slice/mysql.service
             └─184012 /usr/sbin/mysqld

4月 24 23:56:15 he-ThinkPad-X200 systemd[1]: Stopped MySQL Community Server.
4月 24 23:56:15 he-ThinkPad-X200 systemd[1]: Starting MySQL Community Server...
4月 24 23:56:17 he-ThinkPad-X200 systemd[1]: Started MySQL Community Server.
3.4.2.3 访问
he@he-ThinkPad-X200:~$ sudo mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值