Python Specific Version Pip Install, VE Best Practices
🔹 How to Install Global Packages in a Specific Python Version?
By default, when you run pip install <package>
, it installs modules for the currently selected Python interpreter. If you want to explicitly install packages for a specific Python version, you need to use:
<full_python_path> -m pip install <package>
Example: Installing a package for Python 3.12
Run this in the VS Code terminal (or any command prompt):
C:/Users/user/AppData/Local/Programs/Python/Python312/python.exe -m pip install beautifulsoup4
This ensures that beautifulsoup4
is installed for Python 3.12 instead of another version.
🔹 Setting the Default Python Version for pip
in VS Code Terminal
If you want all pip install
commands to install packages for Python 3.12 by default, do this:
- Open the VS Code terminal (
Ctrl + ~
). - Check which Python is being used:
python --version
- If it returns Python 3.12, you’re good to go! 🎯
- If not, follow these steps:
- Manually specify the Python version for the current terminal session:
$env:Path = "C:\Users\user\AppData\Local\Programs\Python\Python312;" + $env:Path
- Then verify with:
python --version
🔹 Alternative: Use Virtual Environments (Recommended)
If you’re working on multiple projects, a virtual environment (VE) prevents conflicts between different Python versions and packages. Instead of installing packages globally, you can create an isolated environment.
- Create a virtual environment:
python -m venv venv
- Activate it:
- PowerShell:
venv\Scripts\Activate
- Command Prompt (cmd):
venv\Scripts\activate.bat
- Install packages inside VE:
pip install beautifulsoup4
- Deactivate the VE:
deactivate