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.
-
Type cmd on Windows search bar then open Windows Command prompt.
-
Type
$ python
on the terminal to access Microsoft Office Store. -
Download python from Microsoft Office Store.
-
Check that python has been installed successfully.
$ python --version
- This command should output:
python 3.11.4
-
Check that pip has been installed together with python.
$ pip --version
- This command should output:
pip 23.2.1
-
Install virtualenv tool for creating isolated Python environments.
$ python -m pip install --user virtualenv
-
Check that virtualenv wrapper has been installed successfully.
$ virtualenv --help
- If you do not receive help command args, then installation was unsuccessful.
- You can skip over to troubleshooting section below if pip and python have been installed successfully.
-
Create a virtual environment.
$ virtualenv env_name
Example:
$ virtualenv virtual
- This creates a virtual environment named
virtual
- This creates a virtual environment named
-
Activate the virtual environment,
virtual
created in the step above.$ source ./virtual/Scripts/activate
-
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