金融量化学习---Python, MySQL, Pandas

这里用来记录一些在金融领域,尤其是银行相关的资金、债券、票据中应用到的数据管理与分析, 编程等心得或笔记,以及个人的一点小小兴趣(易经八卦、藏密禅修)等

导航

Python 中的 requirement.txt


Python 也需要维护项目相关的依赖包。通常我们会在项目的根目录下放置一个 requirement.txt 文件,用于记录所有依赖包和它的确切版本号。

requirement.txt 的内容长这样:

alembic==1.0.10
appnope==0.1.0
astroid==2.2.5
attrs==19.1.0
backcall==0.1.0
bcrypt==3.1.6
bleach==3.1.0
cffi==1.12.3
Click==7.0
decorator==4.4.0
defusedxml==0.6.0
entrypoints==0.3

...

1. 如何使用?requirement.txt导出

那么 requirement.txt 究竟如何使用呢?
当我们要把环境中的依赖写入 requirement.txt 中时,可以借助 freeze 命令:
pip freeze >requirements.txt

当我们拿到一个项目时,首先要在项目运行环境安装 requirement.txt 所包含的依赖:
pip install -r requirement.txt

2.环境混用怎么办?

在导出依赖到 requirement.txt 文件时会有一种尴尬的情况。
你的本地环境不仅包含项目 A 所需要的依赖,也包含着项目 B 所需要的依赖。此时我们要如何做到只把项目 A 的依赖导出呢?
pipreqs 可以通过扫描项目目录,帮助我们仅生成当前项目的依赖清单。

Usage:
    pipreqs [options] <path>

Options:
    --use-local           Use ONLY local package info instead of querying PyPI
    --pypi-server <url>   Use custom PyPi server
    --proxy <url>         Use Proxy, parameter will be passed to requests library. You can also just set the
                          environments parameter in your terminal:
                          $ export HTTP_PROXY="http://10.10.1.10:3128"
                          $ export HTTPS_PROXY="https://10.10.1.10:1080"
    --debug               Print debug information
    --ignore <dirs>...    Ignore extra directories
    --encoding <charset>  Use encoding parameter for file open
    --savepath <file>     Save the list of requirements in the given file
    --print               Output the list of requirements in the standard output
    --force               Overwrite existing requirements.txt
    --diff <file>         Compare modules in requirements.txt to project imports.
    --clean <file>        Clean up requirements.txt by removing modules that are not imported in project.

通过以下命令安装:

pip install pipreqs
运行:
pipreqs ./
或者
pipreqs --use-local ./

如果遇到报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence

需要上--encoding=utf8 如下
pipreqs --use-local --encoding=utf8 --force .
如果已经存在requirements.txt,需要加上 --force 覆盖之

3.wget下载requirements.txt里的文件

Install prerequisite libraries by downloading the requirements.txt file (it contains the library version numbers):
wget https://raw.githubusercontent.com/dataprofessor/eda-app/main/requirements.txt

4.使用pip安装requirements.txt里的文件

pip install -r requirements.txt

posted on 2023-06-01 10:35  chengjon  阅读(617)  评论(0编辑  收藏  举报