Create a Python virtual environment in Windows

This post shows how to create a Python virtual environment (VE) to sandbox your coding projects.

Python 3 comes with a library call venv, therefore you do not need to install any modules.

  • Using command prompt, 1st navigate to the folder which you want to store your project’s folders.
  • 2nd, key in ‘python -m venv venv’, the 2nd venv will be the name of the VE, you can change any name that you like. See screenshot below, I have created another VE named ‘ml_score_analysis’. Once created, navigate to the folders, you will see the VE folders which you have created.
  • 3rd, activate VE. You will see a ‘Scripts’ folder in your main VE folder, and inside ‘Scripts’, you will see a ‘activate’ batch file. Key in the command prompt, in this example, ‘venv\Scripts\activate’ or ‘ml_score_analysis\Scripts\activate. You can also navigate to ‘Scripts’ folder using cd command and just key in activate. You will noticed that from the command prompt, you are now in the VE.
  • 4th, to deactivate VE, key in ‘deactivate’ in the cmd prompt, you are now out of the VE.

See the screenshots for the steps mentioned above.

The VE folders will be created in the folder of your project (after Step2):