Cloning a Functional Python Virtual Environment to an Offline System – 2/2
2023-11-29 01:14:5 Author: blogs.sap.com(查看原文) 阅读量:5 收藏

Certain operational systems used for machine learning tasks might not be connected to internet due to security concerns and privacy reasons. Such systems may operate offline to prevent potential cyber threats and / or unauthorized access. Natural question that arises is that how does one create and / or deploy developed machine learning models on such offline systems? This technical article documents steps needed to clone a Python based virtual machine learning environment from an online computer to such an offline system.

This article outlines steps that are necessary to clone a hana_ml package based virtual environment on a computer that is not connected to the Internet. The steps are demonstrative, and some computer environments may require different additional Python packages to enable a specific task at hand.

Steps as listed in the following have been tested for the following:

  1. Windows 11 operating system
  2. Python version 3.9

Please note that in the following, “|>” represents the Windows terminal prompt. In an earlier blog, a Miniconda based approach was presented to help solve the same problem. In this blog a Python based approach is documented for programmers who do not use Miniconda for Python based application development.

Also, note that for the following steps to succeed, Python and pip versions must be the same on both online as well as offline computers.

  1. Install needed version of Python after download from the following website. Follow the installation steps as prompted.  https://www.python.org/downloads/
    • In this case  python-3.9.0-amd64.exe was used.
    • If there are multiple versions of Python on the same system, the %PATH% environment system variable would have to be modified to ensure that the desired version of Python is executed for the following steps.
  2. Store the downloaded Python installer for subsequent transfer to the intended offline computer.
  3. After Python installation, create a virtual environment following the steps as listed below. The listed steps are executed at the Windows command prompt. If the commands do not work at first, necessary %PATH% environment variable may have to be modified to include location of the desired version of Python and pip executables. In the following syntax, hanaml_online is the name of the intended virtual environment on the offline system.
    |> pip install virtualenv
    |> pip install virtualenvwrapper-win
    |> cd C:\Users\%USERBANE%\
    |> mkdir envs
    |> cd envs
    |> virtualenv hanaml_online
    
  4. Verify that intended virtual environment has been created.
    |> lsvirtualenv
    

    Newly created virtual environment should be listed in the output.

    dir /b /ad "C:\Users\%USERNAME%\envs"
    ==============================================================================
    hanaml_online
    
  5. Activate the newly created environment.
    |> hanaml_online\Scripts\activate
  6. Install hana_ml Python package from PyPI public repository into the newly created hanaml_online virtual environment.
    (hanaml_online) |> pip install hana_ml ​
  7. Verify that the hana_ml package can be imported without error in Python.
    (hanaml_online) |> python
    Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import hana_ml
    ModuleNotFoundError: No module named 'shapely'
    >>> print(hana_ml.__version__)
    2.18.23111400
    >>> import hana_ml.dataframe as dataframe
    >>> quit()
    
    (hanaml_online) |>

    *Note: Python package shapely is missing. This omission is intentional. In a subsequent blog rationale for this omission and how to remedy the situation will be shared.

  8. Create the files requiremetns.txt and hana_ml_dependencies.tar.gz which will be required to clone the newly created virtual environment on the computer without internet connection.
    (hanaml_online) |> pip freeze > requirements.txt
    (hanaml_online) |> mkdir dependencies
    (hanaml_online) |> pip download -r requirements.txt -d "./dependencies"​
    (hanaml_online) |> tar cvfz dependencies.tar.gz dependencies
  9. Deactivate hanaml_online virtual environment.
    (hanaml_online) |> deactivate
  10. Copy the following contents into a secure and suitable portable storage media which is permissible for use with the intended offline computer system.
    • python-3.9.0-amd64.exe
    • hana_ml_dependencies.tar.gz
    • requirements.txt
  1. Connect the portable storage media with the contents as created in the step 10 of the previous section to the offline computer system and copy the contents into a suitable folder.
  2. Navigate the selected folder in the previous step and install Python by double clicking on python-3.9.0-amd64.exe
  3. In order to clone the desired virtual Python environment, named hanaml_offline, the commands are executed as listed in the following in the listed order. (Please note that it is possible that following step can be accomplished in a different way, but the steps listed in the following were executed for the creation / documentation of this blog).
    • Open a new Windows terminal. Please note that the default working directory after creation of a new Windows terminal is C:\Users\%USERNAME%
    • Create the desired virtual environment and activate.
      |> mkdir evns
      |> cd evns
      |> virtualenv hanaml_offline
      |> hanaml_offline\Scripts\activate
    • Change directory to the same folder
      |> cd C:\Users\%USERNAME%\evns
    • Copy the file dependencies.tar.gz file into the same folder.
    • Extract the contents of the shared archive by issuing the following command.
      (hanaml_offline) |> tar zxvf dependencies.tar.gz
    • Successful execution of the above stated command will create a folder with the same name. The folder name will be dependencies.
    • In the command prompt window, move into the same newly created dependencies folder. Also, copy requirements.txt file into the same folder.
    • Issue the following command to install necessary dependencies and SAP HANA ML Python package including its dependencies.
      (hanaml_offline) |> pip install --no-index --find-links .\ -r requirements.txt​
  4. After successful execution of the above stated step, SAP HANA ML package can be accessed within the newly created Python virtual environment in the offline system.
    (hanaml_offline) |> python
    Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import hana_ml
    ModuleNotFoundError: No module named 'shapely'
    >>> print(hana_ml.__version__)
    2.18.23111400
    >>> import hana_ml.dataframe as dataframe
    >>> quit()
    
    (hanaml_offline) |> deactivate

Note: In some of the above listed statements, actual username has been replaced with %USERNAME%.

Acknowledgment: I would like to acknowledge valuable discussions with Andreas Forster without which this blog would not have been as comprehensive and informative.

文章来源: https://blogs.sap.com/2023/11/28/cloning-a-functional-python-virtual-environment-to-an-offline-system-2-2/
如有侵权请联系:admin#unsafe.sh