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:
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
Activate the Virtual Environment:
Activate the created environment to start using it:
source virtualenv_name/bin/activate
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
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.
Restart the Python Interpreter:
Restart the Python interpreter to ensure the changes take effect.
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