Python3 offline installation of cryptography in Windows environment fails

When releasing the Flask Web project, an error was reported that Cryptography was missing, so I tried to reinstall the library, but there is no network on this machine, and only manual offline installation is supported. I tried two methods of pip and setup.py to install, but both reported errors. . Finally, copy the installation package to this machine (files in sitepackages installed on other computers), and find that the library can be read after restarting

Error: 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

This error message is due to the use of the pymysql library in your project, which needs to rely on cryptography to support sha256_password or caching_sha2_password Authentication method. This error is caused because the cryptography package is missing in your current environment.

In order to solve this problem, you can consider the following methods:

  1. Installing the cryptography package manually: Try building and installing the cryptography package manually from source, as described previously. This will provide the required support for pymysql.

  2. Try other MySQL connection libraries: If you just want to connect to the MySQL database, you can also try to use other MySQL connection libraries, such as mysql-connector-python, it may not be necessary cryptography depends.

  3. Waiting for an update: As mentioned, you can also wait for cryptography to be updated and released for your configuration.

Whichever method you choose, you will need to ensure that you have the required dependent libraries installed in your project, and update or adjust your code to accommodate new library versions as necessary.

Does cryptography need to be installed separately?

Yes, the cryptography package needs to be installed separately. It is not a built-in standard library of Python, so it needs to be installed using the pip command.

You can install the cryptography package with the following command:

pip install cryptography

Once installed, your project should be able to use the pymysql library and support the sha256_password or caching_sha2_password authentication methods. This will fix your previous error.

If you don’t have an internet connection to install the cryptography package directly using pip, you can download the cryptography package from another computer with an internet connection The installation file (whl file) of the , and then transfer it to your target computer for installation.

The following are the specific steps:

  1. On a computer with a network connection, use the following command to download the installation file for the cryptography package:
pip download cryptography
  1. This will download the cryptography package and its dependencies to the current directory. Copy the downloaded .whl file to your target computer.

  2. On the target computer, install with the following command (assuming you have the .whl file in the same folder):

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

Please note that the .whl file name in the above command may be slightly different from the file name you downloaded, you need to adjust according to the actual file name.

In this way, you can transfer the installation file of the cryptography package from a computer with a network connection to a target computer, thereby installing without a network connection.

Can I directly copy cryptography, cryptography-3.3.2.dist-info to sitepackages?

Yes, you could try copying the cryptography package and its associated .dist-info folder into Python’s site-packages directory. This method sometimes makes it possible to manually install packages without a network connection.

Follow the steps below:

  1. Download the .whl installer file and .dist-info folder for the cryptography package from another computer with a network connection. Make sure you download the correct version for your Python version and operating system.

  2. Copy these files to the target computer. You can copy the .whl file of the cryptography package along with the .dist-info folder to the Python’s site on the target machine -packages directory.

  3. Open a command line window and navigate to the Scripts folder of your Python installation (for example: E:\software\python\Scripts).

  4. Run the following command at the command line to regenerate the metadata cache for the installed package:

    python -m ensurepip
    
  5. Then, try running your application from the command line to see if that resolves the error.

Note that while this approach may work in some cases, it may not always be applicable due to package version and dependency issues. When manually installing packages without an internet connection, be sure to back up your existing environment in case unexpected issues arise.

If these steps don’t resolve the issue, you might need to try another method or work in an environment with a network connection.

I copied the cryptography package and its related .dist-info folder to Python’s site-packages directory, and found Still can’t read this package

If you copied the cryptography package and its associated .dist-info folder into Python’s site-packages directory, but still cannot read To get the package, you can try the following steps:

  1. Make sure you have copied the cryptography package and its .dist-info folder to the correct site-packages directory. You can find Python’s site-packages path by running:

    python -m site
    

    Look for the USER_SITE and ENABLE_USER_SITE lines in the output to determine the path to the site-packages.

  2. Make sure the copied files have the correct permissions. Sometimes, file permission issues can affect the loading of packages. You can try copying the file with admin privileges, or make sure the copied file has the same permissions as other packages.

  3. Make sure your version of the Python interpreter matches the version of the cryptography package you downloaded from your other computer. If the versions do not match, there may be problems.

  4. After copying the files to the site-packages directory, try rebuilding the metadata cache for the installed package. Run the following command at the command line:

    python -m ensurepip
    
  5. Restart your Python interpreter, then try to import the cryptography package and run your application.

If you still can’t solve the problem, you may want to consider other workarounds, such as trying to install using a different computer and copying the resulting virtual environment (including the site-packages directory) to your target computer. Alternatively, you might consider resolving the issue in an environment with a network connection, such as using a proxy or moving to a location with a better network connection.

Restart the computer to read it! ! !

Other questions

What to do about it: Microsoft Windows [Version 10.0.17134.1]

? 2018 Microsoft Corporation. all rights reserved.

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>

Solution

The problem is that you are missing compilation tools for Microsoft Visual C++ 14.0 or later, this is an error when installing the cryptography package. To fix this, you need to install Microsoft C++ Build Tools. You can follow these steps:

  1. Visit the link https://visualstudio.microsoft.com/visual-cpp-build-tools/ to download and install the build tools for Microsoft Visual C++ 14.0 or later. This will give you the build environment needed to successfully compile the associated C extensions when installing the cryptography package.

  2. After the installation is complete, re-run your install command. You can use the pip command instead of python setup.py install to install, and add the --use-pep517 parameter to enable the PEP 517 installer :

    pip install . --use-pep517
    
  3. If the steps above don’t resolve the issue, you can try the following:

    • Make sure your Python environment and installed packages are compatible, for example make sure you are using a version of Python that is compatible with the cryptography package.
    • Update your version of pip to the latest version: pip install --upgrade pip.
    • Read the official installation documentation of the cryptography package for specific installation instructions for your operating system and environment: https://cryptography.io/en/latest/installation.html.

If you still encounter problems, you can try the debugging steps provided in the “DEBUG ASSISTANCE” section mentioned in the official documentation, which may help you further troubleshoot your installation.