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

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

This article specifically outlines steps that are necessary to install 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 specific task at hand. To allow for coexistence of multiple virtual Python environments on the same computer, free tool Miniconda was used in the following steps. There may exist other tools that provide similar functionality.

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

  1. Windows 11 operating system
  2. Miniconda environment – conda 23.9.0
  3. Python version 3.9

Please note that in the following, “|>” represents the Windows terminal prompt. Also, an alternate purely Python based methodology will be shared to accomplish the same in a companion blog.

  1. Install Miniconda after download from the following website. Follow the installation steps as prompted.  https://docs.conda.io/projects/miniconda/en/latest/miniconda-install.html
  2. Store the downloaded Miniconda3-latest-Windows-x86_64.exe for subsequent transfer to the intended offline computer.
  3. After Miniconda installation, create a virtual environment using the following syntax. This command needs to be issued at the Windows command prompt. If the command does not work at first, necessary %PATH% environment variable may have to be modified to include location of the conda executable. In the following syntax, hanaml_online is the name of the intended virtual environment and python=3.9 indicates the version of Python to be used within newly create virtual environment.
    |> conda create -y -n hanaml_online python=3.9
  4. Verify that intended virtual environment has been created.
    |> conda-env list​

    Newly created virtual environment should be listed in the output which should be as follows assuming that Miniconda was installed in its default location. Following output will change according to the installation directory of Miniconda.

    # conda environments:
    #
    base                     C:\Users\%USERNAME%\AppData\Local\miniconda3
    hanaml_online            C:\Users\%USERNAME%\AppData\Local\miniconda3\envs\hanaml_online
  5. Activate the newly created environment.
    |> activate hanaml_online ​
  6. Install hana_ml Python package from the  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.18 (main, Sep 11 2023, 14:09:26) [MSC v.1916 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 it will be documented.

  8. Deactivate hanaml_online virtual environment.
    (hanaml_online) |> conda deactivate​
  9. Install conda-pack package.
    |> conda install conda-pack​
  10. Package newly created virtual environment for subsequent transfer to the offline system using the following syntax.
    |> conda pack -n hanaml_online -o hanaml_offline.tar.gz
    Collecting packages...
    Packing environment at 'C:\\Users\\%USERNAME%\\AppData\\Local\\miniconda3\\envs\\hanaml_online' to 'hanaml_offline.tar.gz'
    [########################################] | 100% Completed |  9.0s
    ​
  11. Copy the following contents into a secure and suitable portable storage media which is permissible for use with the intended offline computer system.
    • Miniconda3-latest-Windows-x86_64.exe
    • hanaml_offline.tar.gz
  1. Connect the portable storage media with the contents as created in the step 11 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 Miniconda by double clicking on Miniconda3-latest-Windows-x86_64.exe
  3. Assuming default Miniconda installation, open a Windows terminal and change to the selected folder in the step 1 of this section.
  4. In order to create a virtual Python environment, named hanaml_offline, the commands as listed in the following are executed in the listed order. (Please note that following step can be accomplished in a number of ways, but the steps listed in the following were executed for the creation / documentation of this blog).
    • Copy hanaml_offline.tar.gz to the following folder
      C:\Users\%USERNAME%\AppData\Local\miniconda3\envs
    • Change directory to the same folder
      |> cd C:\Users\%USERNAME%\AppData\Local\miniconda3\envs
      |> mkdir hanaml_offline
      |> tar xvf hanaml_offline.tar.gz -C hanaml_offline
      ​
    • Check to see if the intended virtual environment has been created.
      |> conda-env list
      # conda environments:
      #
      Base	             C:\Users\%USERNAME%\AppData\Local\miniconda3
      hanaml_offline	     C:\Users\%USERNAME%\AppData\Local\miniconda3\envs\hanaml_offline
      ​
  5. After successful execution of the above stated step, SAP HANA ML package can be accessed within the new created Python virtual environment environment.
    |> activate hanaml_offline
    (hanaml_offline) |> python
    Python 3.9.18 (main, Sep 11 2023, 14:09:26) [MSC v.1916 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.23092701
    >>> import hana_ml.dataframe as dataframe
    >>> quit()
    
    (hanaml_offline) |> conda 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 on the topic 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-1-2/
如有侵权请联系:admin#unsafe.sh