Python 编程环境

python解释器环境:使用miniconda,mamba,pip

python的IDE基于Python的解释器环境。

  • miniconda 是比较方便的python包管理工具。mamba可以对conda的升级、安装等功能进行加速。
  • 有些包依然只能通过pip安装

奇怪的问题的解决方案:重装

  • 如果miniconda 的使用碰到了莫名其妙解决不了的问题, 重装
  • 如果miniconda 有新版本,重装

包管理工具

  • miniconda + mamba + pip: mamba 是conda的高性能替代品,有些包只能通过pip安装
  • Miniforge:目前是小众,以后可能会发展起来,放弃使用

windows 下使用miniconda

windows下卸载miniconda

参考: Installing on Windows

删除miniconda:

  • In the Windows Control Panel, click Add or Remove Program.
  • Select Python X.X (Miniconda), where X.X is your version of Python.
  • Click Remove Program.
  • 删除用户目录下可能出现的相关文件夹:.conda, .matplotlib, .ipython,.spyder-py3,.streamlit
  • 删除用户目录下可能出现的相关文件: .condarc

windows下安装miniconda

创建配置文件:

conda config --set show_channel_urls yes
  • 设置配置文件,以下为阿里云
    channels定制:根据国内源的url 加 conda-forge 等组装起来,例如: - http://mirrors.aliyun.com/anaconda/cloud/conda-forge/, 示例
channels:
  - http://mirrors.aliyun.com/anaconda/cloud/conda-forge/
  - defaults
show_channel_urls: true
default_channels:
  - http://mirrors.aliyun.com/anaconda/pkgs/main
  - http://mirrors.aliyun.com/anaconda/pkgs/r
  - http://mirrors.aliyun.com/anaconda/pkgs/msys2
custom_channels:
  conda-forge: http://mirrors.aliyun.com/anaconda/cloud
  msys2: http://mirrors.aliyun.com/anaconda/cloud
  bioconda: http://mirrors.aliyun.com/anaconda/cloud
  menpo: http://mirrors.aliyun.com/anaconda/cloud
  pytorch: http://mirrors.aliyun.com/anaconda/cloud
  simpleitk: http://mirrors.aliyun.com/anaconda/cloud
pip install pip -U
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

配置文件示例:

[global]
index-url = https://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

ubuntu下使用miniconda

ubuntu下卸载miniconda

  • 删除miniconda目录:sudo rm -rf ~/miniconda3
  • 清理下.bashrc中的conda相关内容:sudo vi ~/.bashrc
  • source ~/.bashrc
  • 清理conda的安装包:conda clean -a

PyQt5

如果要安装 PyQt5, 先 安装 PyQt5再安装 miniconda,否则有兼容性问题无法启动spyder, 报错:

ModuleNotFoundError: No module named 'PyQt5.QtWebKitWidgets'

安装miniconda

cd ~
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
sudo reboot

如果安装了 zsh, 加入到zsh的路径:

echo 'export PATH="/home/username/miniconda3/bin:$PATH"' >> ~/.zshrc 
source ~/.zshrc

修改为清华大学的源,可参考 https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/, vi ~/.condarc:

channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

更新所有软件包, 检查已经安装的软件包:

conda update --all
conda list

安装常用软件包:

conda install spyder lxml hdf5 pandas beautifulsoup4  h5py   scikit-learn matplotlib  pexpect  Pillow  psycopg2

创建spyder的桌面快捷方式:

  • 拷贝 ~/miniconda3/share/applications/spyder3.desktop 到桌面
  • 修改该文件的属性可以作为执行文件

解决spyder下不能输入中文的问题

拷贝一个文件,然后重启spyder即可:

cp /usr/lib/x86_64-linux-gnu/qt5/plugins/platforminputcontexts/libfcitxplatforminputcontextplugin.so  ~/miniconda3/plugins/platforminputcontexts

解决ubuntu18.04下matplotlib中文显示的问题

  1. 在python的shell环境下查看 matplotlib的字体路径
import matplotlib    
print(matplotlib.matplotlib_fname())

# /home/username/miniconda3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc

2.将字体文件 simhei.ttf ,放到ttf目录下:/home/username/miniconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf

字体下载:https://github.com/yinheco/scp_zh/tree/master/fonts

3.删除缓冲目录: rm -rf ~/.cache/matplotlib

4.在使用中文画图的python源文件中通过如下方式使用matplotlib.pyplot (如果使用的是spyder,可能需要重启控制台才能生效):

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

如果不在源文件中修改如上两行,可以修改 /home/username/miniconda3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc 文件中的如下两处,并重启 spyder之类的IDE:

## 第一处修改,用来正常显示中文标签
#font.sans-serif     : DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
font.sans-serif     : SimHei

