方法一:使用pip freeze > requirements.txt
Microsoft Windows [版本 10.0.17763.914]
(c) 2018 Microsoft Corporation。保留所有权利。
D:\dev\dev_py\deving>
D:\dev\dev_py\deving>
D:\dev\dev_py\deving>python
Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
D:\dev\dev_py\deving>python -m pip freeze > requirements.txt
方法二:使用pipreqs
D:\dev\dev_py\deving>
D:\dev\dev_py\deving>
D:\dev\dev_py\deving>python -m pip install pipreqs
Looking in indexes: http://pypi.douban.com/simple
Collecting pipreqs
Downloading http://pypi.doubanio.com/packages/9b/83/b1560948400a07ec094a15c2f64587b70e1a5ab5f7b375ba902fcab5b6c3/pipreqs-0.4.10-py2.py3-none-any.whl
Requirement already satisfied: docopt in c:\users\xh\anaconda3\lib\site-packages (from pipreqs) (0.6.2)
Collecting yarg (from pipreqs)
Downloading http://pypi.doubanio.com/packages/8b/90/89a2ff242ccab6a24fbab18dbbabc67c51a6f0ed01f9a0f41689dc177419/yarg-0.1.9-py2.py3-none-any.whl
Requirement already satisfied: requests in c:\users\xh\anaconda3\lib\site-packages (from yarg->pipreqs) (2.19.1)
Requirement already satisfied: idna<2.8,>=2.5 in c:\users\xh\anaconda3\lib\site-packages (from requests->yarg->pipreqs) (2.7)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\xh\anaconda3\lib\site-packages (from requests->yarg->pipreqs) (2019.6.16)
Requirement already satisfied: urllib3<1.24,>=1.21.1 in c:\users\xh\anaconda3\lib\site-packages (from requests->yarg->pipreqs) (1.23)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\users\xh\anaconda3\lib\site-packages (from requests->yarg->pipreqs) (3.0.4)
Installing collected packages: yarg, pipreqs
Successfully installed pipreqs-0.4.10 yarg-0.1.9
WARNING: You are using pip version 19.2.1, however version 20.0.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
D:\dev\dev_py\deving>pipreqs ./
Traceback (most recent call last):
File "c:\users\xh\anaconda3\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\users\xh\anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\XH\Anaconda3\Scripts\pipreqs.exe\__main__.py", line 9, in
File "c:\users\xh\anaconda3\lib\site-packages\pipreqs\pipreqs.py", line 470, in main
init(args)
File "c:\users\xh\anaconda3\lib\site-packages\pipreqs\pipreqs.py", line 409, in init
follow_links=follow_links)
File "c:\users\xh\anaconda3\lib\site-packages\pipreqs\pipreqs.py", line 122, in get_all_imports
contents = f.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 113: illegal multibyte sequence
D:\dev\dev_py\deving>pipreqs ./ --encoding utf-8
WARNING: Requirements.txt already exists, use --force to overwrite it
D:\dev\dev_py\deving>pipreqs ./ --encoding utf-8 --force
INFO: Successfully saved requirements file in ./requirements.txt
D:\dev\dev_py\deving>
方法一和方法二,都可以生成requirements.txt。方法一是会将python环境中的依赖包全都加入;方法二只将项目依赖包加入。
生成 requirements.txt 后,需要安装 requirements.txt :pip install -r requirements.txt.
以上。