
pip, and isolate your projects using virtual environments via venv. Step 1: Installing PythonWindows
++ Visit the official Python website.
++ Download the latest stable version (e.g., Python 3.12).
++ Run the installer and check the box that says _"Add Python to PATH"_.
++ Click “Install Now” and follow the on-screen instructions.
++ To verify installation, open Command Prompt and type:
python --versionmacOS
++ Install Homebrew if not already installed.
++ Open Terminal and run:
brew install python++ Verify by typing:
python3 --versionLinux (Ubuntu/Debian)
++ Open Terminal.
++ Update packages:
sudo apt update++ Install Python:
sudo apt install python3 python3-pip++ Verify with:
Step 2: Installing pip (Python’s Package Installer)Pip is a package manager for Python that allows you to install and manage additional libraries and dependencies that are not part of the standard Python library. python3 --versionpip usually comes pre-installed with Python 3.4+.Verify by running:
BASH
pip --version
If it's not installed, try one of the following methods:
1. Using Python's Built-in Method
BASH
python -m ensurepip --upgrade2. Using get-pip.py
Download and run the script: BASH
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Or:
BASH
wget https://bootstrap.pypa.io/get-pip.py
Then run it with Python:
BASH
python get-pip.py3. Verifying the Installation
BASH
pip --version
Step 3: Creating and Using Virtual Environments
Why use virtual environments?
A virtual environment is an isolated Python environment that allows you to install packages specific to a project without affecting the global installation. It avoids conflicts and makes project management cleaner.Creating a Virtual Environment
BASH
python -m venv myenv
This will create a folder named
myenv containing a standalone Python setup.Activating the Virtual Environment
* Windows:myenv\Scripts\activate
* macOS/Linux: source myenv/bin/activateOnce activated, your terminal should show the environment name like this: (myenv)Installing Packages with pip
BASH
pip install numpy pandas matplotlibFreezing Requirements
To share your environment, create arequirements.txt file:
BASH
pip freeze > requirements.txtInstalling from requirements.txt
BASH
pip install -r requirements.txtDeactivating the Virtual Environment
BASH
deactivate
Tips and Best Practices* Use one virtual environment per project to avoid dependency conflicts.
* Keep your Python version up-to-date, but stable (avoid beta releases).
* Use descriptive names for your environments:
venv-data-science, venv-webapp, etc.
* Regularly back up your requirements.txt. Need Help?If you're a DTU student and need assistance, visit the DTU Python Support site or contact your nearest support assistant.Happy Coding! 🐍
