Wednesday, December 20, 2023

Creating and Using Virtual Environments in Visual Studio Code on macOS

Virtual environments are crucial for managing Python project dependencies. In this tutorial, we'll explore how to create and activate a virtual environment within Visual Studio Code on macOS.

Step 1: Open Visual Studio Code

Launch Visual Studio Code and open your Python project.

Step 2: Open a New Terminal

Go to the "View" menu and select "Terminal" or press Ctrl + ` to open a new terminal in Visual Studio Code.

Step 3: Create a Virtual Environment

python3 -m venv venv

This creates a virtual environment named venv. Make sure you have Python 3 installed; if not, use Homebrew to install it (brew install python).

Step 4: Activate the Virtual Environment

source venv/bin/activate

Your terminal prompt should change, indicating that you are now in the virtual environment.

Step 5: Install Dependencies (Optional)

While in the virtual environment, use pip to install project-specific dependencies:

pip install package_name

Step 6: Deactivate the Virtual Environment

Once done, deactivate the virtual environment:

deactivate

This returns you to the global Python environment.

Conclusion

Creating and using virtual environments in Visual Studio Code provides a clean space for managing project dependencies, crucial for smooth development.

Happy coding!

No comments:

Post a Comment