Monday, October 5, 2026

How to remove and add .venv and requirements.txt in vs code

 

1. Open your project in VS Code

For example:

cd D:\vscode_python_practice

Check your current environment:

python --version

and:

python -c "import sys; print(sys.executable)"

2. Deactivate the current virtual environment

If you see:

(.venv) PS D:\vscode_python_practice>

run:

deactivate

The prompt should become:

PS D:\vscode_python_practice>

Verify:

python --version

3. Create requirements.txt from the existing environment

Do this before deleting .venv if you want to preserve your currently installed packages.

If the old .venv is still available, activate it again temporarily:

.\.venv\Scripts\Activate.ps1

Then:

pip freeze > requirements.txt

Example requirements.txt:

numpy==2.x.x
pandas==2.x.x
matplotlib==3.x.x
pyspark==4.x.x

Then deactivate:

deactivate

4. Delete the old .venv

From your project directory:

Remove-Item -Recurse -Force .venv

Check that .venv is gone:

Get-ChildItem

You should still have:

D:\vscode_python_practice
│
├── requirements.txt
├── your_python_files.py
└── ...

5. Install UV

First check whether UV is already installed:

uv --version

If it says that uv is not recognized, install UV.

One common Windows installation method is:

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

Close and reopen the VS Code terminal afterward.

Verify:

uv --version

You should get something similar to:

uv 0.x.x

6. Check whether Python 3.15 is available

Run:

uv python list

Look for:

cpython-3.15...

You can also ask UV to install Python 3.15:

uv python install 3.15

Then verify:

uv python list

7. Create a UV environment using Python 3.15

From:

D:\vscode_python_practice

run:

uv venv --python 3.15

This creates:

D:\vscode_python_practice\.venv

using Python 3.15.

You should see something similar to:

Using CPython 3.15...
Creating virtual environment at: .venv

8. Activate the UV environment

On Windows PowerShell:

.\.venv\Scripts\Activate.ps1

Your prompt should now show:

(.venv) PS D:\vscode_python_practice>

9. Verify Python 3.15

Run:

python --version

Expected:

Python 3.15.x

Then verify exactly which Python is being used:

python -c "import sys; print(sys.executable)"

Expected:

D:\vscode_python_practice\.venv\Scripts\python.exe

10. Install packages from requirements.txt

Now install your existing requirements:

uv pip install -r requirements.txt

UV will install the packages into your active .venv.

You can verify:

uv pip list

or:

pip list

11. Point VS Code to the new UV environment

This is important.

Press:

Ctrl + Shift + P

Search:

Python: Select Interpreter

Select:

.venv\Scripts\python.exe

It should correspond to:

D:\vscode_python_practice\.venv\Scripts\python.exe

12. Verify from VS Code

Open a new terminal.

You should see:

(.venv) PS D:\vscode_python_practice>

Run:

python --version

Then:

python -c "import sys; print(sys.executable)"

You should get:

Python 3.15.x

D:\vscode_python_practice\.venv\Scripts\python.exe

Complete command sequence

If you already have requirements.txt, the clean process is:

cd D:\vscode_python_practice

deactivate

uv --version

uv python install 3.15

Remove-Item -Recurse -Force .venv

uv venv --python 3.15

.\.venv\Scripts\Activate.ps1

python --version

python -c "import sys; print(sys.executable)"

uv pip install -r requirements.txt

Then in VS Code:

Ctrl + Shift + P
        ↓
Python: Select Interpreter
        ↓
.venv\Scripts\python.exe

One important recommendation for your PySpark setup

Before committing to Python 3.15, check your PySpark version:

findstr pyspark requirements.txt

If your project contains PySpark, Python 3.12 or 3.13 may currently be a much safer choice than 3.15 because the PySpark/Python compatibility matrix and third-party packages matter.

 #################

 

(.venv) PS D:\vscode_python_practice> cd D:\vscode_python_practice
(.venv) PS D:\vscode_python_practice> python --version
Python 3.13.15
(.venv) PS D:\vscode_python_practice> python -c "import sys; print(sys.executable)"
D:\vscode_python_practice\.venv\Scripts\python.exe
(.venv) PS D:\vscode_python_practice> deactivate
PS D:\vscode_python_practice> python --version
Python 3.11.0
PS D:\vscode_python_practice> Remove-Item -Recurse -Force .venv

PS D:\vscode_python_practice> Get-ChildItem


    Directory: D:\vscode_python_practice


Mode                 LastWriteTime         Length Name                                       
----                 -------------         ------ ----                                       
-a----        29-09-2026     16:09            246 Program to show args and kwargs in python  
                                                  copy.py                                    
-a----        29-09-2026     16:41            916 python Version Check.py                    

You should still have:

