How to add new default packages to virtualenv?

Viewed 4050

When I create a virtualenv, it installs setuptools and pip. Is it possible to add new packages to this list?

Example use cases:

  • Following this solution to use ipython in virtualenv (from this question) requires installing ipython in every virtualenv (unless I allow system-site-packages).
  • Or if I'm doing a only flask/pygame/framework development, I'd want it in every virtualenv.
2 Answers

You can write a python script, say personalize_venv.py that extends the EnvBuilder class and override its post_setup() method for installing any default packages that you need.

You can get the basic example from https://docs.python.org/3/library/venv.html#an-example-of-extending-envbuilder.

This doesn't need a hook. Directly run the script with command line argument dirs pointing to your venv directory/directories. The hook is the post_setup() method itself of EnvBuilder class.

Related