Best Practices for Python Project Setup 🚀 Here's a step-by-step guide for proper project initialization:
# Create and Navigate to Project Directory
mkdir project_name
cd project_name
# Create Project Structure
mkdir -p src/{models,api,utils} tests static/{css,js,images} templates docs scripts
touch src/__init__.py src/main.py README.md requirements.txt .gitignore .env.example
# Initialize Virtual Environment
# Create virtual environment
python3 -m venv venv
# Activate it
source venv/bin/activate
# Install Essential Development Tools
# Upgrade pip and tools
python -m pip install --upgrade pip setuptools wheel
# Install packages
pip install python-dotenv fastapi uvicorn sqlalchemy
# Save Dependencies
pip freeze > requirements.txt
# Configure VS Code
{
"python.defaultInterpreterPath": "${workspaceFolder}/venv/bin/python",
"python.terminal.activateEnvironment": true,
"python.formatting.provider": "black",
"editor.formatOnSave": true
}
Initialize Git:
git init
git add .
git commit -m "Initial project setup"
# Create a new repository on GitHub
Go to github.com and log in
Click the "+" button in the top right
Select "New repository"
Name your repository (e.g., "setup_best_practice_python")
Don't initialize with README (since you already have a local repo)
Click "Create repository"
# If you're using HTTPS:
# Replace YOUR-USERNAME and YOUR-REPOSITORY-NAME with your actual GitHub username and repository name
git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPOSITORY-NAME.git
# Verify the remote was added correctly
git remote -v
# Push your code to GitHub (if your main branch is called "main")
git push -u origin main
# OR if your main branch is called "master"
git push -u origin master
# If you prefer using SSH (make sure you have SSH keys set up):
# Replace YOUR-USERNAME and YOUR-REPOSITORY-NAME with your actual GitHub username and repository name
git remote add origin [email protected]:YOUR-USERNAME/YOUR-REPOSITORY-NAME.git
# Verify the remote was added correctly
git remote -v
# Push your code
git push -u origin main # or master
# After this, for future changes you can simply use:
git add .
git commit -m "Your commit message"
git push
# The -u flag in the first push sets up tracking, so you won't need to specify origin main in future pushes.
# git remote -v
# Remove the existing remote origin
git remote remove origin
# Add your new remote origin
git remote add origin https://github.com/Andhar7/python_setup.git
# Verify the new remote was added correctly
git remote -v
# After this, you should be able to push your code to the new repository:
git push -u origin main # or 'master' if that's your branch name
Remember:
Always activate venv before installing packages
Keep requirements.txt updated
Never commit .env files or venv directory
Use .env.example for environment variable templates
This structure follows Python best practices and makes your project maintainable from the start! 🏗️
Complete Project Structure with Frontend and Backend 🌳
Here's a comprehensive project structure for a full-stack application:project_name/ ├── backend/ # Backend root │ ├── venv/ # Python virtual environment │ ├── src/ # Source code │ │ ├── init.py │ │ ├── main.py # FastAPI application entry │ │ ├── config/ # Configuration files │ │ │ ├── init.py │ │ │ └── settings.py │ │ ├── api/ # API routes │ │ │ ├── init.py │ │ │ ├── v1/ # API version 1 │ │ │ └── endpoints/ │ │ ├── core/ # Core functionality │ │ │ ├── init.py │ │ │ ├── security.py │ │ │ └── config.py │ │ ├── models/ # Database models │ │ ├── schemas/ # Pydantic schemas │ │ ├── services/ # Business logic │ │ └── utils/ # Utility functions │ ├── tests/ # Test files │ │ ├── init.py │ │ ├── conftest.py │ │ └── api/ │ ├── .env # Environment variables │ ├── .env.example # Environment template │ ├── .gitignore │ ├── requirements.txt │ └── README.md │ ├── frontend/ # Frontend root │ ├── public/ # Static files │ │ ├── index.html │ │ ├── favicon.ico │ │ └── assets/ │ ├── src/ # Source files │ │ ├── assets/ # Images, fonts, etc. │ │ ├── components/ # React components │ │ ├── hooks/ # Custom hooks │ │ ├── pages/ # Page components │ │ ├── services/ # API services │ │ ├── store/ # State management │ │ ├── styles/ # CSS/SCSS files │ │ ├── types/ # TypeScript types │ │ ├── utils/ # Utility functions │ │ ├── App.tsx │ │ └── main.tsx │ ├── node_modules/ # Node packages (git ignored) │ ├── .env # Frontend environment variables │ ├── .env.example │ ├── .gitignore │ ├── package.json │ ├── tsconfig.json # TypeScript config │ ├── vite.config.ts # Vite config │ └── README.md │ ├── docker/ # Docker configuration │ ├── backend/ │ │ └── Dockerfile │ └── frontend/ │ └── Dockerfile │ ├── docker-compose.yml # Docker compose config ├── .gitignore # Root gitignore ├── .env # Root environment variables ├── README.md # Project documentation └── Makefile # Project commands
Initialize Project with Commands:
mkdir -p project_name/{backend,frontend}
cd project_name/backend python3 -m venv venv source venv/bin/activate pip install fastapi uvicorn sqlalchemy python-dotenv pip freeze > requirements.txt
cd ../frontend npm create vite@latest . -- --template react-ts npm install
cd .. touch docker-compose.yml Makefile README.md .gitignore
Sample .gitignore at Root Level:
node_modules/ venv/
.env .env.local .env.*.local
dist/ build/ *.pyc pycache/
.vscode/ .idea/
.log npm-debug.log
.DS_Store
Remember to initialize git and create your first commit after setting up! 🚀