2025年3月24日 星期一 甲辰(龙)年 月廿三 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

python导出依赖

时间:10-25来源:作者:点击数:45

最近在导出 Python 项目依赖的时候遇到了些小问题。

导出依赖的方法:

第一种,通过

  • pip freeze > requirements.txt

导出,不过这种方法有个缺点,它会导出环境中的所有库。比如我导出了一个小项目,它包含这么多依赖:

  • certifi==2016.2.28
  • cycler==0.10.0
  • kiwisolver==1.1.0
  • matplotlib==3.1.0
  • numpy==1.16.4
  • pyparsing==2.4.0
  • python-dateutil==2.8.0
  • six==1.12.0
  • wincertstore==0.2

很多情况我们并不需要,因此推荐下面的方法:

第二种,安装 pipreqs,然后在项目目录下运行

  • pipreqs .

这种方法只会导出该项目所需要的库。上面的项目通过 pipreqs 导出之后只剩下了这么一点点:

  • matplotlib==3.1.0
  • numpy==1.16.4

比较推荐。

另外,在 cmd 中运行的时候遇到了这样的错误:

  • (test) C:\WINDOWS\system32>conda deactivate
  • Fatal Python error: init_sys_streams: can't initialize sys standard streams
  • LookupError: unknown encoding: 65001
  • Current thread 0x0000360c (most recent call first):

这是因为修改了操作系统编码所导致的。在 CMD 中设置环境变量:

  • set PYTHONIOENCODING=UTF-8

即可。如果是在 Powershell 中运行的话,需要

  • $env:PYTHONIOENCODING = "UTF-8"
参考资料

Python 2.7 : LookupError: unknown encoding: cp65001 duplicate

然而里面的这个方法:

Also you can try to install win-unicode-console with pip:
  • pip install win-unicode-console
Then reload your terminal and try to execute pip --version
However you can follow suggestions from Windows cmd encoding change causes Python crash answer because you have same problem.

好像并没有用,所以可能要把上面的环境变量写死。另外 VSCode 中好像没有遇到过这个问题。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门