Stories & Insights

Jan 9, 2026 Linux

🚀 Automatically Activate Python Virtual Environments When Entering a Project Directory (Linux)

If you work on multiple Python projects, you’ve probably typed this command hundreds of times: source venv/bin/activate It’s easy to forget, and running code outside the correct environment can cause confusing bugs. In this article, you’ll learn how to automatically activate a Python virtual environment whenever you enter a specific directory—on any Linux machine or server. 🎯 Why Auto-Activate Virtual Environments? Prevents running code with the wrong dependencies Saves time and mental overhead Works seamlessly with SSH, VS Code Remote, and servers Ideal for Django, FastAPI, Flask, and data projects 🧩 How This Solution Works Bash provides a special hook called PROMPT_COMMAND that runs before each command prompt is displayed. We use this hook to: Detect your current directory Check if a virtual environment is already active Activate the environment automatically when needed 📋 Prerequisites Linux system (Ubuntu, Debian, Arch, etc.) Bash shell Python virtual environment already created Basic terminal access 🛠 Step-by-Step Setup Step 1: Open your .bashrc file nano ~/.bashrc Step 2: Add the auto-activation logic Paste the following code at the end of the file: 🔹 Replace: /full/path/to/your/project with your project directory /full/path/to/your/venv with your virtual environment path Step 3: Reload the shell configuration source ~/.bashrc ✅ What Happens Now? When you cd into your project → virtual environment activates automatically When you open a new terminal inside the project → already activated When you’re outside the project → no activation 🧪 Example Behavior 🧠 Why This Approach Is Safe ✔ No repeated activation ✔ No errors if the virtualenv is missing ✔ No performance impact ✔ Works on remote servers Multiple Projects You can duplicate the function for multiple directories or convert it into a loop. 🆚 Alternative: direnv Tools like direnv can also do this, but they require extra installation and configuration. This Bash-only approach is lightweight, portable, and dependency-free. 🧩 Supported Environments Ubuntu 20.04 / 22.04 Linux servers SSH sessions VS Code Remote SSH WSL (Linux mode) 🏁 Final Thoughts Automating virtual environment activation is a small change that dramatically improves your daily Python workflow. Once you set it up, you’ll wonder why you didn’t do it sooner.

🚀 Automatically Activate Python Virtual Environments When Entering a Project Directory (Linux)
Dec 18, 2025 Python

How to Install Python 3.12 on Ubuntu 22.04

How to Install Python 3.12 on Ubuntu 22.04
Dec 18, 2025 GIS

Working with ESRI Shapefiles using GDAL / OGR

This guide covers common OGR/GDAL command-line operations for working with ESRI Shapefiles and PostGIS databases, including inspection, reprojection, format conversion, querying, and database updates. Identify the Geometry Type of a Shapefile ogrinfo Bittner_BellePrairieSec8_N40_Boundary.shp This command lists layers and shows the geometry type (Polygon, Point, etc.). Get Full Details of a Shapefile (Schema + Metadata) ogrinfo -so Bittner_BellePrairieSec8_N40_Boundary.shp \ -sql "SELECT * FROM Bittner_BellePrairieSec8_N40_Boundary" -so → summary only (no feature dump) Shows fields, geometry type, and spatial reference Get Full Details of a Specific Record (by FID) ogrinfo Bittner_BellePrairieSec8_N40_Boundary.shp \ -sql "SELECT * FROM Bittner_BellePrairieSec8_N40_Boundary WHERE fid = 1" Reproject an Existing ESRI Shapefile ogr2ogr ../newShp/Bittner_BellePrairieSec8_N40_Boundary.shp \ -t_srs EPSG:4326 \ Bittner_BellePrairieSec8_N40_Boundary.shp Converts projection to WGS84 (EPSG:4326) Convert an ESRI Shapefile to GeoJSON ogr2ogr -f GeoJSON \ ../newShp/Bittner_BellePrairieSec8_N40_Boundary.json \ -t_srs EPSG:4326 \ Bittner_BellePrairieSec8_N40_Boundary.shp Select Records and Create a New Shapefile ogr2ogr ../newShp/filtered.shp \ Bittner_BellePrairieSec8_N40_Boundary.shp \ -sql "SELECT * FROM Bittner_BellePrairieSec8_N40_Boundary WHERE name = 'Apple'" Count Records in a Shapefile ogrinfo Bittner_BellePrairieSec8_N40_Boundary.shp \ -sql "SELECT COUNT(*) FROM Bittner_BellePrairieSec8_N40_Boundary" Count DISTINCT Records in a Shapefile ogrinfo Bittner_BellePrairieSec8_N40_Boundary.shp \ -sql "SELECT COUNT(DISTINCT name) FROM Bittner_BellePrairieSec8_N40_Boundary" Clip Spatial Data (Points within Polygon) ogr2ogr ../newShp/clipped.shp \ ../esridata/points.shp \ -clipsrc Bittner_BellePrairieSec8_N40_Boundary.shp Note: Input geometry must be POINT Clip geometry must be POLYGON OGR & PostGIS Queries Connect to PostGIS and List Tables ogrinfo PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1" Extract Data from PostGIS to GeoJSON ogr2ogr -f GeoJSON \ ../newShp/trees.geojson \ PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1" \ -sql "SELECT * FROM bittner_belleprairiesec8_n40_boundary" Extract Data from PostGIS to an ESRI Shapefile ogr2ogr -f "ESRI Shapefile" \ ../newShp/trees.shp \ PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1" \ -sql "SELECT * FROM bittner_belleprairiesec8_n40_boundary" Extract & Reproject PostGIS Data to EPSG:32643 ogr2ogr -f "ESRI Shapefile" \ ../newShp/trees.shp \ -t_srs EPSG:32643 \ PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1" \ -sql "SELECT * FROM bittner_belleprairiesec8_n40_boundary" Extract Specific Fields & Records from PostGIS ogr2ogr -f "ESRI Shapefile" \ ../newShp/trees.shp \ -select name \ PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1" \ -sql "SELECT * FROM bittner_belleprairiesec8_n40_boundary WHERE name = 'mango'" Insert Shapefile Records into PostGIS ogr2ogr -f PostgreSQL \ PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1" \ Bittner_BellePrairieSec8_N40_Boundary.shp Add a New Column to a PostGIS Table ogrinfo PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1" \ -sql "ALTER TABLE bittner_belleprairiesec8_n40_boundary ADD COLUMN surveyor TEXT" Update PostGIS Table Records Using WHERE Clause ogrinfo PG:"host=localhost port=5432 user=postgres password=postgres dbname=landgate_local_db1" \ -dialect SQLite \ -sql "UPDATE bittner_belleprairiesec8_n40_boundary SET surveyor = 'Akash' WHERE gid = 1" Conclusion OGR/GDAL provides a powerful CLI for: Inspecting spatial data Reprojecting layers Converting formats Querying spatial datasets Integrating Shapefiles with PostGIS These commands are essential for GIS developers, data engineers, and backend developers working with spatial data.

