当前位置 : 主页 > 编程语言 > python >

Python 编译安装与打包

来源:互联网 收集:自由互联 发布时间:2022-06-15
Python的开发环境安装和配置非常的简单,如果是Linux系统则会默认集成安装了Python环境,Python的可执行文件被放在了​​/usr/local/bin​​​目录下,库函数被安装在了​​/usr/local/python​​目

Python的开发环境安装和配置非常的简单,如果是Linux系统则会默认集成安装了Python环境,Python的可执行文件被放在了​​/usr/local/bin​​​目录下,库函数被安装在了​​/usr/local/python​​目录中,接下来我们将使用源码的方式来编译安装一下Python解释器.

1.首先安装gcc编译器,和Python的相关依赖包.

[root@localhost ~]# yum -y install gcc zlib zlib-devel openssl openssl-devel libffi-devel

Package gcc-4.8.5-36.el7.x86_64 already installed and latest version
Package zlib-1.2.7-18.el7.x86_64 already installed and latest version
Package zlib-devel-1.2.7-18.el7.x86_64 already installed and latest version
Package 1:openssl-1.0.2k-16.el7.x86_64 already installed and latest version
Package 1:openssl-devel-1.0.2k-16.el7.x86_64 already installed and latest version
Package libffi-devel-3.0.13-18.el7.x86_64 already installed and latest version
Nothing to do

2.这里我们需要编译并安装Python解释器.

[root@localhost ~]# yum install -y readline
[root@localhost ~]# wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
[root@localhost ~]# tar xvzf Python-3.7.0.tgz -C /usr/src/python3.7
[root@localhost ~]# cd /usr/src/Python-3.7.0/
[root@localhost ~]# ./configure --prefix=/usr/local/python3.7
[root@localhost ~]# make && make altinstall

3.将Python头文件拷贝到标准目录,避免直接使用Python找不到所需的头文件.

[root@localhost ~]# cd /usr/local/include/python3.7/
[root@localhost ~]# cp -a ./* /usr/local/include/

4.接着我们备份一下旧版本的Python,并创建符号链接链接到新版本的Python上面.

[root@localhost ~]# cd /usr/bin/
[root@localhost bin]# mv python python.old
[root@localhost bin]# ln -s /usr/local/bin/python3.7 /usr/local/bin/python
[root@localhost bin]# rm -rf /usr/bin/python
[root@localhost bin]# cp /usr/local/bin/python3.7 /usr/bin/python

5.由于yum是用Python开发的,这里为了避免冲突要改掉他的配置.

[root@localhost ~]# vim /usr/bin/yum
#!/usr/bin/python2.7 ←此处将python改成python2.7

[root@localhost ~]# vim /usr/libexec/urlgrabber-ext-down
#!/usr/bin/python2.7 ←此处将python改成python2.7

6.最后测试python新版本是否生效了.

[root@localhost ~]# python
Python 3.7.0 (default, Apr 17 2018, 11:03:21)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

编译成pyc文件: 将我们写好的一个.py文件编译成.pyc文件.

[root@localhost ~]# ls -lh
total 4.0K
-rw-r--r--. 1 root root 43 Jan 8 02:22 test.py

[root@localhost ~]# python -m test.py
[root@localhost ~]# ls -lh
total 8.0K
-rw-r--r--. 1 root root 43 Jan 8 02:22 test.py
-rw-r--r--. 1 root root 163 Jan 8 02:25 test.pyo

编译成pyo文件: 将我们写好的一个.py文件编译成.pyo文件.

[root@localhost ~]# ls -lh
total 4.0K
-rw-r--r--. 1 root root 43 Jan 8 02:22 test.py

[root@localhost ~]# python -O -m test.py
[root@localhost ~]# ls -lh
total 8.0K
-rw-r--r--. 1 root root 43 Jan 8 02:22 test.py
-rw-r--r--. 1 root root 163 Jan 8 02:25 test.pyo

打包Python程序: 通过pip安装打包工具​​pyinstaller​​.

[root@localhost ~]# yum install -y python-pip

Package python2-pip-8.1.2-6.el7.noarch already installed and latest version
Nothing to do

[root@localhost ~]# pip install pyinstaller
Collecting pyinstaller
Downloading https://files.pythonhosted.org/pyinstall

2.准备测试文件,执行以下命令加密生成可执行文件.

[root@localhost ~]# ls -lh
total 4.0K
-rw-r--r--. 1 root root 43 Jan 8 02:22 test.py

[root@localhost ~]# pyinstaller -F ./test.py
44 INFO: PyInstaller: 3.4
44 INFO: Python: 2.7.5
44 INFO: Platform: Linux-3.10.0-862.el7.x86_64-x86_64-with-centos-7.5.1804-Core
44 INFO: wrote /root/test.spec
152 INFO: UPX is not available.
155 INFO: Extending PYTHONPATH with paths
......

3.然后查看,在当前目录会看到生成个3个目录,其中​​dist​​目录中的内容,就是生成的可执行文件.

[root@localhost ~]# ls -lh
total 8.0K
drwxr-xr-x. 3 root root 18 Jan 8 02:29 build
drwxr-xr-x. 2 root root 18 Jan 8 02:29 dist
-rw-r--r--. 1 root root 43 Jan 8 02:22 test.py
-rw-r--r--. 1 root root 786 Jan 8 02:29 test.spec

[root@localhost dist]# ls -lh
total 4.7M
-rwxr-xr-x. 1 root root 4.6M Jan 8 02:29 test

[root@localhost dist]# ./test
hello world

版权声明:本博客文章与代码均为学习时整理的笔记,文章 [均为原创] 作品,转载请 [添加出处] ,您添加出处是我创作的动力!





上一篇:Python 运算符与数据类型
下一篇:没有了
网友评论