Python源设置\国内源地址\打包成exe\pip升级

#########国内源地址##############

腾讯开源镜像站 http://mirrors.cloud.tencent.com/pypi/simple
豆瓣 http://pypi.douban.com/simple/
网易开源镜像站 http://mirrors.163.com/
搜狐开源镜像 http://mirrors.sohu.com/
华为镜像源 https://mirrors.huaweicloud.com/
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
浙江大学开源镜像站 http://mirrors.zju.edu.cn/

#########临时设置源##############

pip install XXXX -i http://pypi.douban.com/simple/

#########本地设置源##############

【我自己用的豆瓣,亲测可用】

# 查看设置
python -m pip config list
# 设置源
python -m pip config set global.index-url 'http://pypi.douban.com/simple/'
# 设置信任镜像
python -m pip config set install.trusted-host 'pypi.douban.com'
#########pip升级##############

python -m pip install --upgrade pip

#########打包成 exe方法1############

1、pip install pyinstaller

2、pyinstaller -F AllSelenium.py       

3、pyinstaller --noconsole your_script.py  创建无控制台的程序

4、pyinstaller --noconsole --onefile your_script.py

 

然后生成exe           exe要与静态资源配合,根据设置的资源路径。放在同一目录下最好

 

#########打包成 exe方法2############

基于pycharm

1、 pip install auto-py-to-exe

2、启动:auto-py-to-exe

 以上2种打包方式  打包时如果在代码中使用了多进程,出现的GUI可能出现N多窗口,此时加入multiprocessing.freeze_support() 原因:

在 Windows 上,multiprocessing 模块使用了一个主进程和多个子进程来执行并行任务。然而,在打包成可执行文件后,Windows 的执行方式会发生变化,可能会导致创建额外的子进程,引起一些不正常的行为。

为了解决这个问题,可以在主程序的入口点(通常是在 if __name__ == '__main__': 语句中)调用 multiprocessing.freeze_support() 函数。该函数会检查当前程序是否是被冻结(frozen)的,如果是,则初始化 Windows 的支持,并确保多进程代码能够正常运行。

 

另外

 

 

 

可能有路径问题问题,脚本添加:


import os
import sys


def app_path():
if hasattr(sys, 'frozen'):
  return os.path.dirname(sys.executable) # 使用pyinstaller打包后的exe目录
return os.path.dirname(__file__) # 没打包前的py目录

posted @ 2023-01-05 09:40  王亚锋  阅读(219)  评论(0编辑  收藏  举报