## 第二处修改,用来正常显示负号
#axes.unicode_minus  : True    ## use unicode for the minus symbol
                               ## rather than hyphen.  See
                               ## http://en.wikipedia.org/wiki/Plus_and_minus_signs#Character_codes
axes.unicode_minus  : False

spyder中用matplotlib画图时提示信息处理

spyder中用matplotlib画图时提示:

Figures now render in the Plots pane by default. To make them also appear inline in the Console, uncheck "Mute Inline Plotting" under the Plots pane options menu. 

最新版本的Spyder 4.0默认显示图片在右上角,不显示在终端中, 想要弹出窗口显示matplotlib画的图片,修改设置:

Tools > Preferences > iPython console > Graphics > Graphics backend > Automatic

重启IDE,现在画图就会弹出窗口显示了。

pip

为pip设置国内源:

mkdir  ~/.config/pip/ 
vi ~/.config/pip/pip.conf 

pip.conf 内容:

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

查看升级 pip 安装的包:

pip list --outdate
pip install  pkg_name  --upgrade

Warning: xx possible package resolutions (only showing differing packages)

解决方式,重置conda的信道优先级: conda update --strict-channel-priority --all

常用软件包

可以通过conda或mamba 安装:

  • matplotlib: 会图库
  • Pillow :图像处理库
  • requests : 抓取网页
  • spyder:IDE
  • pyinstaller : 打包工具
  • redis-py
mamba install spyder psycopg2 numpy pandas numba selenium lxml furl orjson openpyxl xlrd html5lib beautifulsoup4

谨慎安装的库:

  • json5:速度太慢,推荐orjson

需要通过pip 安装

pip install DBUtils selenium-wire tushare tables  markdown  python-wordpress-xmlrpc  mdx_del_ins

其他

windows下安装PySide2

参考: https://wiki.qt.io/Qt_for_Python

pip install PySide2

示例:

# -*- coding: utf-8 -*-
import sys
from PySide2.QtWidgets import QApplication, QLabel

if __name__ == "__main__":
    app = QApplication(sys.argv)
    label = QLabel("Hello World")
    label.show()
    sys.exit(app.exec_())

该示例不能在 spyder下运行,会报错, spyder绑定了pyqt5:

ImportError: 
    Importing PySide2 disabled by IPython, which has
    already imported an Incompatible QT Binding: pyqt5

从命令号运行示例报错:

python .\temp.py
qt.qpa.plugin: Could not load the Qt platform plugin "windows" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: direct2d, minimal, offscreen, windows.

这也是和 PyQt5冲突造成的,PySide2是QT官方出的Python的QT封装,不过默认安装运行的时候会出现一些小问题,可能是系统里已经安装了其他版本的QT的原因,可以通过添加环境变量来解决:

方式一,python代码中添加, 不会影响使用 PyQt5的应用spyder等:

import os
envpath = r'C:\Miniconda3\Lib\site-packages\PySide2\plugins\platforms'
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = envpath

方式二, 设置环境变量 QT_QPA_PLATFORM_PLUGIN_PATH 的值为 C:\Miniconda3\Lib\site-packages\PySide2\plugins\platforms, 但会影响使用 PyQt5的应用. 如果要消除影响,卸载pyqt重装.

spyder开发组目前明确不会适配 PySide2, 如果是spyder重度用户, 谨慎考虑用 PySide2 — 用PyQt5相对坑少很多.

IDE:PyCharm

PyCharm是功能强大的Python IDE,可以使用conda维护的环境。

基本安装配置:miniconda + pycharm

安装包管理工具miniconda 到 C:\Miniconda3,用conda管理python软件包。

安装PyCharm社区版到: C:\PyCharm2021.2.3

学生和老师可以免费申请pycharm专业版: https://www.jetbrains.com/shop/eform/students

设置python环境: file,settings,project,Python解释器,添加,conda环境(make available to all projects),conda路径 C:\Miniconda3\Scripts\conda.exe

conda 命令 conda info -e : 查看所有虚拟环境,带星号的为当前环境

显示变量:IDE右上角run按钮左侧向下按钮,Edit Configurations,选中 Run with Python Console,这种方法的变量显示上没有Spyder直观。PyCharm专业版下 view,scitific mode, 就和Spyder差不多了。

自动换行:file,settings,编辑器,常规,自动换行,添加 ;*.py

帮助,更改内存设置:尽量设置大一点。

关闭自动更新: file,settings,Appearance Behavior,system settings,updates,

字体:file,settings,Appearance Behavior,Appearance

插件

file,settings,Plugins进行设置。

  • 安装的插件:Chinese 语言包
  • 禁用所有插件
  • 使用的插件
    • Terminal
    • Chinese 语言包
    • windows10 Light Theme

快捷键

自定义快捷键,File —> Settings,Keymap —> 齿轮状设置 —> Duplicate(可选,备份),设置运行: F5

格式化代码: CTRL + ALT + L

正文完
 
评论(没有评论)