Setting up a Python Virtual Environment on Windows (September 2023)

Rodolflying
3 min readSep 20, 2023

--

In the expansive world of Python development, the importance of virtual environments is often underestimated, especially by beginners. Without these isolated spaces, developers risk a tangled web of library dependencies, where one project’s requirements can interfere with another’s. Imagine working on multiple projects, each relying on different versions of a library. Installing a library globally might work for one project, but what if it breaks another?

For a long time, I too navigated the Python ecosystem without the safety net of virtual environments. It seemed like an optional layer of complexity — until it wasn’t. As I juggled multiple projects, I found myself ensnared in a mess of conflicting library versions. Past projects, reliant on older library versions, suddenly became incompatible with newer installations. Errors became frequent visitors, and the time spent troubleshooting surged.

This article aims to spare you from learning the hard way, as I did. By setting up and using virtual environments, you can ensure each project remains in its own contained space, free from the chaos of conflicting dependencies.

1. Install Python:

  • Use the Python installer for Windows which fetches necessary components during installation. https://www.python.org/downloads/
  • For bundled software needs, Python redistributable files are available.
  • Ubuntu users? Python3 is already onboard. Check using:
which python3

2. Setting Up PIP:

Python3 usually has pip. But if you see “pip command not found”:

Download get-pip.py with:

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

Save the file (e.g., to Desktop). Install pip:

cd Desktop 
python3 get-pip.py

3. Setting Up Virtualenv:

  • Install with:
pip install virtualenv
  • Navigate to your project (e.g., my_project) and set up. You can choose the name of the virtualenv also (In this case it’s simply “myenv”)
cd my_project 
virtualenv myenv

4. Activating Virtualenv:

Depending on your operating system, the script to activate the virtual environment will differ:

Windows:

Use the activate.bat located in the \venv\Scripts\ directory:

C:\Users\'Username'\venv\Scripts\activate.bat

Note on PowerShell Execution Policy:

If you encounter a policy issue in PowerShell, Run PowerShell as admin. Change the policy:

Set-ExecutionPolicy RemoteSigned

To revert the policy back to its original setting (after you’re done with your tasks):

Set-ExecutionPolicy Restricted

Linux/Mac:

  • Use the activate script located in the venv/bin/ directory:
source venv/bin/activate

When you activate a virtual environment with virtualenv, the prompt of your terminal or command line should change to reflect the name of the activated virtual environment.

For instance, if you created a virtual environment named myenv, after activating it, your prompt might look something like this:

  • Linux/Mac:
(myenv) username@machine-name current-directory$
  • Windows:
(myenv) C:\path\to\current\directory

The (myenv) at the beginning of the prompt indicates that the myenv virtual environment is active. While this environment is active, any Python packages you install using pip will be installed only within that environment, not affecting other environments or the system-wide Python.

When you deactivate the virtual environment using the deactivate command, the prompt will return to its normal state, without the (myenv) prefix.

5. (Totally Alternative) Using virtualenvwrapper-win:

Yes, you can absolutely use virtual environments without virtualenvwrapper-win. The core functionality of creating and using virtual environments is provided by virtualenv itself.

virtualenvwrapper-win (and its Unix counterpart virtualenvwrapper) simply offers some convenience tools and commands to manage multiple virtual environments. For instance, it makes it easier to switch between different environments, list all available environments, and remove environments you no longer need.

  • Install using pip:
pip install virtualenvwrapper-win

Or from source:

git clone git://github.com/davidmarble/virtualenvwrapper-win.git 
cd virtualenvwrapper-win
python setup.py install

Conclusion:

Setting up a Python virtual environment, especially on Windows, can seem daunting at first glance. However, with the steps outlined in this article, you’re well-equipped to create isolated environments for your projects with ease. While tools like virtualenvwrapper-win offer added conveniences, it's essential to remember they are optional. The core of virtual environments lies in virtualenv, ensuring your projects remain free from dependency clashes and system-wide disruptions. By embracing virtual environments, you not only enhance the organization and structure of your projects but also ensure reproducibility and consistency across different systems. Happy coding!

--

--

Rodolflying

Industrial Engineer. I find inspiration in data science and technology to solve real-life problems. https://www.linkedin.com/in/rodolfo-sepulveda-847532135/