Activity 18: Research Virtual Environment and Python package manager (SIA2)
How to Create and Use Virtual Environments
Create a Virtual Environment: Run the following command in your terminal to create a virtual environment:
python -m venv myenv
This creates a folder (
myenv
) containing a Python interpreter and a separate package directory.Activate the Virtual Environment:
On Windows:
.\myenv\Scripts\activate
On Mac/Linux:
source myenv/bin/activate
After activation, your terminal prompt changes, indicating the virtual environment is active.
Install Dependencies: Install packages using pip, and they’ll only be available in this environment:
pip install package_name
Deactivate the Environment: When done, simply deactivate it by typing:
deactivate
Best Practices: Managing Dependencies
To ensure that your virtual environment is not uploaded to GitHub or other cloud repositories, follow these steps:
Use .gitignore to Exclude the Virtual Environment: Create or edit a
.gitignore
file in your project root and add the following line:# Ignore the virtual environment folder venv/
This prevents Git from tracking your
venv
folder, keeping your repository clean.Generate a
requirements.txt
file: To save all dependencies, run:(Make sure the Virtual Environment is activated.)
pip freeze > requirements.txt
This file lists all installed packages with their versions.
Clone or Update the Repository with Dependencies: After cloning or pulling the repository, create a virtual environment and install dependencies with:
(Make sure to crate you repository in GitHub then copy the URL link to connect your local project to your GitHub repository)
pip install -r requirements.txt
LINK
REPOSITORY:
https://github.com/MonetForProgrammingPurposes/my-python-project
Subscribe to my newsletter
Read articles from Monette Nicolas directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by