Introduction to Environment Isolation
Utilizing Conda allows developers to maintain isolated workspaces for each project, ensuring dependency conflicts are avoided. Each project can operate with its own specific set of libraries and Python versions without interfering with the system-wide installation or other projects.
Installation Procedures
Windows Environment
For Windows users, downloading the graphical installer from the official Anaconda or Miniconda repository is the standard approach. Once the installation wizard completes, ensure the option to add Conda to the system PATH is selected to facilitate command-line access.
Linux and ARM Architecture
On Linux systems, particularly those with ARM64 architecture (such as NVIDIA Jetson Nano), specific installers are required. Users should verify their machine architecture before downloading.
$ uname -m
aarch64
If the output indicates aarch64, download the ARM64 compatible installer. For standard x86_64 systems, the regular 64-bit installer is appropriate. After downloading the shell script, execute it using bash:
bash Miniconda3-latest-Linux-aarch64.sh
Follow the prompts to accept the license agreement and confirm the installation path. Once finished, modify the shell configuration file to ensure the Conda Python interpreter takes precedence.
nano ~/.bashrc
Add the following line to update the PATH variable:
export PATH="$HOME/miniconda3/bin:$PATH"
Apply the changes immediately:
source ~/.bashrc
Verify the active Python interpreter:
which python
Configuring Mirror Sources
Default package repositories may result in slow download speeds depending on geographic location. Configuring regional mirrors can significantly improve performance during package installation.
Conda Channel Configuration
To switch to reliable domestic mirrors (example using USTC mirrors), execute the following configuration commands:
conda config --remove-key channels
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
Alternatively, Tsinghua University mirrors are also widely used:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
Pip Mirror Configuration
For pip installations, set the global index URL to a faster mirror:
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
This creates a configuration file automatically. To verify the current settings:
pip config list
For one-off installations without changing global config, use the -i flag:
pip install package_name -i https://pypi.mirrors.ustc.edu.cn/simple/
Managing Python Versions
Linux systems typically come with a pre-installed "System Python" located in /usr/bin. Conda provides its own managed Python versions within isolated environments. Its crucial to distinguish between the two to avoid breaking system tools that rely on the default Python.
To check system Python versions:
ls /usr/bin/python*
If specific system tools require a particular version, you can create an alias in your shell configuration rather than changing the system default. For example, to map a command to a specific system version:
alias system_python='/usr/bin/python3.6'
Conda environments should be activated when working on projects requiring specific dependencies.
Package Manager Comparison
Understanding the distinction between package managers is vital for system stability:
- apt-get: Manages system-level software on Debian-based distributions. Packages are installed globally and handle OS-level dependencies.
- pip: The standard package installer for Python. It installs libraries into the current Python environment but does not manage non-Python dependencies well.
- conda: A cross-language package manager that handles binary dependencies and environments. It is preferred for data science workflows as it can manage Python versions and C-libraries simultaneously.
Essential Conda Commands
Environment Operations
# Create a new environment with a specific Python version
conda create -n project_env python=3.10
# List all available environments
conda info --envs
# Activate a specific environment
conda activate project_env
# Deactivate the current environment
conda deactivate
# Remove an environment completely
conda remove --name project_env --all
Package Operations
# Install a package
conda install numpy
# Install a specific version
conda install pandas=1.3.0
# Update all packages in the current environment
conda update --all
# List installed packages
conda list
# Search for a package
conda search scipy
Practical Example: OpenCV on Jetson Platform
When working with NVIDIA Jetson modules, accessing CSI cameras often requires GStreamer pipelines via OpenCV. Below is a refactored example demonstrating how to configure and capture video streams.
First, ensure necessary camera overrides are in place if required by the hardware manufacturer:
wget http://www.waveshare.net/w/upload/e/eb/Camera_overrides.tar.gz
tar zxvf Camera_overrides.tar.gz
sudo cp camera_overrides.isp /var/nvidia/nvcam/settings/
sudo chmod 664 /var/nvidia/nvcam/settings/camera_overrides.isp
sudo chown root:root /var/nvidia/nvcam/settings/camera_overrides.isp
Install the required Python libraries:
pip3 install opencv-python numpy
The following script constructs a GStreamer pipeline dynamically and initializes the video capture:
import cv2
import numpy as np
def construct_gstreamer_string(sensor_index, width, height, fps, flip=0):
pipeline = (
f"nvarguscamerasrc sensor-id={sensor_index} ! "
f"video/x-raw(memory:NVMM), width=(int){width}, height=(int){height}, "
f"format=(string)NV12, framerate=(fraction){fps}/1 ! "
f"nvvidconv flip-method={flip} ! "
f"video/x-raw, width=(int){width}, height=(int){height}, format=(string)BGRx ! "
f"videoconvert ! video/x-raw, format=(string)BGR ! appsink"
)
return pipeline
def run_camera_preview():
stream_config = construct_gstreamer_string(
sensor_index=0,
width=1280,
height=720,
fps=30
)
video_capture = cv2.VideoCapture(stream_config, cv2.CAP_GSTREAMER)
if not video_capture.isOpened():
print("Error: Could not open camera stream")
return
while True:
ret, video_frame = video_capture.read()
if not ret:
break
cv2.imshow("CSI Camera Feed", video_frame)
# Press 'q' to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
run_camera_preview()
Ubuntu Repository Configuration
For Ubuntu systems, switching to a local mirror for apt packages can speed up system updates. Always backup the original source list before modifying.
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
Edit the sources list to point to a regional mirror (e.g., Tsinghua for ARM64 ports):
sudo nano /etc/apt/sources.list
After updating the file content, refresh the package index:
sudo apt-get clean
sudo apt-get update
sudo apt-get upgrade
Exercise caution when upgrading kernel-related packages on embedded devices to prevent boot failures.