笔者在 python笔记4–python常见功能函数和问题 中记录了很多python相关的小技能和注意事项,内容比较多而且排版较杂乱,因此重新在此处记录相关事宜;后续会在此处需更,以便自己和有需要的小伙伴查阅 & 学习!
1 配置安装
- 使用清华源安装库
也可以通过如下方式将系统默认pip源配置为清华的源: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/ 即可
进入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
- ubuntu 16.04 安装 python3.6
NO_PUBKEY 5BB92C09DB82666C #53sudo 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
更多ubuntu python 版本可以参考 github.com/deadsnakes - 通过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 常见问题
-
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.” 的解决方法
-
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
-
使用 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
-
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 安装即可
-
python3.5 报错 “AttributeError: module ‘os’ has no attribute ‘PathLike’”
解决方法: 安装更高版本的 python, 如python3.6 安装方法参考本文: 1 配置安装-> 2. ubuntu 16.04 安装 python3.6
3 常见功能函数
- 获取python 脚本所在的文件目录和文件名
python获取程序执行文件路径方法import os dirname, filename = os.path.split(os.path.abspath(__file__)) print(dirname, ',', filename)
- 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
- 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.1 时间字符串转时间戳
4.2 时间戳转时间字符串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
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+