Saturday, February 18, 2023

Python virtual environment

 

Installing packages using pip and virtual environments

    A virtual environment is created on top of an existing Python installation, known as the virtual environment’s “base” Python, and may optionally be isolated from the packages in the base environment, so only those explicitly installed in the virtual environment are available.

Installing Python virtual environment:

    virtualenv is used to manage Python packages for different projects. Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects. You can install virtualenv using pip.

    Open command prompt and type or just copy and paste:

py -m pip install --user virtualenv


Creating a virtual environment:

    

    virtual environment allow you to manage separate package installations for different projects. They essentially allow you to create a “virtual” isolated Python installation and install packages into that virtual installation. When you switch projects, you can simply create a new virtual environment and not have to worry about breaking the packages installed in the other environments. It is always recommended to use a virtual environment while developing Python applications.

    To create a virtual environment, go to your project’s directory and run venv.

py -m venv env



The second argument is the location to create the virtual environment. Generally, you can just create this in your project and call it env.

venv will create a virtual Python installation in the env folder.

Activating a virtual environment:

    Before you can start installing or using packages in your virtual environment you’ll need to activate it. Activating a virtual environment will put the virtual environment-specific python and pip executables into your shell’s PATH.

.\env\Scripts\activate

for example in my case


after executing the command




No comments:

Post a Comment