Skip to content

Create a Python 3+ Virtual Environment in Windows 10

Posted on:August 2, 2023 at 08:50 AM

Table of Contents

Open Table of Contents

Create a Python 3+ Virtual Environment in Windows 10

Why Use a Virtual Environment?

Python developers are discouraged from executing their python scripts globally. This is due to the problem of executing unrelated scripts that have different business logic, versions and dependencies.

To mitigate the aforementioned problem, tools like virtualenv and pipenv help python developers to create virtual environments that isolate their builds within specific environments.

Guide

In order to create a virtual environment in Windows 10 using the virtualenv tool, follow these six easy steps.

  1. Type cmd on Windows search bar then open Windows Command prompt.

  2. Type $ python on the terminal to access Microsoft Office Store.

  3. Download python from Microsoft Office Store.

  1. Install virtualenv tool for creating isolated Python environments.

    $ python -m pip install --user virtualenv

  1. Create a virtual environment.

    $ virtualenv env_name

    Example: $ virtualenv virtual

    • This creates a virtual environment named virtual
  2. Activate the virtual environment, virtual created in the step above.

    $ source ./virtual/Scripts/activate

  3. Deactivate the virtual environment.

    $ deactivate

Troubleshooting

If you run the command:

$ virtualenv --help or $ virtualenv virtual

Then the output you recieve is:

bash: virtualenv: command not found

or

virtualenv : The term 'virtualenv' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ virtualenv --help
+ ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (virtualenv:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

whereas you have correctly installed pip and python, then, the issue is that you have installed virtualenv in a relative path that is not on environment variables PATH.

WARNING: The script virtualenv.exe is installed in 'C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\Scripts'
which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

Therefore, since python is installed globally, you can simply run:

$ python -m virtualenv --help or $ python -m virtualenv virtual