Setting Up a Python Environment for AI Development: A Comprehensive Guide - Flexiana
avatar

Posted on 10th December 2024

Setting Up a Python Environment for AI Development: A Comprehensive Guide

news-paper AI | News | Software Development |

System Requirements

Before starting, ensure your system meets these minimum requirements:

  • 8GB RAM (16GB recommended)
  • 64-bit operating system
  • 10GB free disk space
  • (Optional) NVIDIA GPU for deep learning
# Check Python version
python --version

# Check system architecture
python -c "import platform; print(platform.architecture())"

Python Installation

Download Python

# Ubuntu/Debian
sudo apt update
sudo apt install python3.10 python3.10-dev python3-pip

# macOS (using Homebrew)
brew install python@3.10

# Windows
# Download from python.org and enable "Add Python to PATH"

Verify Installation

python --version
pip --version

Package Management

Set up pip and conda for package management:

# Upgrade pip
python -m pip install --upgrade pip

# Install conda
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

# Initialize conda
conda init

Virtual Environments

Create isolated environments for different projects:

# Using venv
python -m venv ai_env
source ai_env/bin/activate  # Linux/macOS
ai_env\Scripts\activate     # Windows

# Using conda
conda create -n ai_env python=3.10
conda activate ai_env

IDE Setup

Visual Studio Code

# Install extensions
code --install-extension ms-python.python
code --install-extension ms-toolsai.jupyter

PyCharm

  • Download PyCharm Professional/Community
  • Configure Python interpreter
  • Install AI plugins

Essential AI Libraries

Create a requirements.txt:

numpy==1.23.5
pandas==1.5.3
scikit-learn==1.2.2
tensorflow==2.12.0
torch==2.0.0
transformers==4.28.1
jupyter==1.0.0
matplotlib==3.7.1
seaborn==0.12.2

Install dependencies:

pip install -r requirements.txt

GPU Configuration

For NVIDIA GPUs:

# Check GPU
nvidia-smi

# Install CUDA Toolkit
wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run
sudo sh cuda_11.8.0_520.61.05_linux.run

# Install cuDNN
# Download from NVIDIA website and install

Update .bashrc or .zshrc:

export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

Testing Your Setup

Create a test script test_setup.py:

import numpy as np
import pandas as pd
import tensorflow as tf
import torch
from sklearn.datasets import load_iris
from transformers import pipeline

def test_cpu_gpu():
    # Test TensorFlow
    print("TensorFlow version:", tf.__version__)
    print("TensorFlow GPU available:", tf.config.list_physical_devices('GPU'))

    # Test PyTorch
    print("PyTorch version:", torch.__version__)
    print("PyTorch GPU available:", torch.cuda.is_available())

    # Test basic ML
    iris = load_iris()
    print("Sklearn dataset loaded successfully")

    # Test transformers
    classifier = pipeline('sentiment-analysis')
    result = classifier("Testing my AI environment!")
    print("Transformers test:", result)

if __name__ == "__main__":
    test_cpu_gpu()

Run the test:

python test_setup.py

Common Issues and Solutions
GPU Not Detected
# Check CUDA version
nvcc --version

# Verify TensorFlow GPU
python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

Package Conflicts

# Create environment with specific versions
conda create -n ai_env python=3.10 tensorflow-gpu=2.12.0 pytorch=2.0.0 cudatoolkit=11.8

Memory Issues

# Limit GPU memory growth
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
    tf.config.experimental.set_memory_growth(gpu, True)

Best Practices

Project Structure

ai_project/
├── .gitignore
├── README.md
├── requirements.txt
├── setup.py
├── data/
├── notebooks/
├── src/
│   ├── __init__.py
│   ├── data/
│   ├── models/
│   └── utils/
└── tests/

Environment Management

# Save environment
pip freeze > requirements.txt
conda env export > environment.yml

# Load environment
pip install -r requirements.txt
conda env create -f environment.yml

Git Configuration

# .gitignore
venv/
__pycache__/
.ipynb_checkpoints/
*.pyc
.env
data/raw/
models/trained/

Remember to:

  • Regularly update packages
  • Document environment setup
  • Use version control
  • Keep separate environments for different projects

Your AI development environment is now ready! Start building amazing AI applications!