python超正确安装方法

python正确安装方法

安装python指定版本

sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.8-dev

管理python版本

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
sudo update-alternatives --config

安装对应python版本的pip

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
sudo python get-pip.py

1.如果出现这个错误

  File "<frozen zipimport>", line 259, in load_module
  File "/tmp/tmpk_f4xoz6/pip.zip/pip/_internal/locations/_distutils.py", line 20, in <module>
ModuleNotFoundError: No module named 'distutils.cmd'

解决方法
安装

apt-get install python3.8-distutils

2.如果出现gi这个错误

Traceback (most recent call last):
  File "/usr/bin/gnome-terminal", line 9, in <module>
    from gi.repository import GLib, Gio
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in <module>
    from . import _gi
ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import) (/usr/lib/python3/dist-packages/gi/__init__.py)

解决方法
强制为python 3.8安装PyGObject:

sudo python3.8 -m pip install --ignore-installed PyGObject

如果这一步过程中出现

creating build/temp.linux-x86_64-3.8/cairo
    x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -DPYCAIRO_VERSION_MAJOR=1 -DPYCAIRO_VERSION_MINOR=20 -DPYCAIRO_VERSION_MICRO=1 -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/python3.8 -c cairo/bufferproxy.c -o build/temp.linux-x86_64-3.8/cairo/bufferproxy.o -Wall -Warray-bounds -Wcast-align -Wconversion -Wextra -Wformat=2 -Wformat-nonliteral -Wformat-security -Wimplicit-function-declaration -Winit-self -Winline -Wmissing-format-attribute -Wmissing-noreturn -Wnested-externs -Wold-style-definition -Wpacked -Wpointer-arith -Wreturn-type -Wshadow -Wsign-compare -Wstrict-aliasing -Wundef -Wunused-but-set-variable -Wswitch-default -Wno-missing-field-initializers -Wno-unused-parameter -Wno-unused-command-line-argument -fno-strict-aliasing -fvisibility=hidden -std=c99
    cairo/bufferproxy.c:32:10: fatal error: Python.h: No such file or directory
     #include <Python.h>
              ^~~~~~~~~~
    compilation terminated.
    error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1
    ----------------------------------------
    ERROR: Failed building wheel for pycairo
  Failed to build pycairo
  ERROR: Could not build wheels for pycairo, which is required to install pyproject.toml-based projects
  WARNING: You are using pip version 21.3; however, version 21.3.1 is available.
  You should consider upgrading via the '/usr/bin/python3.8 -m pip install --upgrade pip' command.

安装一下这些库

sudo apt install python-dev
sudo apt install python3-dev
sudo apt install python3.8-dev

sudo apt-get install libgirepository1.0-dev
sudo apt-get install python-cairo
sudo apt-get install libcairo2

重新安装PyGObject即可


Python代码性能(时间+内存)

背景

在运行复杂的Python程序时,执行时间会很长,这时也许想提高程序的执行效率。但该怎么做呢?首先,要有个工具能够检测代码中的瓶颈,例如,找到哪一部分执行时间比较长。接着,就针对这一部分进行优化。同时,还需要控制内存和CPU的使用,这样可以在另一方面优化代码。本文主要介绍两种最常用的监控工具。

时间分析

line_profiler模块可以给出执行每行代码所需占用的CPU时间,首先,安装该模块:

  • pip安装
$ pip install line_profiler

or

  • 源码安装
$ git clone https://github.com/rkern/line_profiler.git
$ find line_profiler -name '*.pyx' -exec cython {} \;
$ cd line_profiler
$ pip install . --user
  • 示例

import random 
from line_profiler import LineProfiler
from functools import wraps

#查询接口中每行代码执行的时间
def func_line_time(f):
    @wraps(f)
    def decorator(*args, **kwargs):
        func_return = f(*args, **kwargs)
        lp = LineProfiler()
        lp_wrap = lp(f)
        lp_wrap(*args, **kwargs)
        lp.print_stats()
        return func_return
    return decorator

@func_line_time
# 定义一个测试函数
def random_sort2(n):
    l = [random.random() for i in range(n)]
    l.sort()
    return l

if __name__ == "__main__":
    random_sort2(2000000)

输出如下所示,各参数含义为

Total Time:测试代码的总运行时间
Line:代码行号
Hits:表示每行代码运行的次数
Time:每行代码运行的总时间
Per Hits:每行代码运行一次的时间
% Time:每行代码运行时间的百分比

网上大多数方法是下边:

1.在需要测试的函数前添加装饰器 @profile
2.启动程序 $kernprof -l -v test.py

这种方法无法正常运行原来的脚本,如果不用kernprof指令运行脚本时,还需注释掉@profile语句,不太方便,因此建议使用这里推荐的装饰器的方法。

内存分析

memory_profiler模块用来基于逐行测量代码的内存使用,安装方法如下:

$ pip install memory_profiler psutil

memory_profiler需要使用@profile装饰器来标识需要追踪的函数(即在函数前边加入@profile),就是上边一节后边提到的方法

# 前边内容省略

@profile
# 定义一个测试函数
def random_sort2(n):
    l = [random.random() for i in range(n)]
    l.sort()
    return l

# 后边内容省略

运行

$ python -m memory_profiler test.py

结果

从结果可以看出,内存使用是以MiB为单位衡量的,表示的mebibyte(1MiB = 1.05MB)。

posted @ 2022-09-19 12:42  nanmi  阅读(146)  评论(0编辑  收藏  举报