发布Flask Web项目时,报错缺少Cryptography,于是尝试重新安装该库,但本机没有网络,只支持手动离线安装,尝试了pip、setup.py两种方式安装,结果都报错。。最后使用将安装包拷贝至本机(在其他电脑上安装的sitepackages里面的文件),重启后发现可以读取到该库

报错:RuntimeError

RuntimeError: 'cryptography' package is required for sha256_password or caching_sha2_password auth methods

Traceback (most recent call last)

· File "E:\software\python\lib\site-packages\flask\app.py", line 2213, in __call__

return self.wsgi_app(environ, start_response)

· File "E:\software\python\lib\site-packages\flask\app.py", line 2193, in wsgi_app

response = self.handle_exception(e)

· File "E:\software\python\lib\site-packages\flask\app.py", line 2190, in wsgi_app

response = self.full_dispatch_request()

· File "E:\software\python\lib\site-packages\flask\app.py", line 1486, in full_dispatch_request

rv = self.handle_user_exception(e)

· File "E:\software\python\lib\site-packages\flask\app.py", line 1484, in full_dispatch_request

rv = self.dispatch_request()

· File "E:\software\python\lib\site-packages\flask\app.py", line 1469, in dispatch_request

return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)

· File "E:\projects\WEB\HR_DL_DATA\hrproject\hrproject\V0809.01\app.py", line 7, in my_echart

conn1, cur1 = utils.get_conn()

· File "E:\projects\WEB\HR_DL_DATA\hrproject\hrproject\V0809.01\utils.py", line 8, in get_conn

