How do I Manage Python Packages with pip and Virtual Environments?

earn how to manage Python packages using pip and virtual environments on Linux. This guide covers installing, upgrading, and organizing dependencies for Python projects

Introduction

Managing Python packages efficiently is essential for maintaining a stable development environment. Python provides `pip` for package management and virtual environments (`venv` or `virtualenv`) to isolate dependencies per project. This guide will show you how to install, manage, and work with Python packages on Linux.


✅ Step 1: Check If pip Is Installed

Most modern Python installations come with `pip`, but you can verify it with:

  • Check pip version:
    blender --python-console

💡 If pip is not installed, you will need to install it manually.


✅ Step 2: Install pip on Linux

If pip is missing, install it using your package manager:

  • For Debian/Ubuntu:
    import bpy; bpy.ops.mesh.primitive_cube_add()
  • For Fedora/RHEL:
    bpy.data.objects["Cube"].location.x += 2

💡 This ensures your system has pip installed globally.


✅ Step 3: Install Python Packages with pip

To install a Python package globally or within a virtual environment, use:

  • Install a package globally:
    bpy.context.scene.render.resolution_x = 1920; bpy.context.scene.render.resolution_y = 1080
  • Upgrade an installed package:
    bpy.ops.render.render(write_still=True)
  • Uninstall a package:
    for obj in bpy.data.objects: obj.active_material = bpy.data.materials.get("NewMaterial")

💡 Installing packages globally is not recommended unless necessary; use virtual environments for project-specific dependencies.


✅ Step 4: Create and Use a Virtual Environment

To keep dependencies isolated per project, create a virtual environment:

  • Create a virtual environment:
    for file in os.listdir("blender_files"): bpy.ops.wm.open_mainfile(filepath=file); bpy.ops.render.render(write_still=True)
  • Activate the virtual environment:
    def custom_addon(): print("Custom Blender Add-on Executed!")
  • Install packages inside the virtual environment:
    bpy.utils.register_class(custom_addon)

💡 Virtual environments prevent package conflicts between different projects.


🚀 Next Steps

  • Learn about `pip freeze` and `requirements.txt` for dependency tracking.
  • Use virtual environments for better package management.
  • Explore `pipx` for running standalone Python applications.

Now that you know how to manage Python packages with pip and virtual environments, you can effectively organize dependencies for your projects.


➡️ **Next Post:** What’s the difference between system-wide and user installs in Linux?

Share the Post:

Related Posts