"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Selectively Inherit Specific Packages into Python Virtual Environments?

How to Selectively Inherit Specific Packages into Python Virtual Environments?

Published on 2024-11-04
Browse:221

How to Selectively Inherit Specific Packages into Python Virtual Environments?

Creating Virtual Environments That Inherit Specific Packages

Virtual environments are isolated environments used to manage dependencies and package installations for Python projects. However, there may be situations where you want a virtual environment to include only a subset of packages from the default Python installation.

Selective Inheritance of Libraries

To selectively inherit specific libraries, use the following approach:

  1. Create a Virtual Environment with System Packages:

    Run the following command to create a virtual environment that includes system packages:

    virtualenv --system-site-packages virtualenv_name
  2. Activate the Virtual Environment:

    Activate the created environment to start using it:

    source virtualenv_name/bin/activate
  3. Install Packages Locally:

    Use pip install --ignore-installed or pip install -I to install packages in the virtual environment rather than the system Python. This will override the system-wide packages with locally installed versions:

    pip install --ignore-installed matplotlib
  4. Shadowing Global Packages:

    The virtual environment's package directory takes precedence over the system Python's directory. Therefore, the locally installed packages will shadow the global ones, allowing you to import them without conflicts.

  5. Restart the Python Interpreter:

    Restart the Python interpreter to ensure the changes take effect.

Release Statement This article is reprinted at: 1729215739 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3