Install TensorFlow/Keras and PyTorch via Anaconda, and Migrate Virtual Environments to Offline Systems
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
- Activate the target environment and install conda-pack:
source activate tf_env
conda install -c conda-forge conda-pack
- Package the environment into a compressed file:
conda pack -n tf_env -o tf_env.tar.gz
3.2 Deploy on Offline System (Linux)
- Copy
tf_env.tar.gzto the target machine's Anaconda envs directory (e.g.,/opt/anaconda3/envs). - Extract the package:
mkdir -p /opt/anaconda3/envs/tf_env
tar -xzf tf_env.tar.gz -C /opt/anaconda3/envs/tf_env
- Activate the environment and finalize setup:
source activate tf_env
conda-unpack
3.3 Deploy on Offline System (Windows)
- Copy
tf_env.tar.gztoC:\ProgramData\Anaconda3\envs. - 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
- 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