How to Switch Between Python 2 and Python 3 with Virtualenv on Windows
This guide walks you through installing virtualenv, creating a Python 2 virtual environment on Windows, activating it, verifying the interpreter version, and switching back to Python 3, all with step‑by‑step commands and screenshots.
In this tutorial we show how to create and manage a Python virtual environment on Windows, allowing you to switch between Python 2 and Python 3 using virtualenv. Ensure both Python versions are installed before proceeding.
Open a command prompt and run virtualenv -p C:\Python27\python.exe demo to create a virtual environment named demo that uses Python 2. The -p option specifies the interpreter path.
After execution, the demo environment is created (see screenshot).
The environment folder appears in the current directory (see screenshot).
Inspect the Scripts folder; it contains the Python 2 interpreter (see screenshot).
Activate the environment by running activate.bat in the command prompt. The prompt changes to show (demo) at the beginning, indicating the environment is active (see screenshot).
Within the activated environment, run python. The displayed version confirms you are using Python 2 (see screenshot).
To exit, press Ctrl+Z or type exit() to leave Python, then execute deactivate.bat. The prompt returns to normal and the (demo) prefix disappears (see screenshot).
If you create another virtual environment without specifying -p, it defaults to Python 3 (see screenshot).
By following 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.
