Sure, here’s how to create a virtual environment in Python on Windows:
- Open a Command Prompt window: Press
Windows + Ron your keyboard to open the Run dialog box, typecmdand press Enter. - Navigate to the directory where you want to create the virtual environment: Use the
cdcommand to change directory. For example, if you want to create the virtual environment in a folder namedmyenvon your desktop, you would use the following command:
cd C:\Users\YourUsername\Desktop\myenv
Create a virtual environment: Use the python command to create a virtual environment with the venv module. Specify the name of the virtual environment folder that you want to create after the python -m venv command. For example, to create a virtual environment named myvenv, you would use the following command:
python -m venv myvenv
- Activate the virtual environment: To use the virtual environment, you need to activate it first. Use the following command to activate the virtual environment:
myvenv\Scripts\activate.bat
You should see the name of the virtual environment in parentheses before the prompt in the Command Prompt window.
That’s it! You have now created and activated a virtual environment in Python on Windows. You can now install packages and run Python code in the virtual environment without affecting the global Python installation on your system. To exit the virtual environment, simply run the deactivate command.
