问题
There are a few Python dependencies that I would like to be available in every venv (virtual environment) that I create for each project. For example black, flake8 and pytest. Is that possible and if so, how to achieve that?
I'd like to install these three once under my main Python installation, instead I have to reinstall all of them in every venv that I create when I start a new project. This is specially annoying when using VSCode which throws popups complaining about "Linter flake8 is not installed" or "...black is not installed", etc. when you switch to a venv where you haven't installed these packages.
回答1:
One Possible solution would be,
- Deactivate virtual environment.
- Install all packages which you need to be available globally.
- Again activate virtual environment.
make sure you enable inherit package from global
Note: Please refer to these SO threads as well for more information.
- How do I install a pip package globally instead of locally?
- What does sudo -H do?
回答2:
Let me answer my own question based on comment from @jonrsharpe.
Assuming you want to have black, flake8 and pytest available 'globally' or in other words you want to have these packages in every new venv that you create but don't want to repeat pip install black flake8 pytest
each and every time. Here's what you can do:
- install the packages once under your main Python version (that you'd like to use for your venvs. NOTE: you make have several Python versions installed.)
- when creating a new venv use
--system-site-packages
option. For example:
python -m venv --system-site-packages .venv/dev
- activate your venv, i.e.
source .venv/dev/bin/activate
and check w/pip list
that the packages are available
来源:https://stackoverflow.com/questions/61308334/how-to-install-python-dev-dependencies-globally-so-that-i-dont-have-to-reinst