D:\vscode_python_practice
│
├── requirements.txt
├── your_python_files.py
└── ...


PS D:\vscode_python_practice> powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"                                                                            
downloading uv 0.12.23 (x86_64-pc-windows-msvc)                                               
installing to C:\Users\rajanikanta\.local\bin
  uv.exe
  uvx.exe
  uvw.exe
everything's installed!

To add C:\Users\rajanikanta\.local\bin to your PATH, either restart your shell or run:

    set Path=C:\Users\rajanikanta\.local\bin;%Path%   (cmd)
    $env:Path = "C:\Users\rajanikanta\.local\bin;$env:Path"   (powershell)
PS D:\vscode_python_practice> 


Close VS Code and Reopen :


PS D:\vscode_python_practice> uv --version
uv 0.12.23 (46b84fd0b 2026-10-03 x86_64-pc-windows-msvc)



PS D:\vscode_python_practice> Test-Path .venv
False
PS D:\vscode_python_practice> uv venv .venv --python 3.13.15
Using CPython 3.13.15
Creating virtual environment at: .venv
Activate with: .venv\Scripts\activate
PS D:\vscode_python_practice> .\.venv\Scripts\Activate.ps1
(.venv) PS D:\vscode_python_practice> python --version
Python 3.13.15
(.venv) PS D:\vscode_python_practice> python -c "import sys; print(sys.executable)"
D:\vscode_python_practice\.venv\Scripts\python.exe
(.venv) PS D:\vscode_python_practice> 



D:\vscode_python_practice
│
├── .venv
├── requirements.txt
└── your_python_file.py



(.venv) PS D:\vscode_python_practice> uv pip install -r requirements.txt



(.venv) PS D:\vscode_python_practice> uv pip install -r requirements.txt
Resolved 122 packages in 957ms
Prepared 122 packages in 12.97s
░░░░░░░░░░░░░░░░░░░░ [0/122] Installing wheels...warning: Failed to hardlink files; falling back to full copy. This may lead to degraded performance.
         If the cache and target directories are on different filesystems, hardlinking may not be supported.
         If this is intentional, set `export UV_LINK_MODE=copy` or use `--link-mode=copy` to suppress this warning.
Installed 122 packages in 52.96s
 + anyio==4.15.1
 + argon2-cffi==25.1.0
 + argon2-cffi-bindings==26.1.0
 + arrow==1.4.0
 + asttokens==3.0.2
 + async-lru==2.3.0
 + attrs==26.1.0
 + babel==2.18.0
 + beautifulsoup4==4.15.0
 + bleach==6.4.0
 + certifi==2026.7.22
 + cffi==2.1.1
 + charset-normalizer==3.5.2
 + colorama==0.4.6
 + comm==0.2.3
 + contourpy==1.4.0
 + cycler==0.12.1
 + debugpy==1.8.22
 + defusedxml==0.7.1
 + executing==2.2.1
 + fastjsonschema==2.22.2
 + filelock==4.0.11
 + fonttools==4.66.1
 + fqdn==1.6.0
 + fsspec==2026.9.0
 + h11==0.16.0
 + httpcore==1.0.9
 + httpx==0.28.1
 + huggingface-hub==0.36.2
 + idna==3.20
 + ipykernel==6.29.5
 + ipython==9.17.1
 + ipython-pygments-lexers==1.1.1
 + ipywidgets==8.1.9
 + isoduration==20.11.0
 + jedi==0.20.0
 + jinja2==3.1.6
 + json5==0.15.0
 + jsonpointer==3.1.1
 + jsonschema==4.25.0
 + jsonschema-specifications==2025.9.1
 + jupyter==1.1.1
 + jupyter-client==8.10.0
 + jupyter-console==6.6.3
 + jupyter-core==5.9.1
 + jupyter-events==0.12.1
 + jupyter-lsp==2.3.1
 + jupyter-server==2.21.1
 + jupyter-server-terminals==0.5.4
 + jupyterlab==4.4.10
 + jupyterlab-pygments==0.3.0
 + jupyterlab-server==2.28.1
 + jupyterlab-widgets==3.0.17
 + kiwisolver==1.5.1
 + lark==1.3.1
 + markdown-it-py==4.2.0
 + markupsafe==3.0.4
 + matplotlib==3.11.2
 + matplotlib-inline==0.2.2
 + mdurl==0.1.2
 + mistune==3.3.4
 + mpmath==1.3.0
 + nbclient==0.11.0
 + nbconvert==7.17.1
 + nbformat==5.11.1
 + nest-asyncio==1.6.0
 + networkx==3.7
 + notebook==7.4.4
 + notebook-shim==0.2.4
 + numpy==2.3.1
 + packaging==26.3
 + pandas==2.3.0
 + pandocfilters==1.5.1
 + parso==0.8.7
 + pillow==12.3.0
 + platformdirs==4.12.3
 + prometheus-client==0.26.0
 + prompt-toolkit==3.0.53
 + psutil==7.2.2
 + pure-eval==0.2.4
 + pycparser==3.0
 + pygments==2.21.0
 + pyparsing==3.3.3
 + python-dateutil==2.9.0.post0
 + python-dotenv==1.1.1
 + python-json-logger==4.2.0
 + pytz==2026.5
 + pywinpty==3.0.5
 + pyyaml==6.0.3
 + pyzmq==27.2.0
 + referencing==0.37.0
 + regex==2026.9.29
 + requests==2.32.4
 + rfc3339-validator==0.1.4
 + rfc3986-validator==0.1.1
 + rfc3987-syntax==1.1.0
 + rich==14.0.0
 + rpds-py==2026.9.1
 + safetensors==0.8.0
 + send2trash==2.1.0
 + setuptools==84.0.0
 + six==1.17.0
 + soupsieve==2.10
 + stack-data==0.6.3
 + sympy==1.14.0
 + terminado==0.18.1
 + tinycss2==1.5.1
 + tokenizers==0.21.4
 + torch==2.7.1
 + tornado==6.5.10
 + tqdm==4.70.1
 + traitlets==5.16.1
 + transformers==4.53.2
 + typing-extensions==4.16.0
 + tzdata==2026.5
 + uri-template==1.3.0
 + urllib3==2.8.0
 + wcwidth==0.9.2
 + webcolors==25.10.0
 + webencodings==0.6.1
 + websocket-client==1.9.2
 + widgetsnbextension==4.0.16
