pip3 --version
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
升级pip:
pip3 install --upgrade pip
配置虚拟环境
创建虚拟环境:
使用 venv模块创建一个虚拟环境,这样可以为每个项目隔离依赖:
python3 -m venv myenv
myenv是你虚拟环境的名称,你可以根据需要命名。
运行结果如下:
python3 -m venv test
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt-get install python3-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/home/wst/test/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
black 是 Python 中一个流行的 formatter,它使用 PEP 8 作为默认的代码风格指南,并提供一致的格式化结果,减少人为的主观判断。
配置 PATH 环境变量
安装一些库后,出现报错(部分):
WARNING: The script pyflakes is installed in '/home/wst/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The script pycodestyle is installed in '/home/wst/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The script flake8 is installed in '/home/wst/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The scripts black and blackd are installed in '/home/wst/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
python3
Python 3.7.3 (default, Nov 1 2022, 10:27:39)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python3.7/turtle.py", line 107, in
import tkinter as TK
ModuleNotFoundError: No module named 'tkinter'
在Deepin系统上配置Python开发环境
简介
为了帮助Python初学者在Deepin系统上尽快配置好一个能用的Python开发环境,本教程介绍了相关的步骤。
本教程主要是在Deepin系统上直接安装相关软件,并尽量依系统自带软件,比如其自研的IDE。
对于一些更高级的开发需求来说,还可以考虑安装VS Code、Anaconda等软件。
安装Python
更新系统:
卸载不需要的软件包:
检查Python版本:
如果未安装,可以执行以下命令:
安装pip
安装
pip:运行情况(部分):
检查是否已安装
pip:运行结果:
升级pip:
配置虚拟环境
创建虚拟环境:
使用
venv模块创建一个虚拟环境,这样可以为每个项目隔离依赖:myenv是你虚拟环境的名称,你可以根据需要命名。运行结果如下:
上述信息提示安装
python3-venv。运行结果如下(部分):
激活虚拟环境:
退出虚拟环境:
安装linter和formatter
为了提高代码质量,安装linter和formatter,如flake8和black:
在编程中,"linter" 和 "formatter" 是两种不同的工具,它们帮助提高代码的质量和一致性:
Linter(语法检查工具):
对于 Python,
flake8是一个流行的 linter 工具,它集成了多个检查器,包括对代码风格、编程错误和复杂度的检查。Formatter(代码格式化工具):
black是 Python 中一个流行的 formatter,它使用 PEP 8 作为默认的代码风格指南,并提供一致的格式化结果,减少人为的主观判断。配置
PATH环境变量安装一些库后,出现报错(部分):
这些警告信息表明,
pyflakes、pycodestyle、flake8、black和blackd这些Python脚本安装在/home/wst/.local/bin目录下,但这个目录不在系统的环境变量PATH中。因此,当你尝试在终端中运行这些脚本时,系统可能找不到它们。修改你的
PATH环境变量,将~/.local/bin添加到里面。这样,系统就能在任何位置下找到这些脚本。对于基于bash的shell,比如你可以通过修改
~/.bashrc或~/.profile文件来添加以下行:然后,运行以下命令使更改生效:
或者,如果你修改的是
.profile,则使用:对于其他类型的shell,比如zsh,你可能需要修改
~/.zshrc文件。你可以使用以下命令来查看当前的
PATH环境变量:安装Python库
使用
pip安装项目所需的库。例如,如果你需要安装
pygame,可以运行以下命令:使用Turtle
Turtle是一个简单的绘图工具,允许我们通过控制一个虚拟的乌龟(turtle)在屏幕上移动来绘制图形。Turtle库通常包含在Python中。
尝试在Python解释器中导入
turtle,报错如下:错误信息表明 Python 3.7.3 环境中缺少
tkinter模块,而turtle模块依赖于tkinter来运行。tkinter是 Python 的标准 GUI(图形用户界面)库。要解决这个问题,你需要安装tkinter。对于基于 Debian 的系统如 Deepin,使用以下命令:
运行过程如下(部分):
安装完成后,你应该能够成功导入
turtle模块。配置Deepin Union Code
我们选择使用系统自带的Deepin Union Code这个IDE。该IDE可能会要求用户安装以下软件:
通过Deepin Union Code打开一个Python工程后,可以参照这个教程上的说法,进行Python相关配置。
该IDE还预置了CodeGeeX插件。建议登录该软件,使用其AI功能。