How To Setup a Python Virtual Environment on Windows 10
Like other languages, Python has its way to download, store, and resolve packages. Creating a Python Virtual environment will allow you to work on an isolated copy of Python for specific projects without interfering or impacting the working of other ongoing projects. These Python environments can be easily hosted on a VPS server or a dedicated server to be available for multiple users. It will be beneficial for the different team members working on the same project.
It is a tool that provides you with the facility of installing multiple Python versions for each project. All the Python projects will use the same directory to store and retrieve site-packages. That is why we require a virtual environment because Python cannot differentiate between different versions in the same site-package directory. To overcome this Python problem, you can create a different Python virtual environment for every project. There are no limitations on how many virtual environments you can create within a system.
You can use Python’s module named a virtual environment or venv for creating a unique environment for each project. You can also install the required project settings along with a neatly organized project. Venv does not modify the default system setting of Python or its modules. The venv will create a folder containing necessary executables to use required Python packages.
If you want to leverage the functionality of venv on Windows 10. In that case, you are recommended to enable the Windows Subsystem for Linux (WSL) that will allow you to run Linux distro within Windows 10.
It will be beneficial because most of the Python information is for Linux environments. Most of the developers use Linux installation tools. With WSL, the development and production environment will be compatible.
Process of enabling WSL on Windows 10
- Click the Start menu, and you can search for “Turn Windows features on or off.”
- Open the link from the appeared panel.
- A Windows features pop-up menu will appear.
- Scroll down to search and select the checkbox for the “Windows Subsystem for Linux” option
- Reboot the system, which is necessary to apply the system changes.
Store image on your image server and adjust path/filename/extension if necessary.
Installing your choice of Linux Distro
There are several Linux distributions available online, and you can download them. But prefer downloading the latest version of the particular Linux distribution. Here we are considering a Ubuntu 18.04 LTS distribution as it has an up-to-date version, a vast support community, and is well documented even for beginners.
- Download the Ubuntu 18.04 LTS version from the Microsoft store.
- After the download is complete, type “Ubuntu 18.04 LTS” in the Start menu.
- A prompt menu will appear to ask you to create a username and a password if you are a first time user.
- After that, you will be signed in the terminal as the default user automatically.
- Then we’ll run an update on the new OS using the below command.
sudo apt update && sudo apt upgrade
If the command mentioned above does not work, you have to update and upgrade the new OS manually.
Now you can use the PowerShell to install the distribution and navigate to the folder containing the newly downloaded Linux distribution (app_name.appx) and run the below command.
Adding distro path in Windows environment path
We will use PowerShell to add the path setting.
$userenv = [System.Environment]::GetEnvironmentVariable("Path", "User")
_[System.Environment]::SetEnvironmentVariable("PATH", $userenv + ";C:\Users\Admin\Ubuntu", "User")
After you initialize the newly installed distribution, you have to launch a new instance. You can launch it from the distro’s executable file available on the start menu. Later, it gets stored locally on your system and ready to use. For Windows server users, launch the Linux distribution’s executable file stored under the distro’s installation folder.
You can set up a Python virtual environment on Windows 10, which will require you to follow the below-mentioned steps.
- You have to install the compatible Python version.
- Then, you have to install Pip.
- Installing a virtual environment.
- Then, installing VirtualEnvWrapper-win.
It is preferred to install the latest and updated Python version for setting up the environment. So we are going for the Python 3.8.0 version to get started.
- It comes with a web-based Python installer, which will also install the required software.
- It comes with Python redistributable files with Windows build that allow Python to integrate with another software bundle easily.
- With the newly installed Ubuntu 18 distribution, Python 3 version comes by default.
Usually, PIP is pre-installed with the Python3 version. If you cannot find the required PIP, you can install it manually using the below command.
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
After you install the get-pip.py file, you can save it to the desktop. Then, navigate to the desktop with an admin account to run the file on the command prompt to make it work system-wide.
Installing and creating Virtual Environment
- The venv module comes with a standard library if you are using Python 3 or installing it manually using the below command.
$ pip install virtualenv
- After installation, you can test the installation using the below command on the command prompt.
$ virtualenv --version
- Now we will create the virtual environment (my_project) for our project using the below command. A directory (my_project) will be created with all required executables to use the installed packages.
$ virtualenv my_project
The directory will have the below file structure.
- For using Python 3 interpreter, you can run the below command.
$ virtualenv -p /usr/bin/python3 my_project
- Then navigate to your project location where you want to work. You can run the below command using the command prompt.
- You have to activate the virtual environment (my_project) using the above file structure’s activate batch script with the below command.
$ source my_project/bin/activate
The above prompt is prefixed with my_project, which indicates that the virtual environment is now active.
Installing the VirtualEnvWrapper-win
The virtual environment may solve the package management problem to some extent but comes with some challenges in managing the environments, which can be solved using the virtualenvwrapper tool.
- It helps you to organize and manage the virtual environments within one location.
- It allows you to create, delete, and copy environments.
- You can also switch between different environments.
You can install the wrapper batch script using the below commands.
pip install virtualenvwrapper-win
git clone git://github.com/davidmarble/virtualenvwrapper-win.git
- You can check the location of the wrapper script using the below command.
$ which virtualenvwrapper.sh
- Once it is installed and verified, you can navigate to that folder and run the below command.
_python setup.py install _
After this command, the Python virtual environment is ready to use.
If you do not want to continue with the virtual environment, you can deactivate it using the below command.
(my_project) $ deactivate
This article has pointed to how you can install Linux distribution on Windows 10. Even if you are using Windows 10, it is preferred that you have a Linux distribution installed to set up the Python virtual environment to leverage the functionality. It is easier to grasp Linux commands and implement them. It gives you a better environment and security to install the packages and modules, whatever is required. You will get to know how to install Python, pip and start and activate the virtual environment along with the file structure.
I thought about creating a virtual Python environment on my old Linux, but I never got around to it. I want to write my own programs in this programming language, but in a way that would not disrupt the rest of the system on the computer. Your article will help me think about a new method, so I appreciate it. Thank you. What if I am going to buy a dedicated netherlands server, which will be at my full disposal. Will I be able to move the virtual environment or the projects created there to the new server or sites of that server? I want to be a sought after developer, but I'm so confused. Can you answer my question?