Virtual environment in Python
-
Virtual environments in python are like a separate sandbox which maintains all the required python packages to run a project.
When we create a project in Python, we always have the option to not use a virtual environment.
Usually, newcomers to Python tend to not use this feature. And this may not arise any major issues initially.
But as and when we start working with multiple projects and packages, use of virtual environments become a necessity.
After setting up virtual environment, all the installed packages will be kept in a separate place not dependent on any other projects.
This means that we will have to install all the required packages, even though it was previously installed for another project. -
Why do we need virtual environments
Python heavily uses the concept of modularization and this is achieved using Python Libraries/Packages.
Python itself has many built-in libraries like datetime, math etc.
In addition to this, there are tons of third-party libraries, which will be required in our projects.
And these libraries can have multiple versions.
When we install a package for our project, this gets copied to site_packages folder mentioned in the pythonpath.
Suppose we have 2 projects that require a third-party package.
For our first project we installed this and may have worked fine at that time.
For our second project, we may be requiring the latest version of this packages to use the latest features.
This will require an upgrade the package.
Suddenly the first project may stop working because, it may be dependent on the older version of this package.
This is where the need for virtual environment arises.
-
Setting up virtual environment
To create virtual environment: Navigate to the folder from comment prompt and type:
python -m venv TestProject
This command will create some folders like Include, Lib, Scripts and varies depending on OS. All our pip installations will be kept in Lib/site-packages folder.
Next step is to activate the VE using command:Windows:TestProject\Scripts\activate.batLinux/MacOS:source TestProject /bin/activateNow we can execute all python commands in this virtual environment like installing a package, running a module, etc.
-
pip not found error
In some cases, when we try to install packages in virtual environments using pip commands, we will get an error like: pip not found.
To fix this, run the below command:
python -m ensurepip -
Setting up virtual environment for older python versions
Some of the packages that we need may not be working in latest version of Python.
If so, we have to setup everything for older python version, including virtual environment as below.
This sample sets up Python 3.9 virtual environment.Windows:py -3.9 -m venv TestProjectLinux/MacOS:python3.9 -m venv TestProjectAfter setting this up, we can install additional packages to this virtual environment using command pip3.9 install <PackageName> or pip install <PackageName>