(.venv) PS D:\vscode_python_practice> 

 

requirements.txt:

# Core environment
jupyter==1.1.1
notebook==7.4.4
ipykernel==6.29.5

# JSON validation
jsonschema==4.25.0

# Deep learning / GenAI
torch==2.7.1
transformers==4.53.2

# Data handling
numpy==2.3.1
pandas==2.3.0
matplotlib==3.11.2

# Utilities
rich==14.0.0
python-dotenv==1.1.1
requests==2.32.4

 

                                                                 

 

 

 

 

 

                                                                                                                                                                                                                                

 

 

 

 

 

Monday, September 28, 2026

Query to print the cumulative balance at the end of each day, with the total balance reset back to zero at the start of each month

 

💡 "Given all transactions for a merchant account, write a query to print the cumulative balance at the end of each day, with the total balance reset back to zero at the start of each month."
📌 Goal:
-For each day, show cumulative balance up to that day.
-Reset cumulative balance to zero at the start of every month (so no carry forward).

 

 

1. Table Structure

CREATE TABLE transactions (
    transaction_id INTEGER PRIMARY KEY,
    type VARCHAR(20),              -- deposit / withdrawal
    amount DECIMAL(10, 2),
    transaction_date TIMESTAMP
);

2. Sample Data

INSERT INTO transactions
(transaction_id, type, amount, transaction_date)
VALUES
(10001, 'deposit',    500.00, '2022-07-01 09:00:00'),
(10002, 'withdrawal', 150.00, '2022-07-01 15:00:00'),
(10003, 'deposit',    200.00, '2022-07-02 11:00:00'),
(10004, 'withdrawal',  50.00, '2022-07-03 14:00:00'),
(10005, 'deposit',    300.00, '2022-07-03 16:00:00'),
(10006, 'withdrawal', 100.00, '2022-07-04 10:00:00'),
(10007, 'deposit',    400.00, '2022-07-05 12:00:00'),
(10008, 'withdrawal', 200.00, '2022-07-05 18:00:00'),

(10009, 'deposit',    250.00, '2022-08-01 10:00:00'),
(10010, 'withdrawal', 100.00, '2022-08-02 11:00:00'),
(10011, 'deposit',    300.00, '2022-08-03 09:00:00'),
(10012, 'withdrawal',  50.00, '2022-08-03 13:00:00'),
(10013, 'deposit',    150.00, '2022-08-04 15:00:00'),
(10014, 'withdrawal',  75.00, '2022-08-05 16:00:00');

3. Interview Question

Given all transactions for a merchant account, calculate the cumulative daily balance, but reset the cumulative balance to zero at the beginning of every month.

For example:

July

  • July 1 → 500 - 150 = 350

  • July 2 → 350 + 200 = 550

  • July 3 → 550 - 50 + 300 = 800

  • July 4 → 800 - 100 = 700

  • July 5 → 700 + 400 - 200 = 900

When August starts, the balance does not carry forward 900.

August

  • August 1 → 250

  • August 2 → 250 - 100 = 150

  • August 3 → 150 + 300 - 50 = 400

  • August 4 → 400 + 150 = 550

  • August 5 → 550 - 75 = 475

4. SQL Solution

WITH daily_transactions AS
(
    SELECT
        DATE(transaction_date) AS transaction_day,
        DATE_FORMAT(transaction_date, '%Y-%m') AS transaction_month,

        SUM(
            CASE
                WHEN type = 'deposit' THEN amount
                WHEN type = 'withdrawal' THEN -amount
                ELSE 0
            END
        ) AS daily_net_amount

    FROM transactions

    GROUP BY
        DATE(transaction_date),
        DATE_FORMAT(transaction_date, '%Y-%m')
)

SELECT
    transaction_day,
    transaction_month,
    daily_net_amount,

    SUM(daily_net_amount) OVER
    (
        PARTITION BY transaction_month
        ORDER BY transaction_day
        ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
    ) AS cumulative_balance

FROM daily_transactions

ORDER BY transaction_day;

5. Expected Output

transaction_daytransaction_monthdaily_net_amountcumulative_balance
2022-07-012022-07350.00350.00
2022-07-022022-07200.00550.00
2022-07-032022-07250.00800.00
2022-07-042022-07-100.00700.00
2022-07-052022-07200.00900.00
2022-08-012022-08250.00250.00
2022-08-022022-08-100.00150.00
2022-08-032022-08250.00400.00
2022-08-042022-08150.00550.00
2022-08-052022-08-75.00475.00

6. How the SQL Works

Step 1 — Convert withdrawals to negative values

CASE
    WHEN type = 'deposit' THEN amount
    WHEN type = 'withdrawal' THEN -amount
END

So:

deposit     500  → +500
withdrawal  150  → -150

Step 2 — Calculate the daily net amount

SUM(...)
GROUP BY DATE(transaction_date)

For July 3:

Withdrawal = -50
Deposit    = +300

Daily Net = 250

Step 3 — Calculate the running balance

SUM(daily_net_amount) OVER (...)

This produces the cumulative total.

Step 4 — Reset at the beginning of every month

The key part is:

PARTITION BY transaction_month

Conceptually:

July partition
-----------------------------
Jul 1   350
Jul 2   550
Jul 3   800
Jul 4   700
Jul 5   900


August partition
-----------------------------
Aug 1   250    <-- RESET
Aug 2   150
Aug 3   400
Aug 4   550
Aug 5   475

The PARTITION BY creates a separate running-total calculation for each month.

7. PostgreSQL Version

In PostgreSQL, you can simplify the date handling with DATE_TRUNC:

WITH daily_transactions AS
(
    SELECT
        DATE_TRUNC('day', transaction_date)::DATE AS transaction_day,
        DATE_TRUNC('month', transaction_date)::DATE AS transaction_month,

        SUM(
            CASE
                WHEN type = 'deposit' THEN amount
                WHEN type = 'withdrawal' THEN -amount
                ELSE 0
            END
        ) AS daily_net_amount

    FROM transactions

    GROUP BY
        DATE_TRUNC('day', transaction_date),
        DATE_TRUNC('month', transaction_date)
)

SELECT
    transaction_day,
    transaction_month,
    daily_net_amount,

    SUM(daily_net_amount) OVER
    (
        PARTITION BY transaction_month
        ORDER BY transaction_day
        ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
    ) AS cumulative_balance

FROM daily_transactions

ORDER BY transaction_day;

⭐ Interview Takeaway

The important pattern to remember is:

SUM(daily_amount) OVER (
    PARTITION BY month
    ORDER BY day
)

Think of it as:

GROUP BY day → calculate daily net → PARTITION BY month → ORDER BY day → running SUM

This is a very common window-function + aggregation interview problem and is useful for banking, payments, merchant settlements, and financial reporting scenarios.

 

This is one of the classic real-world questions involving cumulative sums and monthly resets, testing both SQL logic and analytical thinking. Let's break it down!

📊 Breaking Down the SQL Solution (Step by Step)
✅ #CTE (WITH): To summarize daily net balances.
✅ #CASE_WHEN: Treat withdrawals as negative, deposits as positive.
✅ #SUM & #GROUP_BY: Aggregate daily net balance.
✅ #DATE & #DATE_FORMAT: Extract day & month for grouping.
✅ Window Function (#SUM_OVER): Cumulative running balance.
✅ #PARTITION_BY: Reset balance per month.
✅ #ORDER_BY: Day-wise accumulation.

🔥 Bonus Tip:
If you're working on PostgreSQL or BigQuery etc. use #DATE_TRUNC('day', transaction_date) or #DATE_TRUNC('month', transaction_date) to achieve similar date grouping!