Working with ESRI Shapefiles using GDAL / OGR
Dec 18, 2025

Step-by-Step Guide to Reset PostgreSQL User Password for Specific Version

To reset the password for a PostgreSQL user for a specific version of PostgreSQL on your Ubuntu system, you'll need to follow these steps. The process involves connecting to the correct instance of PostgreSQL and utilizing the psql command-line interface. 1) Identify the Correct PostgreSQL Version and Port: $ pg_lsclusters 2) Connect to the Correct Version: $ sudo -u postgres psql -p PORT 3) Change the User Password: Once connected to the PostgreSQL prompt, you can reset the password of a specific PostgreSQL user by executing the following SQL command: $ ALTER USER your_username WITH PASSWORD 'new_password'; 4) Exit the PostgreSQL Shell: After you successfully change the password, you can exit the psql prompt by typing: $ \q Notes: Make sure that the user you are trying to modify exists in that specific PostgreSQL instance. You can check existing users by running\duin the PostgreSQL prompt. If you encounter permission issues while connecting, ensure that you’re using the correct PostgreSQL superuser (usually postgres) to execute the ALTER USERcommand. If PostgreSQL is configured to use the peer authentication method, you may have to change the authentication method in the pg_hba.conf file for the postgres user to md5in order to use password authentication. Summary By connecting to the specific PostgreSQL version and using the appropriate SQL command, you can easily reset any user's password on your Ubuntu system. Always make sure to use a secure password, especially in production environments.

Step-by-Step Guide to Reset PostgreSQL User Password for Specific Version
Dec 18, 2025

Django Model Objects - CheatSheet

One must know how to play with Django models in their views in order to create efficient and short functions. Let's take a model for example. class Teacher(models.Model): name = models.CharField(max_length=100) class Student(models.Model): name = models.CharField(max_length=100) roll = models.CharField(max_length=100) mentor = models.ForeignKey(Teacher, on_delete=models.CASCADE) reg_date = models.DateTimeField(auto_add_now=True) Extracting all objects of a model Let's extract all the students. students = Student.objects.all() Extracting a student by ID ID is the primary key in every model. from django.shortcuts import get_object_or_404 def my_view(request): obj = get_object_or_404(MyModel, pk=1) Or there is another way to do this. stud = Student.objects.get(pk=1) The last one returns a error in case a student doesn't exist with the following id. Filtering the objects Simple filtering can be done with equating like studs = Student.objects.filter(name='Ram Kapoor') This will return the list of students whose name is Ram Kapoor.We can also refer to the details of an attribute with the symbol __. stud_2006 = Student.objects.filter(reg_date__year=2006) This will return all the students registered in 2006. stud_p = Student.objects.filter(name__startswith='P') This will return all the students whose names start with 'P'. Using Q() - Very Powerful This is used to add many filters in a single filter using | (or), & (and). stud = Student.objects.filter(Q(name__startswith='P') | Q(reg_date__year=2006)) This will return both the students whose names start with 'P' or who are registered in year 2006. THANK YOU Find me on LinkedIn