配置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下帶有空格的目錄,可以正常生成。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM