Open In App

Managing Python Dependencies

Last Updated : 22 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Managing dependencies becomes crucial to ensure smooth development and deployment processes. In this article, we will explore various methods for managing Python dependencies, from the basics of using pip to more advanced tools like virtualenv and pipenv.

How to Manage Dependencies in Python

Below are some of the ways by which we can manage Python dependencies:

Manage Python Dependencies Using Pip

Pip is the package installer for Python, and it comes pre-installed with Python versions 3.4 and above. To check if you have it installed, run:

pip --version

1

If not installed, you can install it using the following:

python -m ensurepip --default-pip

2

Step 1: Install a Package

To install a package, use the following command, Replace package_name with the name of the desired package. For example:

pip install package_name
pip install requests

3

Step 2: Managing Packages

To list installed packages and their versions, To uninstall a package:

pip list
pip uninstall package_name

4

Manage Python Dependencies Using Virtualenv

Virtualenv is a tool to create isolated Python environments, preventing conflicts between project dependencies. It can be installed via pip:

pip install virtualenv

qq

Step 1: Create a Virtual Environment

Navigate to your project directory and run:

python -m venv env

a

Step 2: Activate the Virtual Environment

On Windows:

venv\Scripts\activate

b

Step 3: Install Packages within Virtualenv

With the virtual environment activated, install packages as usual with pip, To exit the virtual environment:

pip install package_name
deactivate

d

Manage Python Dependencies Using Pipenv

Pipenv is a higher-level tool that simplifies dependency management and adds functionality like a Pipfile for package specification.

Step 1: Install Pipenv

Install Pipenv using pip:

pip install pipenv

l1

Step 2: Create and Activate Virtual Environment

Navigate to your project directory and run, To install a package:

pipenv install
pipenv install package_name

c

Step 4: Deactivate the Virtual Environment

To exit the virtual environment:

exit

Alternative Solutions

  • Conda: The Conda is a package manager that also manages virtual environments. It is commonly used for the data science and scientific computing.
  • Poetry: The Poetry is a dependency management and packaging tool for Python projects. It simplifies the process of the managing dependencies and packaging projects.

Example : In this example, we have a Python script script.py that imports the NumPy library as np. We perform a simple operation to calculate the sum of the array using the NumPy’s np.sum() function.

Python




import numpy as np
  
# Perform some operations using the NumPy
arr = np.array([1, 2, 3, 4, 5])
print("Sum:", np.sum(arr))


Output:

Sum: 15


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads