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 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.
Prerequisites
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.
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.
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.
Add-AppxPackage .\app_name.appx
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")
Launching a distribution
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.
Setting up the process
You can set up a Python virtual environment on Windows 10, which will require you to follow the below-mentioned steps.
Installing Python
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.
Installing PIP
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.
python3 get-pip.py
cd Desktop
Python get-pip.py
$ pip install virtualenv
$ virtualenv --version
$ virtualenv my_project
The directory will have the below file structure.
$ virtualenv -p /usr/bin/python3 my_project
cd my_project
$ source my_project/bin/activate
(my_project) $
The above prompt is prefixed with my_project, which indicates that the virtual environment is now active.
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.
You can install the wrapper batch script using the below commands.
Using pip:
pip install virtualenvwrapper-win
Or
Using source:
git clone git://github.com/davidmarble/virtualenvwrapper-win.git
$ which virtualenvwrapper.sh
python setup.py install
After this command, the Python virtual environment is ready to use.
Deactivating the virtual environment
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 explained 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.