Virtual Environment on Windows

By default, it is not possible to activate a Python virtual environment in Windows. The Execution Policy must be set. My question is: which Execution Policy level do Django developers use?

Seems there are 3 general options:

  1. Allow all scripts with Set-ExecutionPolicy Unrestricted -Force though that seems a bit unsafe.
  2. Set per PowerShell session Set-ExecutionPolicy Unrestricted -scope process though a major pain
  3. Set per user Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser which is what the Python docs seem to recommend
  4. Just use Docker :slight_smile:

My guess is #3 is what most folks would do but just curious, especially if you were telling a junior Python/Django developer how to configure their machine?

PS. I’m asking because of adding full Windows support to my Django books for 3.2 and it’s quite the effort to do.

1 Like

Option 5: Don’t use power shell, work in the standard command prompt.

Option 6: Work from within the context of your IDE (e.g. Visual Studio)

Option 7: Work within the WSL environment

Option 8: Work within a virtual machine (VirtualBox or VMWare)

I use options 5-8 depending upon the specific project.

2 Likes

:grinning: I agree Ken but all the options are overwhelming to someone new to all this. That’s why I’m using PowerShell vs standard prompt or even Windows Terminal. It’s quite a challenge to make these things “simple” and not overwhelm newbies.

I do think that for my own usage, I’d just go Docker and then WSL. I “think” I’m going to mimic Python and just include the CurrentUser command in the setup. Just feels like one more hurdle on Windows vs Mac.

We take a slightly different approach. Since all our deployments are on Linux systems, our new developers start out with using WSL. That gets them started right away with learning the production environments and avoids learning things that only apply to their development environments.

1 Like

Yeah. I agree with that in a team environment. In terms of teaching it’s always a question of when to present complexity to people. In my own case–at least for books and online tutorials–I want to succinctly/accurately cover installing Python and get into Django itself as quickly as possible. Means a lot of hand waving around Python, operating systems, etc though :slight_smile:

1 Like

Because of muscle memory, I wrote up a shortcuts to use venv instead of virtualenv mimicking virtualenvwrapper, but just for Linux: Shortcuts for Python 3’s venv for virtualenvwrapper users. (github.com)

On the GIST, Vince Salvino put in a comment with similar function for PowerShell. Would this help? See: Shortcuts for Python 3’s venv for virtualenvwrapper users. (github.com)

I’ll ping Vince on Twitter about this thread, since it seems to be in his wheelhouse.

1 Like

Worth noting that Brett Cannon weighed in with Option 3, 7 (WSL), or PEP 582

Thanks @jeff for the Tweet.

1 Like

Windows 10 has a “Developer Mode” setting (in the settings GUI) which automatically turns on the necessary PowerShell knobs. It also enables SSH command line tools, shows file extensions (rookies get really confused why their files do not say .py because File Explorer hides extensions by default) and a lot of other stuff that is a PITA to set up yourself on Windows.

Settings > Updates & Security > For Developers: Then check the PowerShell box and any other boxes and click apply.

For doing Python development, you will also most likely need a C compiler for installing pip packages. So I would recommend installing Visual Studio Community 2019 (which is the free version for individuals and small teams). The installer has a checkbox for Python development which installs everything you need.

Full info on “Developer Mode”: Developer Mode features and debugging - Windows apps | Microsoft Docs


Once set up, we have our junior devs just manually create and use venvs, because it is so much simpler than trying to teach people of varying skill levels the ins and outs of tools like pyenv/poetry. venv is included with Python and “just works”.

After you first git clone the project:

  1. In PowerShell, cd to project folder.
  2. python -m venv .venv

Every time you work on the project:

  1. In PowerShell, cd to project folder.
  2. .\.venv\Scripts\Activate.ps1
  3. pip install -r requirements.txt