Cài Đặt Môi Trường Python
1. Các cách cài đặt Python
Có nhiều cách để cài đặt Python cho Data Science:
| Phương pháp | Ưu điểm | Nhược điểm | Phù hợp với |
|---|---|---|---|
| Python.org | Nhẹ, cơ bản | Phải cài thư viện thủ công | Beginners |
| Anaconda | Đầy đủ thư viện | Dung lượng lớn (~3GB) | Data Scientists |
| Miniconda | Nhẹ, linh hoạt | Cần cài thêm packages | Experienced users |
| Google Colab | Không cần cài | Cần internet | Quick experiments |
2. Cài đặt Anaconda (Khuyến nghị)
Bước 1: Tải Anaconda
Truy cập anaconda.com/download và tải phiên bản phù hợp:
- Windows: Anaconda3-xxx-Windows-x86_64.exe
- macOS: Anaconda3-xxx-MacOSX-x86_64.pkg
- Linux: Anaconda3-xxx-Linux-x86_64.sh
Bước 2: Cài đặt
Windows:
powershell
1# Chạy file .exe và làm theo hướng dẫn2# Tick "Add Anaconda to PATH" (optional)macOS/Linux:
Bash
1# macOS2chmod +x Anaconda3-xxx-MacOSX-x86_64.sh3./Anaconda3-xxx-MacOSX-x86_64.sh4 5# Linux6chmod +x Anaconda3-xxx-Linux-x86_64.sh7./Anaconda3-xxx-Linux-x86_64.shBước 3: Kiểm tra cài đặt
Bash
1# Mở Terminal/Anaconda Prompt2conda --version3# Output: conda 23.x.x4 5python --version6# Output: Python 3.11.x3. Quản lý môi trường với Conda
3.1 Tạo môi trường mới
Bash
1# Tạo môi trường với Python 3.112conda create -n datascience python=3.113 4# Kích hoạt môi trường5conda activate datascience6 7# Kiểm tra8python --version3.2 Cài đặt thư viện
Bash
1# Cài đặt các thư viện Data Science cơ bản2conda install pandas numpy matplotlib seaborn3 4# Hoặc dùng pip5pip install pandas numpy matplotlib seaborn6 7# Cài nhiều thư viện cùng lúc8conda install pandas numpy scipy scikit-learn jupyter3.3 Các lệnh conda thường dùng
Bash
1# Xem danh sách môi trường2conda env list3 4# Xem packages đã cài5conda list6 7# Cập nhật package8conda update pandas9 10# Xóa package11conda remove seaborn12 13# Xóa môi trường14conda env remove -n datascience15 16# Export môi trường17conda env export > environment.yml18 19# Tạo từ file yml20conda env create -f environment.yml4. Jupyter Notebook
Jupyter Notebook là công cụ không thể thiếu cho Data Science.
4.1 Cài đặt và chạy
Bash
1# Cài đặt2conda install jupyter3 4# Chạy Jupyter Notebook5jupyter notebook6 7# Hoặc JupyterLab (giao diện mới hơn)8conda install jupyterlab9jupyter lab4.2 Shortcuts quan trọng
| Shortcut | Chức năng |
|---|---|
Shift + Enter | Chạy cell và di chuyển xuống |
Ctrl + Enter | Chạy cell tại chỗ |
A | Thêm cell phía trên |
B | Thêm cell phía dưới |
DD | Xóa cell |
M | Chuyển sang Markdown |
Y | Chuyển sang Code |
Esc | Command mode |
Enter | Edit mode |
4.3 Magic Commands
Python
1# Đo thời gian chạy2%time df = pd.read_csv('large_file.csv')34# Đo thời gian trung bình (chạy nhiều lần)5%timeit df['col'].sum()67# Hiển thị biểu đồ inline8%matplotlib inline910# Load extension11%load_ext autoreload12%autoreload 21314# Xem tất cả variables15%whos1617# Chạy file Python18%run script.py5. VS Code cho Python
5.1 Cài đặt VS Code
- Tải từ code.visualstudio.com
- Cài đặt Extensions:
- Python (Microsoft)
- Pylance (Microsoft)
- Jupyter (Microsoft)
5.2 Cấu hình Python Interpreter
JSON
1// settings.json2{3 "python.defaultInterpreterPath": "~/anaconda3/envs/datascience/bin/python",4 "python.linting.enabled": true,5 "python.linting.pylintEnabled": true,6 "python.formatting.provider": "black"7}5.3 Chạy Jupyter trong VS Code
- Tạo file
.ipynb - Chọn kernel (môi trường Python)
- Viết code và chạy với
Shift + Enter
6. Google Colab (Không cần cài đặt)
6.1 Truy cập
Vào colab.research.google.com và đăng nhập với Google account.
6.2 Ưu điểm của Colab
- ✅ Miễn phí GPU/TPU - Phù hợp Deep Learning
- ✅ Không cần cài đặt - Chạy trên browser
- ✅ Đã cài sẵn thư viện - Pandas, TensorFlow, PyTorch
- ✅ Lưu trên Drive - Dễ chia sẻ
6.3 Kết nối Google Drive
Python
1# Mount Google Drive2from google.colab import drive3drive.mount('/content/drive')45# Đọc file từ Drive6import pandas as pd7df = pd.read_csv('/content/drive/MyDrive/data.csv')6.4 Cài thêm thư viện
Python
1# Cài thư viện mới2!pip install polars plotly streamlit34# Kiểm tra version5!pip show pandas7. File requirements.txt
7.1 Tạo requirements.txt
Bash
1# Export tất cả packages2pip freeze > requirements.txt3 4# Hoặc tạo thủ công5echo "pandas>=2.0.06numpy>=1.24.07matplotlib>=3.7.08seaborn>=0.12.09scikit-learn>=1.3.010jupyter>=1.0.0" > requirements.txt7.2 Cài từ requirements.txt
Bash
1pip install -r requirements.txt8. Kiểm tra môi trường
Chạy script sau để kiểm tra môi trường đã sẵn sàng:
Python
1# check_environment.py2import sys3print(f"Python version: {sys.version}")45# Kiểm tra các thư viện6libraries = ['pandas', 'numpy', 'matplotlib', 'seaborn', 'sklearn']78for lib in libraries:9 try:10 module = __import__(lib)11 version = getattr(module, '__version__', 'unknown')12 print(f"✅ {lib}: {version}")13 except ImportError:14 print(f"❌ {lib}: NOT INSTALLED")Output mong đợi:
Text
1Python version: 3.11.52✅ pandas: 2.1.03✅ numpy: 1.24.34✅ matplotlib: 3.7.25✅ seaborn: 0.12.26✅ sklearn: 1.3.0Tổng Kết
Trong bài này, bạn đã học:
- ✅ Các cách cài đặt Python cho Data Science
- ✅ Cài đặt và sử dụng Anaconda
- ✅ Quản lý môi trường với Conda
- ✅ Sử dụng Jupyter Notebook
- ✅ Cấu hình VS Code cho Python
- ✅ Sử dụng Google Colab
Bài tiếp theo: Cú pháp cơ bản Python - Variables, Data Types, Operators!
