Python Virtual Environments
Steps to Create and Use a Virtual Environment:
Open a Terminal or Command Prompt
Navigate to Your Project Directory (Optional): If you are working on a specific project, navigate to the project folder where you want to create the virtual environment
$ cd path/to/your/project
Create the Virtual Environment: Run the following command to create a virtual environment. The recommended method is to use
python -m venv
followed by the name you want to give to your virtual environment (usuallyvenv
orenv
):
$ python -m venv venv
This will create a venv
directory containing the virtual environment files in your current folder.
Activate the Virtual Environment: After creating the virtual environment, you need to activate it.
On Windows:
$ venv\Scripts\activate
On macOS and Linux:
$ source venv/bin/activate
Install Packages: Once the virtual environment is activated, you can install packages using
pip
. For example:
$ pip install package_name
Deactivate the Virtual Environment: When you're done working in your virtual environment, you can deactivate it by running:
$ deactivate
Using the Virtual Environment: Each time you want to work on your project, you'll need to reactivate the virtual environment by running the activation command again.
Last updated
Was this helpful?