How do I activate a virtual environment with steps?
Activating a virtual environment involves running a specific script or command to set up your terminal session to use the Python interpreter and packages from within that virtual environment. Below are the steps to activate a virtual environment on different operating systems using the terminal. For this example, I'll use the common virtual environment folder namevenv
, but you can replace it with the name of your virtual environment.
On Windows:
Open the terminal in VS Code (`Ctrl + ``).
Navigate to your project directory.
cd path\to\your\project
Activate the virtual environment:
.\venv\Scripts\activate
After activation, your terminal prompt should change to something like
(venv) PS C:\path\to\your\project>
, indicating that the virtual environment is active.
On macOS and Linux:
Open the terminal in VS Code (`Ctrl + ``).
Navigate to your project directory:
cd path/to/your/project
Activate the virtual environment:
source venv/bin/activate
After activation, your terminal prompt should change to something like
(venv) user@hostname:path/to/your/project$
, indicating that the virtual environment is active.
Notes:
When the virtual environment is active, the terminal prompt typically includes the virtual environment name in parentheses, as mentioned in the examples.
To deactivate the virtual environment and return to the global Python environment, you can use the
deactivate
command on both Windows and macOS/Linux.Remember to activate the virtual environment whenever you work on your project. You may find it helpful to use tools like Pipenv or poetry for more advanced dependency management along with virtual environments.