python笔记14--常见功能函数与异常

python笔记14--常见功能函数与异常

笔者在 python笔记4–python常见功能函数和问题 中记录了很多python相关的小技能和注意事项,内容比较多而且排版较杂乱,因此重新在此处记录相关事宜;后续会在此处需更,以便自己和有需要的小伙伴查阅 & 学习!

1 配置安装

  1. 使用清华源安装库
    pip install PackageName -i https://pypi.tuna.tsinghua.edu.cn/simple 
    pip install --upgrade pip 升级pip版本,或者easy_install -U pip==版本号如19.2.3
    pip list 查看已经安装的包
    pip install --user --upgrade scikit-learn==0.20.2 升级scikit-learn到指定的版本
    pip install --upgrade pip setuptools pyinstaller 升级pip和setuptools
    若使用阿里云的源,则 -i https://mirrors.aliyun.com/pypi/simple/ 即可
    
    也可以通过如下方式将系统默认pip源配置为清华的源:
    进入root模式,设置index-url:
    cd /root/
    mkdir /.pip
    cd ./.pip
    vim pip.conf
    #在打开的编辑文档中中输入以下代码, "=" 两侧不能有空格
    [global]
    index-url=https://pypi.tuna.tsinghua.edu.cn/simple/
    [install]
    trusted-host=pypi.tuna.tsinghua.edu.cn
    
  2. ubuntu 16.04 安装 python3.6
    sudo add-apt-repository ppa:deadsnakes/ppa
    sudo apt update
    sudo apt install python3.6
    
    若出现 NO_PUBKEY 5BB92C09DB82666C 错误, 通过 apt-key adv 的方式解决,然后重新 update 即可
    apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 5BB92C09DB82666C
    apt-get update
    
    NO_PUBKEY 5BB92C09DB82666C #53
    更多ubuntu python 版本可以参考 github.com/deadsnakes
  3. 通过conda安装python环境
    repo.anaconda.com/miniconda/ 下载指定版本的miniconda3, 例如 Miniconda3-py39_4.12.0-Linux-x86_64.sh
    通过 bash Miniconda3-py39_4.12.0-Linux-x86_64.sh 按照提示安装conda即可
    # 创建环境:
    conda create -n <env_name> <package_names>
    conda create -n xg-desktop python=3.9
    # 删除环境:
    conda remove -n <env_name> <package_name>
    conda remove -n xg-desktop python=3.8
    # 激活环境
    conda activate xg-desktop
    使激活无效
    conda deactivate
    # 初始化conda shell
    conda init bash
    # 查看conda中的环境
    conda info --envs
    # 设置默认环境
    conda info --envs
    # 设置指定环境为 xg-desktop (默认为base)
    conda config --set env_prompt xg-desktop
    # vscode 配置conda py环境方法
    通过Ctrl+Shift+P进入命令面板,然后输入 >python: Select Interpreter , 按需选择conda中的环境即可
    # 设置pip源
    pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
    默认写入到 ~/.config/pip/pip.conf 中
    

2 常见问题

  1. ubuntu2021.04 python3.9 安装pip3 报错 launchpadlib 1.10.13 requires testresources, which is not installed.

    具体报错内容:
    Installing collected packages: wheel, setuptools, pip
    ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
    launchpadlib 1.10.13 requires testresources, which is not installed.
    Successfully installed pip-21.3 setuptools-58.2.0 wheel-0.37.0
    解决方法:
    sudo apt install python3-widgetsnbextension
    sudo apt install python3-testresources
    
    再次执行 python3 get-pip.py 的时候就没有报错了
    

    参考文档: Ubuntu 18.04 出现“launchpadlib 1.10.6 requires testresources, which is not installed.” 的解决方法

  2. pip3 install kubernetes 报错 ERROR: Cannot uninstall ‘PyYAML’

    具体报错如下:
    ERROR: Cannot uninstall 'PyYAML'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
    解决方法:
    pip3 install kubernetes --ignore-installed
    
  3. 使用 jenkins 库报错 lookup3.so: cannot open shared object file: No such file or directory

    直接安装 pip3 install jenkins 后,调用jenkins api就会报上述错误
    原因: jenkins 官方使用的是 python-jenkins
    解决方法:
    pip3 uninstall jenkins
    pip3 install python-jenkins
    
  4. Pycurl 安装报错–Could not run curl-config: [Errno 2] No such file or directory: ‘curl-config’
    pip3 install pycurl===7.43.0.5 报错, 具体内容如下:
    在这里插入图片描述
    解决方法:

    错误提示中已经说明是另外一个包异常的问题,因此我们只需要修复对应的包即可
    # apt-get install libcurl4-openssl-dev (ubuntu 21.04)
    然后重新pip3 安装即可
    
  5. python3.5 报错 “AttributeError: module ‘os’ has no attribute ‘PathLike’”

    解决方法: 安装更高版本的 python, 如python3.6
    安装方法参考本文: 1 配置安装-> 2. ubuntu 16.04 安装 python3.6
    

3 常见功能函数

  1. 获取python 脚本所在的文件目录和文件名
     import os
     dirname, filename = os.path.split(os.path.abspath(__file__))
     print(dirname, ',', filename)
    
    python获取程序执行文件路径方法
  2. json 读写操作
    import json
    
    def save_json_to_file(fileName, data_dict):
        with open(fileName, 'w') as f:
    	    json.dump(data_dict, f)
    
    def read_json_from_file(fileName):
        ret= None
        with open(fileName, 'r') as f:
            ret = json.load(fp=f)
        return ret
    
  3. csv 读写操作
     import csv
    
     def save2csv(data_list, file_name, wr_type='a'):
         with open(file_name, wr_type, newline='') as csv_file:
             writer = csv.writer(csv_file)
             for row in data_list:
                 writer.writerow(row)
     
     
     def get_from_csv(filename):
         f = open(filename, 'r')
         csv_list = f.readlines()
         f.close()
         x = []
         for i in range(len(csv_list)):
             temp_list = csv_list[i].replace('\"', '').split(',')
             x.append(temp_list)
         return x
    
  4. 时间相关操作
    4.1 时间字符串转时间戳
    import time
    import datetime
    
    
    def datetime_str_2_timestamp(datetime_str):
        dt = datetime.datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S.%f")
        t = dt.timetuple()
        ts = int(time.mktime(t))
        return ts
    
    4.2 时间戳转时间字符串
    import datetime
    def timestamp_to_time_str(time_int):
        return time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(time_int))
    

4 说明

默认python版本: python 3.8+

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昕光xg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值