Like other languages, Python can load, store, and resolve packages. Creating a virtual Python environment will allow you to work on a separate copy of Python (environment) for each specific project without interfering or affecting the operation of other projects.
Join the channel Telegram of the AnonyViet 👉 Link 👈 |
These Python environments can easily be hosted on a VPS or dedicated server for user support. It will benefit different team members working on the same project.
Why need a Python virtual environment on Windows?
A virtual environment is a tool that provides you with different Python version settings for each project. All Python projects will use the same directory to store and retrieve site-packages. That’s why I need a virtual environment because Python can’t distinguish between different versions in the same site-packages directory. To work around this issue, you can create different virtual Python environments for each project. There is no limit to the number of virtual environments you can create in the same system.
You can use a module called virtual environment or venv to create a unique environment for each project. You can also customize the necessary environment settings for your project. Venv does not modify Python’s default system settings or its modules. Venv will create a directory containing the executables needed to use the Python packages required by the project.
Condition
If you want to use venv functionality on Windows 10. You need to enable Windows Subsystem for Linux (WSL) to allow you to run a Linux distribution in Windows 10.
Since most of the Python information is for the Linux environment. Most developers use Linux installation tools. So with WSL, Python will be more easily compatible with the environment and developers.
How to enable WSL on Windows 10
Step 1: Click on the Start Menu and search for “Turn Windows features on or off”.
Step 2: Click on the first result.
Step 3: Scroll down and select the option “Windows Subsystem for Linux”.
Step 4: Restart the system to apply the changes.
Installing Linux Distros
You can learn the versions Linux is available and install them. But in this article, I will use the Ubuntu 18.04 LTS distribution because of its frequent updates, large support community, and complete documentation for newbies.
Step 1: Setting Ubuntu 18.04 LTS from the Microsoft Store.
Step 2: After downloading, you need to open Ubuntu from the Store or Start menu.
Step 3: The first time you open Ubuntu, it will ask you to create a username and password.
Step 4: Update the system with the command below:
sudo apt update && sudo apt upgrade
If the above command does not work, you must update and upgrade the operating system manually.
Now you need to use PowerShell to install the distro and navigate to the folder containing the newly downloaded Linux distro (app_name.appx) and run the command below.
Add-AppxPackage .\app_name.appx
Add distro path to Windows environment
You need to use PowerShell to install more paths.
$userenv = [System.Environment]::GetEnvironmentVariable("Path", "User")
Eg:
[System.Environment]::SetEnvironmentVariable("PATH", $userenv + ";C:\Users\Admin\Ubuntu", "User")
Open a new distro session
After declaring the newly installed distribution, you must launch a new session. You can launch it from the Ubuntu shortcut available on the Start Menu. It will then be stored locally on your system and ready to use. For Windows server users, launch the Linux distribution’s executable file stored in the distribution’s installation directory.
Environment setting
You need to set up a Python virtual environment on Windows 10 by performing the steps below:
- You must have a compatible version of Python installed.
- Then install Pip.
- Install virtual environment.
- Finally install VirtualEnvWrapper-win.
Install Python
You should install the latest and up-to-date Python version to set up the environment. So I will install Python version 3.8.0. You can also use Python 3.9 Please.
Install PIP
Usually, PIP comes pre-installed in Python3. If you cannot find the PIP, you can install it manually using the command below.
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
After installing the get-pip.py file, you can save it to your desktop. Then navigate to the desktop and use the admin account to run the file.
python3 get-pip.py
cd Desktop
Python get-pip.py
Install and create a virtual environment
The venv module comes with the standard libraries if you are using Python 3 or you can also install it manually using the command below.
$ pip install virtualenv
After installation you can test virtualenv with below command.
$ virtualenv --version
Now we will create virtual environment (my_project) for the project using below command. A directory (my_project) will be created with all the executables required to use the installed packages.
$ virtualenv my_project
The directory will have the same file structure as above.
To use the Python 3 compiler, you can run the command below.
virtualenv -p /usr/bin/python3 my_project
Then access the newly created project folder with the command below.
cd my_project
You have to activate virtual environment (my_project) using above file structure activation script using below command.
$ source my_project/bin/activate
(my_project) $
If it says my_project or shows the name of your project, the virtual environment is active.
Install VirtualEnvWrapper-win
Virtual environments can solve the package management problem to some extent but will come with some hassle in managing the environment, you can solve that by using virtualenvwrapper tool.
- It helps you organize and manage virtual environments in one location.
- It allows you to create, delete and clone environments.
- You can also switch between different environments.
You can install the wrapper using the commands below.
Using pips:
pip install virtualenvwrapper-win
Using git:
git clone git://github.com/davidmarble/virtualenvwrapper-win.git
You can check the location of the wrapper script with the command below.
$ which virtualenvwrapper.sh
Once it is installed and verified, you can navigate to that directory and run the command below.
python setup.py install
After this command, the Python virtual environment is available.
Deactivate the virtual environment
If you do not want to continue with the virtual environment, you can deactivate it with the command below.
(my_project) $ deactivate
Conclusion
This article showed how you can install a Linux distribution on Windows 10. Even if you are using Windows 10, you should install the Linux distribution to set up a Python virtual environment and take advantage of the functionalities. that it has.
It gives you a better environment and security to install packages and modules. You will know how to install Python, pip and start and activate the virtual environment along with the file structure.