How to creating Virtual Environment in python?

How to creating Virtual Environment in python_

Virtual environment is created to isolate the project and its dependencies from any other python project. It also helps in organize your packages much better and know exactly the packages you need to run your code in case someone else needs to run it on their machine. Your main python package directory does not get flooded with unnecessary python packages.

Venv: We will use venv to create virtual environment for our project, the vevn will allow us to create an isolated python environment and all the project specific package installation will be done in the virtual environment only. It is created on the base of default python installation.

To use virtual environment in the project, one need to activate the environment using activate batch file which can found in the scripts folder of virtual environment folder. Similarly, after the usage one can deactivate by calling deactivated batch file from terminal.

Steps to create Virtual Environment:

  1. Go to the Folder where you want to create the project and invoke cmd.
  2. create project dir in command prompt
    1. D:\pyProj\QueryParserApp>mkdir proj1
  3. Change the current directory to newly created directory
    1. D:\pyProj\QueryParserApp>cd proj1
  4. Create Virtual environment
    1. D:\pyProj\QueryParserApp>python -m venv venv_sql_parser
  5. Activate Virtual Environment:
    1. D:\pyProj\QueryParserApp>venv_sql_parser\Scripts\activate
  6. Virtual Environment is activated:
    1. (venv_sql_parser) D:\pyProj\QueryParserApp>
  7. Adding any project dependency:
    1. (venv_sql_parser) D:\pyProj\QueryParserApp>python -m pip install sqlparse

In below screen snippet we have created two project and for each project we have created separate virtual environment.

Leave a Reply

Your email address will not be published. Required fields are marked *