How to Create a Specific Python Virtual Environment on Windows
This guide shows how to use the virtualenv tool on Windows to create a Python 2 virtual environment by specifying the interpreter path, activate it, verify the Python version, and then deactivate it, with step‑by‑step commands and screenshots.
Previously I explained how to create a default virtual environment on Windows; this article focuses on creating a virtual environment with a specific Python version.
First, ensure both Python 2 and Python 3 are installed on the machine. The virtualenv package is required, which was covered in the earlier article.
Step 1: Run the command virtualenv -p C:\Python27\python.exe demo in the command line. The -p option specifies the interpreter path, and demo is the name of the new environment.
Step 2: After execution, the demo folder appears, indicating the environment has been created.
Step 3: The environment is created in the current directory; the folder structure can be seen in the file explorer.
Step 4: Inside the Scripts subfolder, the Python executable is the Python 2 version, confirming the correct interpreter was used.
Step 5: Activate the environment by running activate.bat in the command line. The prompt changes to show (demo) at the beginning, indicating the environment is active.
Step 6: Within the activated environment, run python to verify that the interpreter version is Python 2.
Step 7: To exit, press Ctrl+Z or type exit() to leave the Python REPL, then run deactivate.bat. The prompt returns to normal, confirming the environment is deactivated.
Step 8: Creating another virtual environment without the -p option defaults to Python 3, as demonstrated in the final screenshot.
With these steps, you can freely switch between Python 2 and Python 3 virtual environments on Windows.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
