Creating isolated Python environments is an essential skill for Python developers. Virtualenv allows you to create separate Python environments for each project, allowing you to install packages and manage dependencies without affecting other projects.
However, Python developers frequently encounter frustrating issues when activating their virtualenv environments on Windows. In this comprehensive guide, we’ll explore the common activation errors and provide fixes to get your virtual environments running correctly.
Why Use Virtualenv?
Before diving into solutions, let’s briefly go over why virtualenv can be so useful:
- Isolate dependencies across projects – Each project can have its virtualenv with its own independent set of packages. No version conflicts across projects.
- Replicate production environments locally – Mimic the same Python version and packages in dev/test as you have in production.
- Keep your global Python clean – Avoid installing lots of libraries directly into your base Python install.
As you can see, virtualenv brings some major benefits to Python project management. Now, let’s look at how to properly setup and activate virtualenvs on Windows.
Common Virtualenv Activation Errors
When creating your virtualenv with
python -m venv myenv, you may encounter various errors when trying to activate it with
myenv\Scripts\activate on Windows:
ERROR: Cannot activate virtual environment
'source' is not recognized as an internal or external command
.\env\Scripts\activate : File not found
.\env\Scripts\activate.ps1 cannot be loaded because running scripts is disabled
These errors generally fall into three categories – file permissions, command syntax, or execution policy restrictions. Let’s explore each one…
File Permission Issues
If you receive “access denied” errors when running the activation script, check the file permissions in your virtualenv’s
.\myenv\Scripts\activate : Access is denied
To fix, grant yourself full control permissions:
- Right click the
- Select Properties > Security > Edit
- Grant full permissions to your user
After updating the permissions, the activation script should run successfully.
Activation Command Syntax
Another common mistake is using the wrong command syntax to activate the virtualenv.
On Windows, you need to run the
activate batch script. So use:
Do not try to use the
source command or explicitly call
activate.ps1/activate.bat. Those will result in “command not found” errors.
Restrictive Execution Policy
If PowerShell prevents you from running the activation script with an error like:
.\env\Scripts\activate.ps1 cannot be loaded because running scripts is disabled on this system.
This means PowerShell’s execution policy is blocking the virtualenv script.
To fix, run PowerShell as administrator and use:
Set-ExecutionPolicy RemoteSigned -Scope Process
This will allow scripts to execute in your current PowerShell session.
Step-by-Step Guide to Use Python Virtual Environments in Windows
Now that you understand some of the common activation issues let’s walk through the full steps to create and activate a virtualenv properly on Windows 10:
1. Install Virtualenv
First, make sure you have the virtualenv package installed globally. I have macOS, so the screenshots will be different. However, the commands will be the same.
pip3 install virtualenv
This will enable the
python3 -m venv command we’ll use next.
2. Create the Virtualenv
Navigate to your project directory and create a virtualenv called
python -m venv new_venv
This will create a new folder called
new_venv containing the virtual environment.
3. Activate the Virtualenv
Activate the virtualenv by running:
Your command prompt should now be prefixed with the virtualenv name
(new_venv) indicating activation.
4. Install Packages
With the virtualenv active, use pip to install packages as needed:
pip install numpy pandas
The packages will install isolated within this virtualenv.
5. Deactivate (Optional)
When done working in the virtual environment, run:
This will restore your original PATH and environment variables.
Also read: How to Delete a Virtualenv in Python 3.x
And that’s it! Creating, activating, and deactivating virtualenvs on any operating system can be tricky at first, but once you understand the common pitfalls, hopefully, the process becomes second nature.
Here is a quick reference troubleshooting guide for resolving additional virtualenv issues you may encounter:
|Access is denied
|Restrictive file permissions
|Grant user full control of
|Command not found
|Using wrong activation command
|cannot be loaded – running scripts is disabled
|Execution policy restriction
Set-ExecutionPolicy RemoteSigned -Scope Process
|Already installed errors
|Existing system-wide packages
--no-site-packages flag when creating virtualenv
Handling Existing System Packages
If you receive errors indicating a package exists even within your virtualenv, the issue may be that your environment is still accessing system-wide packages.
When creating the virtualenv, include the
python -m venv myenv --no-site-packages
This will ensure complete isolation from any globally installed libraries.
As you can see, while virtualenv brings tons of benefits for Python dependency and environment management, getting set up initially on Windows can present some unique frustrations.
Hopefully by understanding the main activation errors related to file permissions, command syntax, execution policy restrictions, and more – you’re now equipped to easily get your virtualenvs running properly.
The key is to start simple, validate each step, and incrementally address issues. With consistent practice, you’ll have virtualenv creation and activation mastered in no time!
So go forth and develop with isolated, reproducible Python environments 🙂
Let me know in the comments if you have any other virtualenv challenges on Windows!