A virtual environment allows you to isolate your project's dependencies and prevent them from conflicting with other projects. This is a good practice to ensure that your development environment is clean and easily manageable. Here's how to create and use a virtual environment for your Django projects.
# Install virtualenv if it is not already installed
pip3 install virtualenv
This will install virtualenv, a tool to create isolated Python environments.
# Create a virtual environment named myenv
python3 -m venv myenv
This command creates a directory named myenv that contains the isolated Python environment.
# Activate virtual environment on Linux or macOS
source myenv/bin/activate
# Activate virtual environment on Windows
myenv\Scripts\activate
Once activated, your terminal shows the environment name.
# Install Django inside the active virtual environment
pip install django
# Exit from the virtual environment
deactivate
Click the buttons to simulate terminal behavior:
requirements.txt for dependency trackingpip freeze to inspect packages