Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Install TensorFlow/Keras and PyTorch via Anaconda, and Migrate Virtual Environments to Offline Systems

Tech 1

System Environment and Required Packages

CentOS 7
Anaconda3-5.1.0
TensorFlow 2.1.0
Keras 2.3.1
PyTorch 1.8.0 (CPU)
TorchVision 0.9.0

Install Anaconda, TensorFlow/Keras on Internet-Connected Systems

2.1 Anaconda Installation on Linux

For Linux systems (e.g., CentOS7 on VMware), ensure VMware-related services are running before restarting. If Anaconda wasn't added to your system PATH during installation, manually update it by editing your shell config:

vi ~/.bashrc
export PATH="$PATH:/opt/anaconda3/bin:/opt/anaconda3/condabin"

Save and reload the config with source ~/.bashrc.

2.2 Essential Conda Commands

# Activate virtual environment
source activate tf_env  # Linux
activate tf_env         # Windows

# Deactivate virtual environment
source deactivate       # Linux
deactivate              # Windows

# List all virtual environments
conda info --envs

# List packages in current environment
conda list

# Search for a specific package
conda list pymysql

# Clear conda cache
conda clean --all

# Create new virtual environment with specific Python version
conda create -n tf_env python=3.6.4

# Clone an existing environment
conda create -n tf_clone --clone tf_env

# Remove a virtual environment
conda remove -n tf_env --all

# Install package via conda (note: pip is often more reliable for ML libraries)
conda install tensorflow=2.1.0

# Upgrade a package
conda upgrade pandas

2.3 Configure Pip for Faster Downloads

To use a domestic mirrer (e.g., Tsinghua) for pip packages:

# Set mirror via command line
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

For Windows, manually create or edit C:\Users\<YourUsername>\AppData\Roaming\pip\pip.ini:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn

2.4 Set Up TensorFlow/Keras Environment

Create a deidcated environment and install required libraries:

conda create -n tf_env python=3.6.4
source activate tf_env  # Linux / activate tf_env for Windows

# Install TensorFlow, Keras, and dependencies
pip install tensorflow==2.1.0
pip install keras==2.3.1
pip install pandas==0.22.0 matplotlib==2.1.2 scikit-learn==0.19.1 scikit-image==0.13.1 seaborn==0.8.1

Pip is recommended here over conda due to fewer download compatibility issues.

2.5 Installl PyTorch in a Separate Environment

conda create -n pt_env python=3.6.4
source activate pt_env  # Linux / activate pt_env for Windows

# Install CPU-only PyTorch and TorchVision (compatible with Python 3.6.4)
pip install torch==1.8.0 torchvision==0.9.0

# Install supplementary packages
pip install pandas==0.22.0 matplotlib==2.1.2 scikit-learn==0.19.1 scikit-image==0.13.1 seaborn==0.8.1

Avoid modifying default conda mirrors to prevent installation failures.

Migrate Virtual Environments to Offline Systems

Ensure both source and target systems have the same Anaconda version installed.

3.1 Prepare Environment on Source Machine

  1. Activate the target environment and install conda-pack:
source activate tf_env
conda install -c conda-forge conda-pack
  1. Package the environment into a compressed file:
conda pack -n tf_env -o tf_env.tar.gz

3.2 Deploy on Offline System (Linux)

  1. Copy tf_env.tar.gz to the target machine's Anaconda envs directory (e.g., /opt/anaconda3/envs).
  2. Extract the package:
mkdir -p /opt/anaconda3/envs/tf_env
tar -xzf tf_env.tar.gz -C /opt/anaconda3/envs/tf_env
  1. Activate the environment and finalize setup:
source activate tf_env
conda-unpack

3.3 Deploy on Offline System (Windows)

  1. Copy tf_env.tar.gz to C:\ProgramData\Anaconda3\envs.
  2. Extract using a compatible tool (avoid Windows built-in extractor):
mkdir C:\ProgramData\Anaconda3\envs\tf_env
tar -xzf tf_env.tar.gz -C C:\ProgramData\Anaconda3\envs\tf_env
  1. Activate and finalize:
activate tf_env
conda-unpack

Note: Windows environment migration may cause runtime errors with TensorFlow projects. For reliable results, use Linux-based migration or install directly online on Windows.

Fix h5py Compatibility Issue

When loading Keras models saved in .h5 format with h5py 3.x, you may encounter an AttributeError: 'str' object has no attribute 'decode'. Resolve this by downgrading h5py:

pip install 'h5py==2.10.0' --force-reinstall

Offline Install PyMySQL

Activate the target environment and run the setup script from the downloaded PyMySQL source directory:

source activate tf_env
python setup.py install

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.