Why Virtual Environment is required ?
- If your Python interpreter was installed globally for all the users of your computer, chances are your regular account is not having permission to install packages using pip.
- You have a completed Web Application which is developed using 0.x version of Flask. Now you are starting development of another Web Application and you want to use the latest version of Flask 0.y , but if you replace 0.x with 0.y there is risk attached of malfunctioning of old Application.
For the above issues of maintaining different versions of packages for different applications, Python uses the concept of virtual environments.
A virtual environment is a complete copy of the Python Interpreter. When you install packages in a virtual environment, the system-wide Python interpreter is not affected, only the copy is affected. So the solution to have complete freedom to install any versions of your packages for each application is to use different virtual environment for each application.
Additional Benefit : Virtual Environments are owned by the user who creates them, so they do not require an administrator account.
Creating Virtual Environment
Python3
$ python3 -m venv nameOfVirtualEnvironment
eg:
$ python3 -m venv flaskProject
Python2.7
$ pip install virtualenv
$ virualenv nameOfVirtualEnvironment
eg: virtualenv flaskProject<br>Activating Virtual Environments
Linux
$ source flaskProject/bin/activate Windows
$ flaskProject\Scripts\activate
Deactivating Virtual Environments
(flaskProject)$ deactivate
This is the basic understanding of Virtual Environments in Python. I hope you got to know something useful from this blogpost.
Please do provide Feedback.
Thankyou.
Comments
Post a Comment