How to install Python (dev) dependencies globally so that I don't have to reinstall them in every venv?

半世苍凉 提交于 2020-04-30 08:46:11

问题


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:

  1. 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.)
  2. when creating a new venv use --system-site-packages option. For example:
python -m venv --system-site-packages .venv/dev
  1. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!