Linux系统部署Python语言开发运行环境

简介: Linux系统部署Python语言开发运行环境

本文的使用环境是Windows的Linux 子系统,详见:Windows 使用 Linux 子系统-CSDN

Ubuntu自带python

20.04版本中包含了Python3.8.10,但不包含pip安装工具。

hann@HannYang:~$ python

Command 'python' not found, did you mean:

 command 'python3' from deb python3

 command 'python' from deb python-is-python3

hann@HannYang:~$ python3

Python 3.8.10 (default, May 26 2023, 14:05:08)

[GCC 9.4.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>>

Debian安装python

不所有linux都自带python,比如Debian就不带python,需要自己安装。

安装命令:$ sudo apt-get install python3

hann@HannYang:~$ sudo apt-get install python3

[sudo] password for hann:

Reading package lists... Done

Building dependency tree... Done

The following additional packages will be installed:

 ca-certificates libexpat1 libnsl2 libpython3-stdlib libpython3.11-minimal libpython3.11-stdlib libsqlite3-0

 media-types openssl python3-minimal python3.11 python3.11-minimal

Suggested packages:

 python3-doc python3-tk python3-venv python3.11-venv python3.11-doc binutils binfmt-support

The following NEW packages will be installed:

 ca-certificates libexpat1 libnsl2 libpython3-stdlib libpython3.11-minimal libpython3.11-stdlib libsqlite3-0

 media-types openssl python3 python3-minimal python3.11 python3.11-minimal

0 upgraded, 13 newly installed, 0 to remove and 0 not upgraded.

Need to get 7,879 kB of archives.

After this operation, 26.7 MB of additional disk space will be used.

Do you want to continue? [Y/n] Y

Get:1 http://deb.debian.org/debian bookworm/main amd64 libpython3.11-minimal amd64 3.11.2-6 [813 kB]

Get:2 http://deb.debian.org/debian bookworm/main amd64 libexpat1 amd64 2.5.0-1 [99.3 kB]

Get:3 ......

......略......

Updating certificates in /etc/ssl/certs...

0 added, 0 removed; done.

Running hooks in /etc/ca-certificates/update.d...

done.

hann@HannYang:~$ python3

Python 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>>

安装 pip

安装命令: $ sudo apt install python3-pip

hann@HannYang:~$ sudo apt install python3-pip

Reading package lists... Done

Building dependency tree

Reading state information... Done

The following additional packages will be installed:

 binutils binutils-common binutils-x86-64-linux-gnu build-essential dpkg-dev fakeroot g++ g++-9 gcc gcc-9

...... 略 ......

After this operation, 199 MB of additional disk space will be used.

Do you want to continue? [Y/n] y

Get:1 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 binutils-x86-64-linux-gnu amd64 2.34-6ubuntu1.6 [1613 kB]

Get:2 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 binutils amd64 2.34-6ubuntu1.6 [3376 B]

Get:3 ...

...... 略 ......

Setting up python3-dev (3.8.2-0ubuntu2) ...

Processing triggers for man-db (2.9.1-1) ...

Processing triggers for libc-bin (2.31-0ubuntu9.9) ...

库列表

列表命令: $ pip list

hann@HannYang:~$ pip list

Package                Version

---------------------- --------------------

attrs                  19.3.0

Automat                0.8.0

blinker                1.4

certifi                2019.11.28

chardet                3.0.4

Click                  7.0

cloud-init             23.2.1

colorama               0.4.3

command-not-found      0.3

configobj              5.0.6

constantly             15.1.0

cryptography           2.8

cupshelpers            1.0

dbus-python            1.2.16

defer                  1.0.6

distro                 1.4.0

distro-info            0.23ubuntu1

entrypoints            0.3

httplib2               0.14.0

hyperlink              19.0.0

idna                   2.8

importlib-metadata     1.5.0

incremental            16.10.1

Jinja2                 2.10.1

jsonpatch              1.22

jsonpointer            2.0

jsonschema             3.2.0

keyring                18.0.1

language-selector      0.1

launchpadlib           1.10.13

lazr.restfulclient     0.14.2

lazr.uri               1.0.3

macaroonbakery         1.3.1

MarkupSafe             1.1.0

more-itertools         4.2.0

netifaces              0.10.4

oauthlib               3.1.0

pexpect                4.6.0

pip                    20.0.2

protobuf               3.6.1

psutil                 5.5.1

pyasn1                 0.4.2

pyasn1-modules         0.2.1

pycairo                1.16.2

pycups                 1.9.73

PyGObject              3.36.0

PyHamcrest             1.9.0

PyJWT                  1.7.1

pymacaroons            0.13.0

PyNaCl                 1.3.0

pyOpenSSL              19.0.0

pyRFC3339              1.1

pyrsistent             0.15.5

pyserial               3.4

python-apt             2.0.1+ubuntu0.20.4.1

python-debian          0.1.36+ubuntu1.1

pytz                   2019.3

pyxdg                  0.26

PyYAML                 5.3.1

requests               2.22.0

requests-unixsocket    0.2.0

SecretStorage          2.3.1

service-identity       18.1.0

setuptools             45.2.0

simplejson             3.16.0

six                    1.14.0

sos                    4.4

ssh-import-id          5.10

systemd-python         234

Twisted                18.9.0

ubuntu-advantage-tools 8001

ufw                    0.36

unattended-upgrades    0.1

urllib3                1.25.8

wadllib                1.3.3

wheel                  0.34.2

zipp                   1.0.0

zope.interface         4.7.1

安装第三方库

安装命令: $ pip install numpy

hann@HannYang:~$ pip install numpy

Collecting numpy

 Downloading numpy-1.24.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.3 MB)

    |████████████████████████████████| 17.3 MB 39 kB/s

