博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux 下 Python3 使用 PyInstaller 打包 py 脚本
阅读量:4229 次
发布时间:2019-05-26

本文共 2583 字,大约阅读时间需要 8 分钟。

为什么需要打包呢?直接运行单个的py文件它不香吗?起因在于我其中一台Linux机器连不上外网(或者说只能通过有限的方式上外网),连不上外网就用不了pip,用不了pip就安装不了一些相关的依赖包,缺少依赖包运行一些Python程序就会报错。而打包的方式可以把程序所需要的环境一块打包,这样的话,就算另外一台机器没有相关依赖包,甚至是没有Python环境,也照样可以运行打包后的可执行文件。

首先下载PyInstaller,我这儿下载的是PyInstaller-3.6.tar.gz版本,点击链接可以直接进行下载:

然后进行解压,进入解压目录运行python3 setup.py install(前提是你已经安装好了Python3):

[root@master looking]# tar -zxvf PyInstaller-3.6.tar.gztotal 3492-rw-r--r--.  1 root root   11226 Apr 21 09:42 batch_audio_recognition.py-rw-r--r--.  1 root root    4022 Apr 21 09:33 Config.py-rw-r--r--.  1 root root     511 Aug 16  2019 memorymonitor.py-rw-r--r--.  1 root root     950 Aug 16  2019 memorymonitor.pycdrwxr-xr-x.  2 root root      42 Aug 16  2019 __pycache__drwxrwxr-x. 11 git  git     4096 Apr 21 11:15 PyInstaller-3.6-rw-r--r--.  1 root root 3539593 Apr 21 10:16 PyInstaller-3.6.tar.gz
[root@master looking]# cd PyInstaller-3.6[root@master PyInstaller-3.6]# python3 setup.py install

打包单个文件安装如下命令执行:

[root@master PyInstaller-3.6]# python3 pyinstaller.py -F batch_audio_recognition.py 172 INFO: PyInstaller: 3.6172 INFO: Python: 3.6.8174 INFO: Platform: Linux-3.10.0-957.27.2.el7.x86_64-x86_64-with-centos-7.6.1810-Core174 INFO: wrote /root/looking/PyInstaller-3.6/batch_audio_recognition/batch_audio_recognition.spec177 INFO: UPX is not available.179 INFO: Extending PYTHONPATH with paths['/root/looking/PyInstaller-3.6', '/root/looking/PyInstaller-3.6/batch_audio_recognition']179 INFO: checking Analysis180 INFO: Building Analysis because Analysis-00.toc is non existent180 INFO: Initializing module dependency graph...

就会生成一个以文件 batch_audio_recognition.py 的文件名为名字的目录,进入目录后可以看到有dist目录:

[root@master PyInstaller-3.6]# cd batch_audio_recognition[root@master batch_audio_recognition]# lltotal 4-rw-r--r--. 1 root root 917 Apr 21 11:15 batch_audio_recognition.specdrwxr-xr-x. 3 root root  37 Apr 21 11:15 builddrwxr-xr-x. 2 root root  37 Apr 21 11:16 dist

进入dist目录以后,就可以看到打包好的可执行文件 batch_audio_recognition,当然,这个文件是没有带后缀的。如果想要在其他Linux环境下运行这个可执行文件,使用下面这种格式的命令(记得给文件添加可执行权限,不然可能会报下面这种错):

root@debian:~/looking# ./batch_audio_recognition-bash: ./batch_audio_recognition: Permission deniedroot@debian:~/looking# chmod 777 batch_audio_recognitionroot@debian:~/looking# ./batch_audio_recognition {"code":0, "message":"success","requestId":73*****17}73*****17 识别成功!

我后面发现,pip也可以用下面的方式使用代理访问外网来安装依赖包,还是我有点孤陋寡闻了,如果代理速度比较慢,也还可以使用“代理+镜像”的方式。 

root@debian:~# pip3 --proxy=http://***.**.**.***:8080 install tencentcloud-sdk-pythonroot@debian:~/looking# pip3 --proxy=http://***.**.**.***:8080 install -i https://pypi.tuna.tsinghua.edu.cn/simple tencentcloud-sdk-python

 

转载地址:http://ldjqi.baihongyu.com/

你可能感兴趣的文章
POJ 3363
查看>>
[LeetCode] 849. Maximize Distance to Closest Person @ python
查看>>
axi总线介绍
查看>>
Linux内核中ioremap映射的透彻理解
查看>>
ffs的另外一种实现方法
查看>>
strtol的用法
查看>>
工作队列的使用
查看>>
让vim显示空格,及tab字符 vim 多行注释
查看>>
利用mmc_test.c研究mmc模块
查看>>
tasklet、wait_queue、completion、work_queue用法总结
查看>>
int (*func(int)) (int *,int)
查看>>
在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel
查看>>
Linux内核同步机制API函数:宏:spin_lock_init ( )
查看>>
driver_register 理解
查看>>
copy_from_user && copy_to_user
查看>>
device_register
查看>>
Android上C++对象的自动回收机制分析
查看>>
从spin_lock到spin_lock_irqsave
查看>>
sdio 驱动
查看>>
vim 常用用法
查看>>