Monday, August 25, 2025

Debian 13: pyhton3 setting virtual environment

Run once

Installing packages

As root

# sudo apt install python3 python3-pip python3-venv

As user for example user1

As user1

We create folder to store python3 packages for user1 folder name is mpyvenv

$ mkdir mypyenv
$ cd mypyenv
~/mypyenv$  python3 -m venv venv
~/mypyenv$ ls
venv

Every time entering virtual environment use this command

$ cd mypyenv
~/mypyenv$ source venv/bin/activate
(venv) [user]@[host]:~/mypyenv$

To exit virtual environment

(venv) [user]@[host]:~/mypyenv$ deactivate
~/mypyenv$ 

Bash script to get into virtual environment

#!/bin/bash

# Change to the spider_playwright directory
cd ~/[your_venv_directory] || { echo "Directory not found!"; exit 1; }

# Check if we successfully changed the directory
echo "Changed to $(pwd). Activating virtual environment..."

# Activate the virtual environment
source venv/bin/activate || { echo "Activation failed!"; exit 1; }

echo "Virtual environment activated. You are now in (venv)."
exec bash

Recommendation installing pip-autoremove 

(venv) [user]@[host]:~/mypyenv$ pip install pip-autoremove 

To remove package and all depedency e.g cloudscraper

(venv) [user]@[host]:~/mypyenv$ pip-autoremove cloudscraper -y

Note

  1. put every python project under directory virtual environment, for this example I used mypyenv.
  2. A user can have multiple virtual environment for each project. Each project must have a single entry point to root folder. 

References: chatgpt.com gemini.google.com