How to Activate virtualenv on Windows

The virtualenv is one of the core components and tools of Python that encapsulates the executable packages and dependencies of Python projects. This encapsulation feature of virtualenv provides isolation for the Python program. The virtualenv can be easily installed through Python’s pip package manager.

This article will demonstrate:

    • Prerequisites: Installation of Python on Windows
    • How to Install pip on Windows?
    • How to Activate virtualenv on Windows?
    • How to Deactivate virtualenv on Windows?
    • Bonus Tip: Resolve “virtualenv is not recognized as an internal or external command” Error

Prerequisites: Installation of Python on Windows

To install and activate the virtualenv on Windows, you are required to install Python first.

Step 1: Download Python Installer

Visit the Python official website and install the latest version of the Python installer for Windows:


Step 2: Install Python

Navigate to the “Download” directory from your machine. Double-click on the Python installer to start installation:


Mark the “Add python.exe to PATH” checkbox and hit the below highlighted “Install Now” option to install Python:


Here, you can see we have installed Python successfully on Windows:


Step 3: Verification

Start the Command Prompt via “Startup” menu and check the Python version using the provided command:

python --version

 

The latest version “3.11.2” of Python is installed.

How to Install “pip” on Windows?

The virtualenv can be installed through the Python pip package manager. However, pip is already installed as part of the Python installation. If pip is not installed, you can use a different method to install it on Windows by visiting our related article.

To install pip using the “curl” command on Windows, check out the given procedure.

Step 1: Open Command Prompt

Launch the Windows Command Prompt via the Start menu:


Step 2: Download the “get-pip.py” File

Using the “curl” command, read the script file from “https://ift.tt/B16oq3j” and write it down into the new file named “get-pip.py” using -o option as it can not be directly downloaded to the system:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

 

Step 3: Install pip

Execute the “get-pip.py” file using Python command to install pip on Windows:

python get-pip.py

 

For verification, check the pip version:

pip --version

 

It can be noticed that the pip version “23.0.1” is installed on Windows.

How to Activate virtualenv on Windows?

virtualenv is Python’s environment that is used for the isolation of Python projects and dependencies. To activate the virtualenv on Windows, install it first through the “pip” package manager. Activate the virtualenv for the Python project. For the demonstration, follow the listed steps.

Step 1: Install virtualenv

To install the Python virtualenv, use the “pip install virtualenv” command:

pip install virtualenv

 

Step 2: Create Project Directory

After installation, create the Python project directory where you need to start Python development. For this purpose, first, use the “mkdir” command to create the directory. Then, use “cd” to navigate to the newly created directory:

mkdir project-dir
cd project-dir

 
Step 3: Create virtualenv for Project Directory

Create the new virtual environment by utilizing the “virtualenv <environment-name>” command:

virtualenv venvironment

 

Step 4: Activate “venvironment” virtualenv

Next, activate the newly created virtual environment through “<environment-name>\Scripts\activate” command:

venvironment\Scripts\activate

 

The output shows that the virtual environment is activated on Windows.

How to Deactivate virtualenv on Windows?

To deactivate the virtualenv on Windows, simply use the “deactivate” command:

deactivate

 

Bonus Tip: How to Resolve “virtualenv is not recognized as an internal or external command” Error

In some scenarios, the Windows users may get virtualenv recognition error due to various reasons such as:

    • virtualenv is not properly installed on Windows
    • Path variable of virtualenv is not set.

For diagnosing the right reason, first, navigate to the Python Scripts directory by going to the “C:\Users\<username>\AppData\Local\Programs\Python\Python311\Scripts” path and see if the “virtualenv.exe” file exists. If not, this indicates that the virtualenv is not installed on Windows as the environment variable path of virtualenv is set automatically during its installation. To install virtualenv, follow the above section.


If virtualenv is installed but the error is still there; it means that the PATH variable is not set properly and we need to set it manually.

How to Set the PATH Environment Variable of virtualenv Manually?

To resolve the error, follow the instructions below to set the PATH environment variable.

Step 1: Launch Environment Variable Settings

First, search for “Environment Variables” in the search bar of the Start menu and launch the below-highlighted settings:


Next, hit the “Environment Variables” button to open systems environment variables:


Step 2: Edit “Path” Variable

Under the “Users variables”, select the “Path” variable and press the “Edit” button:


Step 3: Set the PATH Variable of virtualenv

After that, hit the “New” button, add the path of the directory where the “virtualenv.exe” file exists, and press the “Ok” button. By default, the “virtualenv.exe” file is saved on the “C:\Users\<username>\AppData\Local\Programs\Python\Python311\Scripts” path:

Note: Make sure to replace the “<username>” with your own system’s username.


This can resolve the virtualenv recognition error if it occurs when activating virtualenv on Windows.

Conclusion

To activate the virtualenv on Windows, first, install Python and pip. Then, install the virtualenv through Python’s pip package manager. After that, move to the project directory, create the virtualenv using the “virtualenv <environment-name>” command, and activate the environment through the “environment-name\Scripts\activate” command. This article has demonstrated how to activate the virtualenv on Windows.



from https://ift.tt/xtkEHsp

Post a Comment

0 Comments