Activating a Virtual Environment in Windows 10 Command Prompt

Activate Virtual Environment

In this article, we will learn how to create and activate a virtual environment in Windows 10 using the command prompt. Before we start with deploying the virtual environment, we will also look at what exactly is a virtual environment and what is it used for.

What is a Virtual Environment and Why is it Used?

Python packages for numerous projects are managed through the virtual environment. By using a virtual environment, you may prevent installing Python packages globally, which might disrupt system tools or other projects.

The main objective of using a virtual environment for a particular project is to avoid any sort of disturbance caused related to packages – their installation, versions, etc.

Common installation tools like pip – the Python package manager, will automatically help to install the Python packages into a virtual environment when used from within a virtual environment without being expressly instructed to do so.

A virtual environment is built on top of an already installed Python installation, this already existing installation serves as the base for the created virtual environments, and it may be separated from the base environment’s packages so that only those specifically installed in the virtual environment are accessible.

We’ve already covered virtual environments in detail if you are new to the topic.

Creating a Virtual Environment in Windows 10

To create a Python virtual environment in Windows, open the command prompt and navigate to the desired directory using the “cd” command followed by the path. Once in the directory, run “python -m venv [name of the virtual environment]” or “python3 -m venv [name of the virtual environment]” if the first command throws an error. This will create the virtual environment in the specified directory.

Following are the steps one should follow to create a virtual environment in Windows 10.

  • Open your Windows command prompt. You can do so by pressing the windows key and type ‘cmd’
  • Head to the directory/ path where you want to create the virtual environment using the change directory – “cd” command followed by the path of your choice.
  • Finally, run the following line of code in your command prompt
    • python -m venv [name of the virtual environment]
  • Note, if the above command throws an error, then try running the following line of code instead.
    • python3 -m venv [name of the virtual environment]
Creating Venv
Creating Virtual Environment

Activating the Virtual Environment in Windows 10

Before activating the virtual environment make sure to change the current directory. Run the following line of code in your command prompt to do so.

cd [name of the virtual environment]

Change Path To Venv
Change Path To Virtual Environment

Your virtual environment needs to be first activated before you can begin installing or using any packages. The virtual environment is enabled to behave as the shell session’s default Python interpreter after its activation.

Depending on what operating system and command shell you’re working on, you’ll need to use different syntax to activate the virtual environment. Following is the line of code executed in the Windows 10 command prompt to activate the virtual environment.

Scripts\activate

Once the virtual environment gets activated, the (virtual environment's name) will appear at the very start of every line in the command prompt

Activating Venv
Activating Virtual Environment

Deactivating the Virtual Environment

As a good habit, you should always deactivate the virtual environment after you are finished using it in order avoid any potential problems. To deactivate the virtual environment and avoid potential issues, simply type ‘deactivate‘ in the command prompt. After deactivation, the virtual environment’s name will no longer appear at the beginning of each line.

Conclusion

Using a virtual environment is not only beneficial but also considered a best practice among programmers. The syntax for virtual environment-related operations depends on the operating system and command shell being used. In this article, we learned how to create, activate, and deactivate a virtual environment in Windows 10 using the command prompt. Are there any other best practices you follow when working with Python virtual environments?

References

Official Document – Python Documentation of the venv module.