Installing collected packages: numpy

Successfully installed numpy-1.24.4

国外服务下载速度极慢,速度显示在30到300 kB/s之间跳动。

使用国内镜像站

比如使用清华镜像,秒装:

$ pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple

实装 tkinter 库

不像windows版本的python3已自带tkinter库,ubantu python3需要自己安装。

安装命令:$ sudo apt-get install python-tk

编写运行代码

测试代码1

1. 创建项目

比如在根目录下建一个python子目录

hann@HannYang:~$ mkdir python
hann@HannYang:~$ cd python
2. 创建源码文件

比如test.py

hann@HannYang:~/python$ touch test.py
hann@HannYang:~/python$ ll
total 8
drwxr-xr-x  2 hann hann 4096 Aug  5 13:40 ./
drwxr-xr-x 22 hann hann 4096 Aug  5 13:38 ../
-rw-r--r--  1 hann hann    0 Aug  5 13:40 test.py
3. 写入源代码

注意:一定要在首行标记解释程序的所在位置 #!/usr/bin/python3

hann@HannYang:~/python$ echo "#!/usr/bin/python3" > test.py
hann@HannYang:~/python$ echo "print('Hello, python')" >> test.py
hann@HannYang:~/python$ more ./test.py
#!/usr/bin/python3
print('Hello, python')
hann@HannYang:~/python$ ll
total 12
drwxr-xr-x  2 hann hann 4096 Aug  5 13:40 ./
drwxr-xr-x 22 hann hann 4096 Aug  5 13:38 ../
-rw-r--r--  1 hann hann   43 Aug  5 13:42 test.py
4. 修改权限

把默认的文件权限-rw-r--r--修改为-rwxr-xr-x

hann@HannYang:~/python$ chmod +x test.py
hann@HannYang:~/python$ ll
total 12
drwxr-xr-x  2 hann hann 4096 Aug  5 13:40 ./
drwxr-xr-x 22 hann hann 4096 Aug  5 13:38 ../
-rwxr-xr-x  1 hann hann   43 Aug  5 13:42 test.py*
5. 运行代码
hann@HannYang:~/python$ ./test.py
Hello, python

测试代码2

再找一个稍微复杂点进行测试,如对链表进行插入排序:

插入排序的动画演示如上。从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示)。

每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中。

插入排序算法:

插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。

每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适当的位置,并将其插入。

重复直到所有输入数据插入完为止。

示例 1:

输入: 4->2->1->3

输出: 1->2->3->4

示例 2:

