其他安装方式:
【MJJ玩鸡新人的Python入门指南】以 Debian 系统为例,纯IPv6,安装Python3环境
效果图
正文开始
作为MJJ新人、VPS小白,学习Python3之前,你肯定已经有若干个废物小鸡了,
选择一个最烂最便宜的,立刻开始学习Python3叭~
本文以 duocloud 的 6.99r/年,HK 纯IPv6 LXC
小鸡为例
.
.
配置如下:
1核 1G内存 1G硬盘
.
.
第一步,修改系统默认的 /etc/resolv.conf 文件内容(下面的四行命令要一起复制,然后粘贴到控制台,输入回车)
这是TREX(芬兰)的公益DNS64,源链接:TREX - Public Service: DNS64 Name Servers
# 命令行直接输入
cat >/etc/resolv.conf <<EOF
nameserver 2001:67c:2b0::4
nameserver 2001:67c:2b0::6
EOF
第二步,更新 apk 包管理器的软件索引
# 命令行直接输入
apk update
# 此时输出:
fetch https://dl-cdn.alpinelinux.org/alpine/v3.19/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.19/community/x86_64/APKINDEX.tar.gz
v3.19.1-579-g751a2e41b25 [https://dl-cdn.alpinelinux.org/alpine/v3.19/main]
v3.19.1-578-g8b830b8367b [https://dl-cdn.alpinelinux.org/alpine/v3.19/community]
OK: 23040 distinct packages available
第三步,安装 Python3
# 命令行直接输入
apk add python3
# 此时输出:
hk:~# apk add python3
(1/14) Installing libexpat (2.6.2-r0)
...
... 省略若干行
...
(11/14) Installing python3 (3.11.9-r0)
(12/14) Installing python3-pycache-pyc0 (3.11.9-r0)
(13/14) Installing pyc (3.11.9-r0)
(14/14) Installing python3-pyc (3.11.9-r0)
Executing busybox-1.36.1-r15.trigger
OK: 97 MiB in 68 packages
第四步,安装 Python3 的包管理工具 pip3
# 命令行直接输入
apk add py3-pip
# 此时输出:
hk:~# apk add py3-pip
(1/8) Installing py3-setuptools-pyc (68.2.2-r0)
...
... 省略若干行
...
(7/8) Installing py3-setuptools (68.2.2-r0)
(8/8) Installing py3-pip (23.3.1-r0)
Executing busybox-1.36.1-r15.trigger
OK: 122 MiB in 76 packages
第五步,创建学习环境 mjj
# 命令行直接输入
python3 -m venv mjj
# 创建好以后,我们用ls命令发现当前目录多了一个mjj文件夹
ls -lh
# 此时输出:
hk:~# ls -lh
total 4K
drwxr-xr-x 5 root root 4.0K May 31 13:49 mjj
第六步,激活学习环境
# 命令行直接输入
source mjj/bin/activate
# 此时命令行开头出现(mjj),表示已经成功激活了
hk:~# source mjj/bin/activate
(mjj) hk:~#
第七步,在学习环境中,安装学习工具 —— ipython
# 命令行直接输入(确保你已经完成了第六步,并且激活了学习环境)
pip install ipython
# 此时输出:
(mjj) hk:~# pip install ipython
Collecting ipython
Using cached ipython-8.25.0-py3-none-any.whl.metadata (4.9 kB)
Collecting decorator (from ipython)
Using cached decorator-5.1.1-py3-none-any.whl.metadata (4.0 kB)
...
... 省略若干行
...
Collecting jedi>=0.16 (from ipython)
Using cached jedi-0.19.1-py2.py3-none-any.whl.metadata (22 kB)
Collecting matplotlib-inline (from ipython)
Using cached matplotlib_inline-0.1.7-py3-none-any.whl.metadata (3.9 kB)
Installing collected packages: wcwidth, pure-eval, ptyprocess, typing-extensions, traitlets, six, pygments, prompt-toolkit, pexpect, parso, executing, decorator, matplotlib-inline, jedi, asttokens, stack-data, ipython
Successfully installed asttokens-2.4.1 decorator-5.1.1 executing-2.0.1 ipython-8.25.0 jedi-0.19.1 matplotlib-inline-0.1.7 parso-0.8.4 pexpect-4.9.0 prompt-toolkit-3.0.45 ptyprocess-0.7.0 pure-eval-0.2.2 pygments-2.18.0 six-1.16.0 stack-data-0.6.3 traitlets-5.14.3 typing-extensions-4.12.0 wcwidth-0.2.13
第八步,进入学习工具 IPython 的 交互界面
# 命令行直接输入
ipython
# 此时输出:
(mjj) hk:~# ipython
Python 3.11.9 (main, Apr 14 2024, 13:40:00) [GCC 13.2.1 20231014]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.25.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]:
第九步,学习第一条代码,print,尝试打印字符串 hello mjj
# 命令行直接输入
print('hello mjj')
# 此时输出:
(mjj) hk:~# ipython
Python 3.11.9 (main, Apr 14 2024, 13:40:00) [GCC 13.2.1 20231014]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.25.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: print('hello mjj')
hello mjj
In [2]: <------- 光标已经定位到这里了,可以接着输别的命令
最后一步,退出ipython环境
# 命令行直接输入
exit()
# 此时输出:
(mjj) hk:~# ipython
Python 3.11.9 (main, Apr 14 2024, 13:40:00) [GCC 13.2.1 20231014]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.25.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: print('hello mjj')
hello mjj
In [2]: exit()
(mjj) hk:~# <----------- 这里显示已经退回到控制台了
FAQ:如何退出学习环境呢?
# 输入命令
deactivate
# 此时输出
(mjj) hk:~# deactivate
hk:~# <----------- 这里发现(mjj)前缀已经没有了,表示已经成功退出学习环境