conn = pymysql.connect(host='127.0.0.1',

· File "E:\software\python\lib\site-packages\pymysql\connections.py", line 352, in __init__

self.connect()

· File "E:\software\python\lib\site-packages\pymysql\connections.py", line 636, in connect

self._request_authentication()

· File "E:\software\python\lib\site-packages\pymysql\connections.py", line 933, in _request_authentication

auth_packet = _auth.caching_sha2_password_auth(self, auth_packet)

· File "E:\software\python\lib\site-packages\pymysql\_auth.py", line 265, in caching_sha2_password_auth

data = sha2_rsa_encrypt(conn.password, conn.salt, conn.server_public_key)

· File "E:\software\python\lib\site-packages\pymysql\_auth.py", line 143, in sha2_rsa_encrypt

raise RuntimeError(

RuntimeError: 'cryptography' package is required for sha256_password or caching_sha2_password auth methods

The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.

To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

· dump() shows all variables in the frame

· dump(obj) dumps all that's known about the object

这个报错提示是由于在您的项目中使用了 pymysql 库,而该库需要依赖 cryptography 来支持 sha256_password 或 caching_sha2_password 身份验证方法。由于您当前的环境中缺少 cryptography 包,所以导致了这个报错。

为了解决这个问题,您可以考虑以下几种方法:

手动安装 cryptography 包: 尝试手动从源代码构建并安装 cryptography 包,如前面所述。这将为 pymysql 提供所需的支持。 尝试其他 MySQL 连接库: 如果您只是为了连接 MySQL 数据库,您也可以尝试使用其他的 MySQL 连接库,例如 mysql-connector-python,它可能不需要 cryptography 依赖。 等待更新: 如前所述,您也可以等待 cryptography 更新并发布适用于您的配置的版本。

无论您选择哪种方法,都需要确保您的项目中安装了所需的依赖库,并且根据需要更新或调整代码以适应新的库版本。

cryptography需要单独安装吗?

是的,cryptography 包需要单独安装。它不是Python的内置标准库,因此需要使用 pip 命令来安装。

您可以使用以下命令来安装 cryptography 包:

pip install cryptography

安装完成后,您的项目应该就能够正常使用 pymysql 库并支持 sha256_password 或 caching_sha2_password 身份验证方法了。这将解决您之前报错的问题。

如果您没有网络连接,无法直接使用 pip 安装 cryptography 包,您可以从另一台有网络连接的计算机上下载 cryptography 包的安装文件(whl 文件),然后将其传输到您的目标计算机进行安装。

以下是具体步骤:

在有网络连接的计算机上,使用以下命令下载 cryptography 包的安装文件:

pip download cryptography

这将会下载 cryptography 包及其依赖的安装文件到当前目录。将下载得到的 .whl 文件复制到您的目标计算机上。 在目标计算机上,使用以下命令进行安装(假设您将 .whl 文件放在了同一个文件夹内):

pip install cryptography-3.3.2-cp36-abi3-win_amd64.whl

请注意,上述命令中的 .whl 文件名可能与您下载的文件名略有不同,您需要根据实际文件名进行调整。

通过这种方式,您可以将 cryptography 包的安装文件从有网络连接的计算机传输到目标计算机上,从而在没有网络连接的情况下进行安装。

我能直接把cryptography、cryptography-3.3.2.dist-info复制到sitepackages吗?

是的,您可以尝试将 cryptography 包及其相关的 .dist-info 文件夹复制到 Python 的 site-packages 目录中。这种方法有时可以在没有网络连接的情况下手动安装包。

请按照以下步骤操作:

从另一台有网络连接的计算机上下载 cryptography 包的 .whl 安装文件和 .dist-info 文件夹。确保您下载的是适用于您的 Python 版本和操作系统的版本。 将这些文件复制到目标计算机上。您可以将 cryptography 包的 .whl 文件和 .dist-info 文件夹一起复制到目标计算机上的 Python 的 site-packages 目录中。 打开命令行窗口,并导航到 Python 安装目录的 Scripts 文件夹中(例如:E:\software\python\Scripts)。 在命令行中运行以下命令来重新生成安装包的元数据缓存: python -m ensurepip

然后,尝试在命令行中运行您的应用程序,看看是否成功解决了报错问题。

请注意,尽管这种方法在某些情况下可能有效,但由于包的版本和依赖性问题,可能并不总是适用。在没有网络连接的情况下手动安装包时,请务必备份好您的现有环境,以防止出现意外问题。

如果这些步骤无法解决问题,可能需要尝试其他方法或在有网络连接的环境中进行操作。

我拷贝了cryptography 包及其相关的 .dist-info 文件夹复制到 Python 的 site-packages 目录中,发现还是读取不到这个包

如果您将cryptography包及其相关的.dist-info文件夹复制到了Python的site-packages目录中,但仍然无法读取该包,您可以尝试以下步骤:

确保您已经将cryptography包及其.dist-info文件夹复制到了正确的site-packages目录下。您可以通过运行以下命令来查找Python的site-packages路径: python -m site

在输出中查找USER_SITE和ENABLE_USER_SITE行,以确定site-packages的路径。 确保复制的文件具有正确的权限。有时,文件权限问题可能会影响包的加载。您可以尝试使用管理员权限复制文件,或者确保复制的文件具有与其他包相同的权限。 确保您的Python解释器版本与您从其他计算机下载的cryptography包的版本相匹配。如果版本不匹配,可能会出现问题。 在将文件复制到site-packages目录后,尝试重新生成安装包的元数据缓存。在命令行中运行以下命令: python -m ensurepip

重新启动您的Python解释器,然后尝试导入cryptography包并运行您的应用程序。

如果您仍然无法解决问题,可能需要考虑其他解决方法,例如尝试使用其他计算机进行安装并将生成的虚拟环境(包括site-packages目录)复制到您的目标计算机上。另外,您还可以考虑在有网络连接的环境中解决此问题,例如使用代理或移动到网络连接较好的地方进行操作。

重启了电脑可以读取到!!!

其他问题

怎么处理这个问题:Microsoft Windows [版本 10.0.17134.1]

© 2018 Microsoft Corporation。保留所有权利。

E:\software\python\Lib\site-packages\package\cr\cryptography-3.3.2>python setup.py install

E:\software\python\lib\site-packages\setuptools_init_.py:84: _DeprecatedInstaller: setuptools.installer and fetch_build_eggs are deprecated.

!!

********************************************************************************

Requirements should be satisfied by a PEP 517 installer.

If you are using pip, you can try `pip install --use-pep517`.

********************************************************************************

!!

dist.fetch_build_eggs(dist.setup_requires)

WARNING: The wheel package is not available.

running install

E:\software\python\lib\site-packages\setuptools_distutils\cmd.py:66: SetuptoolsDeprecationWarning: setup.py install is deprecated.

!!

********************************************************************************

Please avoid running ``setup.py`` directly.

Instead, use pypa/build, pypa/installer, pypa/build or

other standards-based tools.

See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details.

********************************************************************************

!!

self.initialize_options()

E:\software\python\lib\site-packages\setuptools_distutils\cmd.py:66: EasyInstallDeprecationWarning: easy_install command is deprecated.

!!

********************************************************************************

Please avoid running ``setup.py`` and ``easy_install``.

Instead, use pypa/build, pypa/installer, pypa/build or

other standards-based tools.

See https://github.com/pypa/setuptools/issues/917 for details.

********************************************************************************

!!

self.initialize_options()

running bdist_egg

running egg_info

writing src\cryptography.egg-info\PKG-INFO

writing dependency_links to src\cryptography.egg-info\dependency_links.txt

writing requirements to src\cryptography.egg-info\requires.txt

writing top-level names to src\cryptography.egg-info\top_level.txt

reading manifest file ‘src\cryptography.egg-info\SOURCES.txt’

reading manifest template ‘MANIFEST.in’

no previously-included directories found matching ‘docs_build’

warning: no previously-included files found matching ‘vectors’

warning: no previously-included files matching ‘*’ found under directory ‘vectors’

warning: no previously-included files matching ‘*’ found under directory ‘.github’

warning: no previously-included files found matching ‘release.py’

warning: no previously-included files found matching ‘.coveragerc’

warning: no previously-included files found matching ‘codecov.yml’

warning: no previously-included files found matching ‘.readthedocs.yml’

warning: no previously-included files found matching ‘dev-requirements.txt’

warning: no previously-included files found matching ‘rtd-requirements.txt’

warning: no previously-included files found matching ‘tox.ini’

warning: no previously-included files matching ‘*’ found under directory ‘.zuul.d’

warning: no previously-included files matching ‘*’ found under directory ‘.zuul.playbooks’

adding license file ‘LICENSE’

adding license file ‘LICENSE.APACHE’

adding license file ‘LICENSE.BSD’

adding license file ‘LICENSE.PSF’

adding license file ‘AUTHORS.rst’

writing manifest file ‘src\cryptography.egg-info\SOURCES.txt’

installing library code to build\bdist.win-amd64\egg

running install_lib

running build_py

running build_ext

generating cffi module ‘build\temp.win-amd64-cpython-310\Release\_padding.c’

already up-to-date

generating cffi module ‘build\temp.win-amd64-cpython-310\Release\_openssl.c’

already up-to-date

building ‘_openssl’ extension

=============================DEBUG ASSISTANCE=============================

If you are seeing a compilation error please try the following steps to

successfully install cryptography:

1) Upgrade to the latest pip and try again. This will fix errors for most

users. See: https://pip.pypa.io/en/stable/installing/#upgrading-pip

2) Read https://cryptography.io/en/latest/installation.html for specific

