Open In App

How To Install A Package Inside Virtualenv?

Last Updated : 24 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Understanding the significance of a virtual environment in Python is crucial before exploring package installation in virtualenv. It provides a segregated workspace for projects, isolating dependencies and ensuring the system-installed Python remains unaffected. This isolation enables seamless switching between projects, eliminating concerns about compatibility issues. This article will guide you through the creation and deletion of virtual environments in Python. Additionally, it will elucidate the process of installing packages within a virtual environment using venv.

What is A Package?

A Package in Python is a directory containing one or more Python modules, which can be imported and utilized in a Python program. This approach allows programmers to achieve organized and structured code. Python offers numerous packages, installable using commands such as conda or pip.

How To Install A Package Inside Virtualenv?

Here, we will guide you through a step-by-step process to install a package inside a virtual environment, covering everything from creating the virtual environment to deactivating it. Let’s get started:

Step 1: Create a Virtual Environment

To create a virtual environment, navigate to your project directory and execute the following command in your command prompt or terminal:

 python -m venv .venv

running this command, a new folder named “.venv” will be created, serving as the environment directory. The output will resemble the following:

Step 2: Activate a Virtual Environment

To use the virtual environment for installing packages we need to activate it first. To activate virtual environment we will use the following command

.\.venv\Scripts\activate

hh

Step 3: Install Package Inside Virtualenv

To install packages within a previously created virtual environment, we use the “pip install” command. Let’s proceed to install a package named “pandas.”

python -m pip install pandas

by executing this command all the required files will get downloaded and package will be installed in the environment itself and you can use this package inside the this environment only.

[04]install

Installing Packages in Virtual Environment

pip command will check all the dependencies required by the pandas and install those as well, in this case they are already satisfied. You can also install package of specific version if you want to. Following will be the command for that:

[05]install_specific

Installing Specific Version of package using pip in venv

Step 4: Get list of Install Package in Virtualenv

Sometimes, it is necessary to check the installed packages within a virtual environment. For instance, to list the installed packages, use the following command:

python -m pip list
[05]list_packages

List All Installed Packages In Virtual Environment

Step 5: Deactivate a Virtual Environment

To deactivate a virtual environment we will use the following command

deactivate

this command is a standalone keyword used to deactivate the virtual environment.

[03]detivate-env

Deactivating Virtual Environment

Conclusion

In conclusion, a virtual environment in Python is a crucial tool for ensuring the isolation of dependencies, enabling developers to work on multiple projects without concerns about conflicts between different package versions. This isolation helps maintain project-specific environments and avoids potential clashes between dependencies


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads