命令:pip3 freeze > requirements.txt
1、安装pipreqs:pip install pipreqs
2、进入当前项目目录下,导包:pipreqs ./ (导包完成会生成一个requirements.txt文件)
3、安装依赖的时候使用:pip install -r requirements.txt
如果遇到编码错误UnicodeDecodeError,则将指定编码为utf8:pipreqs ./ --encoding=utf8
Traceback (most recent call last):
File "d:\program files (x86)\python\python36-32\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "d:\program files (x86)\python\python36-32\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:\Program Files (x86)\Python\Python36-32\Scripts\pipreqs.exe\__main__.py", line 7, in
File "d:\program files (x86)\python\python36-32\lib\site-packages\pipreqs\pipreqs.py", line 470, in main
init(args)
File "d:\program files (x86)\python\python36-32\lib\site-packages\pipreqs\pipreqs.py", line 409, in init
follow_links=follow_links)
File "d:\program files (x86)\python\python36-32\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 812: illegal multibyte sequence
如果遇到SSLError,则需要修改pipreqs.py文件中的request.get参数,增加verify=False:
E:\workfolder\.git\Buff-Android-APP-UIautomation\ComNeteaseBuff163Django>pipreqs --encoding=utf8
Traceback (most recent call last):
File "d:\program files (x86)\python\python36-32\lib\site-packages\urllib3\contrib\pyopenssl.py", line 438, in wrap_socket
cnx.do_handshake()
File "d:\program files (x86)\python\python36-32\lib\site-packages\OpenSSL\SSL.py", line 1907, in do_handshake
self._raise_ssl_error(self._ssl, result)
File "d:\program files (x86)\python\python36-32\lib\site-packages\OpenSSL\SSL.py", line 1639, in _raise_ssl_error
_raise_current_error()
File "d:\program files (x86)\python\python36-32\lib\site-packages\OpenSSL\_util.py", line 54, in exception_from_error_queue
raise exception_type(errors)
OpenSSL.SSL.Error: [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\program files (x86)\python\python36-32\lib\site-packages\urllib3\connectionpool.py", line 594, in urlopen
self._prepare_proxy(conn)
File "d:\program files (x86)\python\python36-32\lib\site-packages\urllib3\connectionpool.py", line 810, in _prepare_proxy
conn.connect()
File "d:\program files (x86)\python\python36-32\lib\site-packages\urllib3\connection.py", line 326, in connect
ssl_context=context)
File "d:\program files (x86)\python\python36-32\lib\site-packages\urllib3\util\ssl_.py", line 325, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "d:\program files (x86)\python\python36-32\lib\site-packages\urllib3\contrib\pyopenssl.py", line 445, in wrap_socket
raise ssl.SSLError('bad handshake: %r' % e)
ssl.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\program files (x86)\python\python36-32\lib\site-packages\requests\adapters.py", line 440, in send
timeout=timeout
File "d:\program files (x86)\python\python36-32\lib\site-packages\urllib3\connectionpool.py", line 630, in urlopen
raise SSLError(e)
urllib3.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\program files (x86)\python\python36-32\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "d:\program files (x86)\python\python36-32\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:\Program Files (x86)\Python\Python36-32\Scripts\pipreqs.exe\__main__.py", line 7, in
File "d:\program files (x86)\python\python36-32\lib\site-packages\pipreqs\pipreqs.py", line 470, in main
init(args)
File "d:\program files (x86)\python\python36-32\lib\site-packages\pipreqs\pipreqs.py", line 432, in init
pypi_server=pypi_server)
File "d:\program files (x86)\python\python36-32\lib\site-packages\pipreqs\pipreqs.py", line 188, in get_imports_info
"{0}{1}/json".format(pypi_server, item), proxies=proxy)
File "d:\program files (x86)\python\python36-32\lib\site-packages\requests\api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "d:\program files (x86)\python\python36-32\lib\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "d:\program files (x86)\python\python36-32\lib\site-packages\requests\sessions.py", line 502, in request
resp = self.send(prep, **send_kwargs)
File "d:\program files (x86)\python\python36-32\lib\site-packages\requests\sessions.py", line 612, in send
r = adapter.send(request, **kwargs)
File "d:\program files (x86)\python\python36-32\lib\site-packages\requests\adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",)
修改以下内容:
response = requests.get(
"{0}{1}/json".format(pypi_server, item), proxies=proxy)
改为:
response = requests.get(
"{0}{1}/json".format(pypi_server, item), proxies=proxy,verify=False) # 当pipreqs报SSLError时将verify设置为False