instructions for your platform.

3) Check our frequently asked questions for more information:

https://cryptography.io/en/latest/faq.html

=============================DEBUG ASSISTANCE=============================

error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build Tools”: https://visualstudio.microsoft.com/visual-cpp-build-tools/

E:\software\python\Lib\site-packages\package\cr\cryptography-3.3.2>

解决办法

问题出在你缺少 Microsoft Visual C++ 14.0 或更高版本的编译工具,这是安装 cryptography 包时出现的错误。为了解决这个问题,你需要安装 Microsoft C++ Build Tools。你可以按照以下步骤进行操作:

访问链接 https://visualstudio.microsoft.com/visual-cpp-build-tools/,下载并安装 Microsoft Visual C++ 14.0 或更高版本的编译工具。这将为你提供所需的编译环境,使得安装 cryptography 包时能够成功编译相关的 C 扩展。 安装完成后,重新运行你的安装命令。你可以使用 pip 命令代替 python setup.py install 进行安装,同时添加 --use-pep517 参数,以启用 PEP 517 安装程序: pip install . --use-pep517

如果上述步骤无法解决问题,你可以尝试执行以下操作:

确保你的 Python 环境和安装的包是兼容的,例如确保使用的是与 cryptography 包兼容的 Python 版本。更新你的 pip 版本到最新版本:pip install --upgrade pip。阅读 cryptography 包的官方安装文档,以获取针对你的操作系统和环境的具体安装指导:https://cryptography.io/en/latest/installation.html。

如果你仍然遇到问题,可以尝试在官方文档中提到的“DEBUG ASSISTANCE”部分提供的调试步骤,这可能会帮助你进一步解决安装问题。

好文阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。