Files
the_information_nexus/docs/tech_docs/python/python_project.md

1.6 KiB

Python Project Setup Guide

#!/usr/bin/env python3

Setting Up a Virtual Environment

  1. Create Project Directory

    mkdir my_project && cd my_project
    
  2. Create Virtual Environment

    python3 -m venv venv
    
  3. Activate Virtual Environment

    • macOS/Linux:
      source venv/bin/activate
      
    • Windows:
      .\venv\Scripts\activate
      
  4. Deactivate Virtual Environment

    deactivate
    

Package Management with pip

  1. Install a Package

    pip install package_name
    
  2. Install Specific Version

    pip install package_name==version
    
  3. Upgrade a Package

    pip install --upgrade package_name
    
  4. Using a Requirements File

    • Install packages:
      pip install -r requirements.txt
      
    • Freeze current packages to requirements.txt:
      pip freeze > requirements.txt
      

Managing Dependencies

  • Create a requirements.txt file in your project root.
  • List dependencies and versions, e.g., flask==1.1.2.
  • Use pip freeze to generate this file with current environment packages.

Best Practices

  • Always activate your virtual environment when working on the project.
  • Regularly update your requirements.txt to reflect new dependencies.
  • Include venv in your .gitignore file.
  • Commit requirements.txt to version control.

This guide provides a streamlined approach to setting up and managing Python projects, ensuring consistency and ease of use across development environments.