Installing Python Packages
In this step, you'll learn to install Python packages both individually and using a requirements file. Ensure you're in your virtual environment (you should see (myproject_env) in your prompt).
Installing Your First Package
Let's install the requests package, which is commonly used for making HTTP requests:
pip install requests
You'll see the download and installation progress. Verify the installation:
pip list
You should see requests in the list of installed packages.
Using a Requirements File
Now let's specify multiple packages with exact versions in your requirements file. Open it:
nano requirements.txt
Add the following content:
requests==2.31.0
numpy==1.24.3
pandas==2.0.3
Save with Ctrl+O, Enter, then exit with Ctrl+X.
This approach ensures consistent environments across different systems by specifying exact package versions.
Installing from Requirements File
Install all packages from your requirements file:
pip install -r requirements.txt
Since requests is already installed, pip will either keep it or update it to match the specified version. The new packages (numpy and pandas) will be installed fresh.
Verify all packages are installed:
pip list | grep -E "(requests|numpy|pandas)"
You should see all three packages with their specified versions.