配置pycharm 一键安装 requirements.txt,一键生成requirements.txt

如上配置

打开项目,在requirements.txt上点右键,就可以安装了。

安装效果如下:

可以看出运行的命令是   C:\Python\Python36/scripts/pip install -r requirements.txt



安装requirement.txt
pip install -r requirement.txt

 

生成requirement.txt
pip freeze > requirements.txt

自己配置一下吧。

不行,配置出来,执行命令也对,但是windows下,就无法生成。

你不行,我来。放在项目根目录,执行一下就可以了。

 

复制代码
import os
import platform
import sys
import subprocess

# 找到当前目录
project_root = os.path.dirname(os.path.realpath(__file__))
# project_root = os.path.realpath(__file__)
print('当前目录' + project_root)

# 不同的系统,使用不同的命令语句

if platform.system() == 'Linux':
    command = sys.executable + ' -m pip freeze > ' + project_root + '/requirements.txt'
if platform.system() == 'Windows':
    command = '"' + sys.executable + '"' + ' -m pip freeze > "' + project_root + '\\requirements.txt"'
# # 拼接生成requirements命令
print(command)
#
# 执行命令。
# os.system(command)  #路径有空格不管用
os.popen(command)   #路径有空格,可用
# subprocess.call(command, shell=True)  #路径有空格,可用
复制代码

 

更新,windows和linux下均可以正常生成。

更新,windows下带有空格的目录,可以正常生成。

 

作者:上官飞鸿

出处:https://www.cnblogs.com/jackadam/p/8302534.html

版权:本作品采用「知识共享-署名-非商业性-禁止演绎(CC-BY-NC-ND)」许可协议进行许可。

posted @   上官飞鸿  阅读(22847)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
more_horiz
keyboard_arrow_up light_mode palette
选择主题
点击右上角即可分享
微信分享提示