[Avg. reading time: 17 minutes]
Python Environment
Prerequisites
For Windows Users
- Install GitBash
- Download and install from:
https://gitforwindows.org/ - This provides a Unix-like terminal environment
- Download and install from:
Step 1: Install Poetry
- Visit the official installer page:
https://python-poetry.org/docs/main/ - Follow the installation instructions for your operating system
- Verify your installation:
poetry --version
Step 2: Configure Poetry Shell
Add the shell plugin to enable virtual environment management:
poetry self add poetry-plugin-shell
Step 3: Create Your First Project
- Create a new project:
poetry new helloworld
This creates a new directory with the following structure:
helloworld/
├── pyproject.toml
├── README.md
├── helloworld/
│ └── __init__.py
└── tests/
└── __init__.py
- Navigate to your project directory
cd helloworld
Windows Users


Step 4: Working with Virtual Environments
- Create and activate a virtual environment:
poetry shell
- Switch Python Interpreter to Virtual Environment
poetry env info
poetry env info -e
poetry env <paste the path>
Or Use this one line.
poetry env use $(poetry env info -e)
- Add project dependencies:
poetry add pandas
- Create a main.py under hellworld (subfolder)
main.py
print("hello world")
Step 5: Managing Your Project
- View all installed dependencies:
poetry show
- Update dependencies:
poetry update
- Remove a dependency:
poetry remove package-name
- Run program
poetry run python main.py
- Exit Virtual Environment
exit
Key Benefits for Beginners
- Simplified Environment Management
- Poetry automatically creates and manages virtual environments
- No need to manually manage pip and virtualenv
- Clear Dependency Specification
- All dependencies are listed in one file (pyproject.toml)
- Dependencies are automatically resolved to avoid conflicts
- Project Isolation
- Each project has its own dependencies
- Prevents conflicts between different projects
- Easy Distribution
- Package your project with a single command:
poetry build
Publish to PyPI when ready:
poetry publish
Best Practices
- Always activate virtual environment before working on project
- Keep pyproject.toml updated with correct dependencies
- Use version constraints wisely
- Commit both pyproject.toml and poetry.lock files to version control
PEP
PEP, or Python Enhancement Proposal, is the official style guide for Python code. It provides conventions and recommendations for writing readable, consistent, and maintainable Python code.
Key Aspects of PEP 8 (Popular ones)
Indentation
- Use 4 spaces per indentation level.
- Avoid tabs; spaces are preferred.
Line Length
- Limit lines to a maximum of 79 characters.
- For docstrings and comments, limit lines to 72 characters.
Blank Lines
- Use blank lines to separate functions, classes, and code blocks inside functions.
- Typically, use two blank lines before defining a class or function.
Imports
- Imports should be on separate lines.
- Group imports into three sections: standard library, third-party libraries, and local application imports.
- Use absolute imports whenever possible.
Naming Conventions
- Use snake_case for function and variable names.
- Use CamelCase for class names.
- Use UPPER_SNAKE_CASE for constants.
- Avoid single-character variable names except for counters or indices.
Whitespace
- Avoid extraneous whitespace inside parentheses, brackets, or braces.
- Use a single space around operators and after commas, but not directly inside function call parentheses.
Comments
- Write comments that are clear, concise, and helpful.
- Use complete sentences and capitalize the first word.
- Use # for inline comments, but avoid them where the code is self-explanatory.
Docstrings
- Use triple quotes (“”“) for multiline docstrings.
- Describe the purpose, arguments, and return values of functions and methods.
Code Layout
- Keep function definitions and calls readable.
- Avoid writing too many nested blocks.
Consistency
Be consistent within a project or module, even if it means diverging slightly from PEP 8, as long as it serves the project better.
Linting
Python linting refers to the process of analyzing Python code for potential errors, stylistic inconsistencies, and programming best practices.
Basically review your code to make sure it follows the PEP 8 standard.
# Incorrect
import sys, os
# Correct
import os
import sys
Ruff : Linter and Code Formatter
Ruff is a fast Python linter, formatter, and code analyzer that checks your Python code for common stylistic and syntactical issues.
Install
poetry add ruff
Verify
ruff –version or –help
Demo
example.py
import os, sys
def greet(name):
print(f"Hello, {name}")
def message(name):
print(f"Hi, {name}")
def calc_sum(a, b): return a+b
greet('World')
greet('Ruff')
poetry run ruff check example.py
poetry run ruff check example.py --fix
poetry run ruff format example.py --check
poetry run ruff format example.py
MyPy : Type Checking Tool
mypy is a static type checker for Python. It checks your code against the type hints you provide, ensuring that the types are consistent throughout the codebase.
It primarily focuses on type correctness—verifying that variables, function arguments, return types, and expressions match the expected types.
Install
poetry add mypy
or
pip install mypy
sample.py
x = 1
x = 1.0
x = True
x = "test"
x = b"test"
print(x)
mypy sample.py
or
poetry run mypy sample.py
````<span id='footer-class'>Ver 6.0.5</span>
<footer id="last-change">Last change: 2026-02-05</footer>````