输入: -1->5->3->4->0

输出: -1->0->3->4->5

操作过程:

hann@HannYang:~/python$ touch test2.py
hann@HannYang:~/python$ chmod +x test2.py
hann@HannYang:~/python$ vi test2.py
hann@HannYang:~/python$ ./test2.py
4->2->1->3->nill
1->2->3->4->nill
-1->5->3->4->0->nill
-1->0->3->4->5->nill

其中,用 vi 编辑test2.py时,直接复制粘贴以下代码:

#!/usr/bin/python3
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
def insertionSortList(head: ListNode) -> ListNode:
    dummy = ListNode(float('-inf'), head)
    tail = dummy
    cur = head
    while cur:
        if tail.val <= cur.val:
            # 若链表尾节点值小于等于当前节点,不需要移动
            tail.next = cur
            tail = cur
            cur = cur.next
        else:
            # 在已排序部分找到插入位置
            prev = dummy
            while prev.next.val < cur.val:
                prev = prev.next
            # 插入节点
            tail.next = cur.next
            cur.next = prev.next
            prev.next = cur
            cur = tail.next
    return dummy.next
def createList(lst):
    head = ListNode(lst[0])
    p = head
    for i in lst[1:]:
        node = ListNode(i)
        p.next = node
        p = p.next
    return head
def traversalList(head):
    while head:
        print(head.val, end="->")
        head = head.next
    print("nill")
#%%
lst1 = [4, 2, 1, 3]
lst2 = [-1, 5, 3, 4, 0]
head1 = createList(lst1)
traversalList(head1)
sorted_head1 = insertionSortList(head1)
traversalList(sorted_head1)
head2 = createList(lst2)
traversalList(head2)
sorted_head2 = insertionSortList(head2)
traversalList(sorted_head2)

exit


目录
相关文章
|
5月前
|
Ubuntu Linux Anolis
Linux系统禁用swap
本文介绍了在新版本Linux系统(如Ubuntu 20.04+、CentOS Stream、openEuler等)中禁用swap的两种方法。传统通过注释/etc/fstab中swap行的方式已失效,现需使用systemd管理swap.target服务或在/etc/fstab中添加noauto参数实现禁用。方法1通过屏蔽swap.target适用于新版系统,方法2通过修改fstab挂载选项更通用,兼容所有系统。
487 3
Linux系统禁用swap
|
5月前
|
Linux
Linux系统修改网卡名为eth0、eth1
在Linux系统中,可通过修改GRUB配置和创建Udev规则或使用systemd链接文件,将网卡名改为`eth0`、`eth1`等传统命名方式,适用于多种发行版并支持多网卡配置。
990 3
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
582 0
Linux系统初始化脚本
|
5月前
|
安全 Linux Shell
Linux系统提权方式全面总结:从基础到高级攻防技术
本文全面总结Linux系统提权技术,涵盖权限体系、配置错误、漏洞利用、密码攻击等方法,帮助安全研究人员掌握攻防技术,提升系统防护能力。
541 1
|
5月前
|
监控 安全 Linux
Linux系统提权之计划任务(Cron Jobs)提权
在Linux系统中,计划任务(Cron Jobs)常用于定时执行脚本或命令。若配置不当,攻击者可利用其提权至root权限。常见漏洞包括可写的Cron脚本、目录、通配符注入及PATH变量劫持。攻击者通过修改脚本、创建恶意任务或注入命令实现提权。系统管理员应遵循最小权限原则、使用绝对路径、避免通配符、设置安全PATH并定期审计,以防范此类攻击。
1172 1
|
Java Linux 应用服务中间件
Linux系统CentOS 7配置Spring Boot运行环境
从阿里云新买的一台Linux服务器,用来部署SpringBoot应用,由于之前一直使用Debian版本,环境配置有所不同,也较为繁琐,本文主要介绍CentOS下配置SpringBoot环境的过程 新建用户 由于新拿到的机器只有root用户,用root用户直接进行各种操作会有一定的安全问题,所以我们先创建用户。
6099 0
|
5月前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
629 1
二、Linux文本处理与文件操作核心命令
|
5月前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
404 137
|
5月前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
967 57